1

Okay so I'm coming dangerously close to a repost here but my situation is a little bit different than the numerous other posters about this function. I am interfacing with a DLL that was written way back in the day and all I have is the file. I don't have a .lib file so I'm using the LoadLibrary and GetProcessAddress functions. I followed the tutorial on the MSDN website to get the basic structure. the DLL is located in the project folder. it compiles. at run time, I am getting a numerical value for "hinstLib" so I'm assuming the DLL was found. I am getting a null value for "ProcAdd" variable. Other posters had there issues resolved by putting extern C in the DLL functions but I don't really have that option. not to mention, to my knowledge this DLL was written in plain C. I do have an interface document and am quite sure I have the function name correct (replaced with a generic example for these purposes). I honestly didn't run anything past the ProcAdd assignment because it came out NULL. Any thoughts as to why this is giving me a 0 value for the function assignment would be great appreciated. Note: unfortunately due to various reasons I can't upload the DLL.

    #include <iostream>
    #include "stdafx.h"
    #include "Windows.h"
    #include <stdio.h> 

    typedef int(__cdecl *MYPROC)(LPWSTR);

    using namespace std;

    int main()
    {
      HINSTANCE hinstLib;
      MYPROC ProcAdd;
      BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;

      hinstLib = LoadLibrary(TEXT("dllName.dll"));
      if (hinstLib != NULL) 
    { 
    ProcAdd = (MYPROC) GetProcAddress(hinstLib, "funcName"); 

    // If the function address is valid, call the function.

    if (NULL != ProcAdd) 
    {
        fRunTimeLinkSuccess = TRUE;
        //(ProcAdd) (L"Message sent to the DLL function\n"); 
    }
    // Free the DLL module.

    fFreeResult = FreeLibrary(hinstLib); 
} 

// If unable to call the DLL function, use an alternative.
if (! fRunTimeLinkSuccess) 
    printf("Message printed from executable\n"); 

return 0;

}

m25
  • 1,473
  • 2
  • 13
  • 14
  • Your problem is the function name, which you have cleverly replaced with a made-up thing in the code above. – Cheers and hth. - Alf May 21 '14 at 19:23
  • Side note: `#include "stdafx.h"` should appear before all other `#include` statements IIRC. – πάντα ῥεῖ May 21 '14 at 19:28
  • that's what I was afraid of... looks like I'm on my own on this one then. Thanks for assistance guys. – m25 May 21 '14 at 19:47
  • 1
    you're not on your own but you're not helping those who could help you, instead placing barriers in their way. :( now, try `dumpbin /exports blahblah.dll` and check the name(s) of the function. By the way, 2 problems with your code. You have a precompiled header not first, which means everything before it is ignored. Second, you're using Microsoft `TEXT` macros for pre-2000 compatibility with MFC in DLL for Windows 9x (phew), that's not realistic. You can just ditch all the `T` stuff. – Cheers and hth. - Alf May 21 '14 at 19:54
  • wow that's a great tool. I ran it on the DLL, the file names in my interface document were not specific enough. It works now. Now as a followup question... I did the exact same thing again and command prompt gave me an error of "invalid file format; ignored". do I have to close the DLL or something? – m25 May 21 '14 at 20:44
  • that sounds like a non-DLL file (more generally, a non-PE format file). – Cheers and hth. - Alf May 21 '14 at 20:49
  • please disregard that last question. I figured it out. thanks for all the help everyone! – m25 May 21 '14 at 20:53
  • So not sure what the protocol is on asking a tacked on question onto the back of a post but I figured it was worth a shot if you guys were willing to help out. When I hit the line '(ProcAdd) (L"Message sent to the DLL function\n"); ' I get the error _Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention._ I changed the LPWSTR in 'typedef int(__cdecl *MYPROC)(LPWSTR);' to char* because that is fnc input – m25 May 29 '14 at 15:17
  • according to some other posts, this error is due to reference errors. I have reason to believe it is due to my char array inputs. Which would you suspect would be the issue? my understanding of LPWSTR might be a bit off. I'm doing some research into that side but the debugging tool isn't really giving me any details. Thank you for the continued service and support on this question – m25 May 29 '14 at 15:21

1 Answers1

1

Compilers usually mangle function names, then a function named funcName may appear inside the DLL with a name funcName@16 , for example... It depends on calling convention and are important for a function to be called properly. For __cdecl calling convention you probably need _funcName :-) .