Problems in Passing File Handle to DLL
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Problems in Passing File Handle to DLL

Hybrid View

  1. #1
    Join Date
    Nov 2010
    Posts
    46

    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

  2. #2
    Join Date
    Apr 1999
    Posts
    27,423

    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
    Last edited by Paul McKenzie; May 6th, 2013 at 01:35 PM.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,197

    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?

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    5,927

    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
    Attached Files Attached Files
    Last edited by Igor Vartanov; May 6th, 2013 at 03:03 PM.
    Best regards,
    Igor

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,197

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center