简体   繁体   中英

C++ calling DLL functions

I have a DLL which I need to call from a C++ program. I received a header file but it contains definitions of the functions in a weird format and I'm not sure how to call them.

For instance, the documentation says the DLL should contain this function:

int InitLib();

... but the header file only contains this definition:

typedef int (__stdcall *lpInitLib)();

The same thing applies to all of the functions. How can I call them?

What you see is pointer to function. I guess the DLL meant to be loaded at run time (like a plug-in) using LoadLibrary and GetProcAddress Win APIs, instead of link with it like regular dll.

#include <windows.h>

lpInitLib pInitLib = NULL;
//you need to load the DLL  
HINSTANCE dllHandle = LoadLibrary("yourdll.dll");
if (NULL != dllHandle) 
{  
  pInitLib = (lpInitLib)GetProcAddress(dllHandle, "InitLib");
}
if(pInitLib != NULL ) 
    pInitLib ();

check the following full sample: https://msdn.microsoft.com/en-us/library/ms810279.aspx

As a matter of fact, if you know the function prototypes , and have the .lib file , you can create your own header file and link with the DLL instead of load it at runtime.

Then, I guess, your header should look similar to this:

#ifdef __cplusplus
extern "C" {
#endif

__declspec(dllimport) int __stdcall InitLib();

#ifdef __cplusplus
}
#endif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM