CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Threaded View

  1. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    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

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured