CreateFile() returning 0xffffff

Nov 1, 2010 at 4:06pm
Hello everyone,

I hope you can help me with this issue!

I am using Extended MAPI in order to create attachments. The problem however is not with the MAPI part it is with CreateFile(). Here is the code snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
        vector<string> text_file;

	ifstream ifs( "c:\\Working\\output.txt" );
	string temp;

	while( getline( ifs, temp ) )
		text_file.push_back( temp );

	LPATTACH attachment;
	
	if(text_file.size() > 6)
	{
		// There are attachments
		for(int i = 6; i < text_file.size(); i++)
			{
				ULONG ulAttachNum = 0;
				LPATTACH strAttachment;
				LPSTREAM lpAttachStream;

				hr = lpMessage->CreateAttach(NULL, 0, &ulAttachNum, &attachment);
				if (FAILED(hr))
					break;

				hr = lpMessage->OpenProperty(PR_ATTACH_DATA_BIN, &IID_IStream, 0, MAPI_CREATE | MAPI_MODIFY, (LPUNKNOWN *)&lpAttachStream);
				CreateStreamOnHGlobal(NULL, TRUE, &lpAttachStream);

                                // This is the line that is not working
				HANDLE hFile = CreateFile((LPCSTR)text_file[i].c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, 0,NULL);
	
				if ( hFile != INVALID_HANDLE_VALUE) 
				{
					// alocate memory on global
					nBytes = GetFileSize( hFile, NULL);
					if ( ( hMem = GlobalAlloc( GMEM_MOVEABLE, nBytes)) != NULL) 
					{
						//read file content in global allocated mem
						if ( ( lpData = GlobalLock( hMem)) != NULL) 
						{
							ReadFile( hFile, lpData, nBytes, &nBytes, NULL);
							GlobalUnlock( hMem);
						}
					}
					::CloseHandle( hFile);
				}
				
				ULONG ulBytesWritten = 0;
				ULONG ulSize = 0;

				// Write the body to the stream
	
				hr = lpAttachStream -> Write((const void*)lpAttachStream, (ULONG)&ulSize, (ULONG*)&ulBytesWritten);


text_file is a vector, and the 6th string contains a filename (no path, the file resides in the same directory as the code). The error returned is 0xffffff. I have scoured the net and all my research suggests that what I'm doing is right, but its not working.

Can anyone see what I'm doing wrong?

Thanks in advance,

Darren
Last edited on Nov 1, 2010 at 4:08pm
Nov 1, 2010 at 4:14pm
Call GetLastError to find out why it failed.
Nov 1, 2010 at 4:33pm
Thanks, I did not realise that function existed, I can take it from here.

Thanks again!

Darren
Topic archived. No new replies allowed.