user2338150
user2338150

Reputation: 479

Passing a HANDLE to a DLL

I'm new to Win32 programming. I'm trying to pass a HANDLE obtained using CreateFile() to a function in a DLL. But upon trying to read bytes, dwBytesRead says 0. Am I allowed to pass HANDLEs to DLL entries? I read here [Writing DLLs] that resources of the caller do not belong to callee, and hence I should not call CloseHandle() or things like free() for malloc() in caller.
Is my understanding correct? Kindly point me in the right direction. Here's the code:

main.c

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>

#define BUFFERSIZE 5

int __declspec( dllimport ) hello( HANDLE );

void __cdecl _tmain(int argc, TCHAR *argv[])
{
    HANDLE hFile; 

    printf("\n");
    if( argc != 2 )
    {
        printf("Usage Error: Incorrect number of arguments\n\n");
        _tprintf(TEXT("Usage:\n\t%s <text_file_name>\n"), argv[0]);
        return;
    }

    hFile = CreateFile(argv[1],               // file to open
                       GENERIC_READ,          // open for reading
                       FILE_SHARE_READ,       // share for reading
                       NULL,                  // default security
                       OPEN_EXISTING,         // existing file only
                       FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, // normal file
                       NULL);                 // no attr. template

    if (hFile == INVALID_HANDLE_VALUE) 
    { 
        _tprintf(TEXT("Terminal failure: unable to open file \"%s\" for read.\n"), argv[1]);
        return; 
    }

    printf( "Entered main, calling DLL.\n" );
    hello(hFile);
    printf( "Back in main, exiting.\n" );
    CloseHandle(hFile);
}


hello.c

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>

#define BUFFERSIZE 5
DWORD g_BytesTransferred = 0;

VOID CALLBACK FileIOCompletionRoutine(
  __in  DWORD dwErrorCode,
  __in  DWORD dwNumberOfBytesTransfered,
  __in  LPOVERLAPPED lpOverlapped )
 {
  _tprintf(TEXT("Error code:\t%x\n"), dwErrorCode);
  _tprintf(TEXT("Number of bytes:\t%x\n"), dwNumberOfBytesTransfered);
  g_BytesTransferred = dwNumberOfBytesTransfered;
 }

int __declspec( dllexport ) hello( HANDLE hFile )
{
    DWORD  dwBytesRead = 0;
    char   ReadBuffer[BUFFERSIZE] = {0};
    OVERLAPPED ol = {0};

    if( FALSE == ReadFileEx(hFile, ReadBuffer, BUFFERSIZE-1, &ol, FileIOCompletionRoutine) )
    {
        DWORD lastError = GetLastError();
        printf("Terminal failure: Unable to read from file.\n GetLastError=%08x\n", lastError);
        return lastError;
    }
    dwBytesRead = g_BytesTransferred;

    if (dwBytesRead > 0 && dwBytesRead <= BUFFERSIZE-1)
    {
        ReadBuffer[dwBytesRead]='\0';

        printf("Data read from file (%d bytes): \n", dwBytesRead);
        printf("%s\n", ReadBuffer);
    }
    else if (dwBytesRead == 0)
    {
        printf("No data read from file \n");
    }
    else
    {
        printf("\n ** Unexpected value for dwBytesRead ** \n");
    }

    printf( "Hello from a DLL!\n" );

    return( 0 );
}

Upvotes: 1

Views: 906

Answers (1)

user1952500
user1952500

Reputation: 6771

You are missing the SleepEx(5000, TRUE) call from the example.

You are using async-io, in which case you will receive a callback when the read occurs. If you don't wait for the callback you may get 0 bytes read depending on when the callback is triggered.

Upvotes: 1

Related Questions