CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Oct 2011
    Posts
    3

    I cannot pass data into a 64 bit DLL from a 64 bit win32 app

    Hi

    I am attempting to create a 64 bit win32 test application that loads a 64 bit MFC test DLL. I created a test function within the DLL that takes in a pointer to an integer. This function is an exported DLL function. I load the DLL in the win32 executable. I get access to the exported function. I call the DLL test function, sending it a pointer to an integer where the value is = to 100. I then step into the DLL test function using the debugger. Within the DLL test function the address of the integer pointer is undefined or NULL. So the pointer was not passed properly. Now, if I rebuild the application and the DLL using the 32 bit compilers then all problems go away. The integer pointer is passed without any issues into the DLL test function and I can see the value (100).

    Any ideas would be greatly appreciated.
    thanks


    Here is my code for the win32 application:



    typedef void (CALLBACK* D_TestDllTest)(int* Val);

    .
    .
    .

    int APIENTRY _tWinMain(HINSTANCE hInstance,...)
    {

    .
    .
    .

    // load "TestDLL.dll"
    CString Path = "c:\\TestDll.dll";
    HINSTANCE TestDllHInst = AfxLoadLibrary((LPCTSTR)Path);

    // get pointer to exported DLL function
    D_TestDllTest TestDllTest = (D_TestDllTest)GetProcAddress(TestDllHInst, "TestDllTest");

    // call the DLL function
    int Val = 100;
    TestDllTest(&Val);

    .
    .
    .

    }



    Here is the DLL code:


    void CTestDllApp::TestDllTest(int* Val)
    {

    *Val = 2;

    return;
    }

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: I cannot pass data into a 64 bit DLL from a 64 bit win32 app

    Are you building a 32bit exe? If so, I don't believe you can call functions in a 64bit DLL from a 32bit exe, and vice versa. Both the exe and library must be of the same "type" (32 bit or 64 bit).

    Viggy

  3. #3
    Join Date
    Oct 2011
    Posts
    3

    Re: I cannot pass data into a 64 bit DLL from a 64 bit win32 app

    I am building a "Win32" app (which from what I have read can be built as a 32 bit or 64 bit application). When I build my application I am using the "x64" compiler. When I run my application I can confirm that it is a 64 bit app by finding it within the process list of the task manager. 32 bit apps have a "*32" displayed to the left of their name in the process list. My test application does not have the "*32" text, so at least windows considers it to be a 64 bit application. From what I can gather there is no such thing as a "win64" application; instead, there are "win32" applications built using the 64 bit "X64"compiler.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: I cannot pass data into a 64 bit DLL from a 64 bit win32 app

    Quote Originally Posted by jmiller9126 View Post
    I call the DLL test function, sending it a pointer to an integer where the value is = to 100. I then step into the DLL test function using the debugger. Within the DLL test function the address of the integer pointer is undefined or NULL. So the pointer was not passed properly.
    Forget about the debugger -- did the program crash when you attempted to set the pointer?

    You're relying strictly on what the debugger is showing. Maybe it's the debugger that is buggy, and there is nothing wrong.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Oct 2011
    Posts
    3

    Re: I cannot pass data into a 64 bit DLL from a 64 bit win32 app

    Yes, the application crashes in release mode. I tried release mode first and then debug mode.
    Also, I did try passing variables by reference with no better luck.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: I cannot pass data into a 64 bit DLL from a 64 bit win32 app

    Quote Originally Posted by jmiller9126 View Post
    Yes, the application crashes in release mode. I tried release mode first and then debug mode.
    My guess is that you're not using the correct calling convention. In other words, your function signature with regards to __stdcall, __cdecl, are inconsistent.
    Also, I did try passing variables by reference with no better luck.
    Never pass C++ references across module boundaries. A reference is not a pointer.

    The implementation of a reference is compiler-dependent, and probably compiler-version dependent. You cannot guarantee that the internals of a reference are the same between application and DLL. Use references within your program, but never pass them to other programs.

    As a matter of fact, you shouldn't even be passing a pointer to "int", as again, what an "int" is is compiler-dependent. The only types guaranteed to be consistent in size and type, are the Windows types, i.e. LONG, DWORD, BOOL, LPTSTR, etc., and pointers to these types.

    So first, make sure your calling conventions are consistent.

    Second, pass a pointer to a DWORD instead of pointer to int.

    Third, do not even consider passing references.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Jan 2007
    Posts
    102

    Re: I cannot pass data into a 64 bit DLL from a 64 bit win32 app

    Development of 64-bit C/C++ applications. Lesson 2. Support of 32-bit applications in the 64-bit Windows environment.

  8. #8
    Join Date
    Nov 2011
    Posts
    9

    Re: I cannot pass data into a 64 bit DLL from a 64 bit win32 app

    @jmiller9126

    Hi,

    Did you ever get this solved?
    I have a very similar issue.

    Thanks.

    :Ron

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: I cannot pass data into a 64 bit DLL from a 64 bit win32 app

    First of all, there's an obvious prototype mismatch:
    Code:
    typedef void (CALLBACK* D_TestDllTest)(int* Val);  // global or static  __stdcall
    . . .
    Here is the DLL code:
    
    
    void CTestDllApp::TestDllTest(int* Val)   // thiscall
    {
    
    *Val = 2;
    
    return;
    }
    The more detailed analysis is possible only with full sample code.
    Best regards,
    Igor

  10. #10
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: I cannot pass data into a 64 bit DLL from a 64 bit win32 app

    Did you ever get this solved?
    I have a very similar issue.
    How can you be sure about the issue similarity, as there was no complete code shown.
    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