Problems in Passing File Handle to DLL
I write a small application and using the following API to open the file:
hHandle = CreateFile(lpszFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
Then I pass the hHandle to a DLL exported function, in that function, I will call
DWORD dwFlags = 0;
ASSERT(GetHandleInformation(hFile,&dwFlags) != 0)
to check if the handle is valid. However, the assert fails. And I use GetLastError to get the error code 6, which means invalid handle.
What is the problem with my codes?
Thanks
Re: Problems in Passing File Handle to DLL
I'll answer your question by asking you two questions:
1) What is a handle in terms of C++? What if one module thinks a handle is really a struct with two int members, while another module thinks that a handle is really a void pointer? What do you think will happen if those two modules start sharing something they both call a "handle", but the internals of the handle are different?
2) What if the application and DLL have separate "handle" managers? What happens if you create a handle in the app, and then pass this "alien" handle to the DLL that uses a separate handle manager? The DLL's handle manager will assume the handle is invalid, since the DLL's handle manager didn't create the handle.
Regards,
Paul McKenzie
Re: Problems in Passing File Handle to DLL
Is the dll used from two different processes? Is the handle created in one process and used in another process without calling DuplicateHandle?
1 Attachment(s)
Re: Problems in Passing File Handle to DLL
Please see the attached sample that passes file handle to dll within the same process. The code does just fine:
Code:
D:\Temp\87>87.exe C:\hiberfil.sys
EXE: last error 32
D:\Temp\87>net helpmsg 32
The process cannot access the file because it is being used by another process.
D:\Temp\87>87.exe 87.exp
EXE: last error 2
D:\Temp\87>net helpmsg 2
The system cannot find the file specified.
D:\Temp\87>87.exe 87dll.exp
EXE: dwFlags = 0
DLL: dwFlags = 0
D:\Temp\87>87.exe 87.exe
EXE: dwFlags = 0
DLL: dwFlags = 0
But when I close handle before passing it to DLL...
Code:
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
BOOL testHandle(HANDLE hFile);
int _tmain(int argc, LPCTSTR* argv)
{
if (argc < 2)
{
_tprintf(TEXT("No path specified. Quit.\n"));
return 1;
}
BOOL success = FALSE;
LPCTSTR lpszFileName = argv[1];
HANDLE hHandle = CreateFile(lpszFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE != hHandle)
{
DWORD dwFlags = 0;
success = GetHandleInformation(hHandle, &dwFlags);
if (success)
{
_tprintf(TEXT("EXE: dwFlags = %d\n"), dwFlags);
CloseHandle(hHandle);
success = testHandle(hHandle);
}
}
if (!success)
{
DWORD err = GetLastError();
_tprintf(TEXT("EXE: last error %d\n"), err);
hHandle = NULL;
}
CloseHandle(hHandle);
return 0;
}
... the handle is reported invalid :)
Code:
D:\Temp\87>87.exe 87dll.exp
EXE: dwFlags = 0
DLL: last error 6
EXE: last error 6
Re: Problems in Passing File Handle to DLL
Good point. I didn't even think to mention not to close the file handle before using it.