Have a DLL library with documentation, but has no header file. Is there a simple way to use the functionality of the library in a C++ program? - How do I reference the DLL with CodeBlocks? Thanks in advance.
答案 0 :(得分:1)
As long as you have the documentation to the exported DLL functions, you don't need a header file (or even an import library), but you do need to know aspects of the function you are calling.
The documentation should include
__stdcall
, __cdecl
, etc.)Once you have this information, you have what you need to call the exported DLL function(s).
So for example, let's say that one of the exported functions returns a LONG
and takes as arguments, 2 DWORD
s. The calling convention is __stdcall
. The name of the function is "Func1";
#include <windows.h>
#include <tchar.h>
typedef LONG (__stdcall *MyFunc)(DWORD, DWORD);
int main()
{
// Load the DLL
HMODULE hMod = LoadLibrary(_T("MyDLL.dll"));
if ( hMod ) // check if DLL was loaded successfully
{
DWORD arg1 = 10;
DWORD arg2 = 20;
LONG return_value;
// get the function
MyFunc fn = (MyFunc)GetProcAddress(hMod, "Func1");
// make sure function exists
if ( fn )
return_value = fn(arg1, arg2); // call the function
//...
//...
// unload the DLL if no longer needed
FreeLibrary( hMod );
}
}
Note the calls to LoadLibrary
, GetProcAddress
, and FreeLibrary
. These are the Windows API functions you need to be familiar with to successfully call the exported DLL functions.
Also note the checks to ensure that the library loads successfully, the function exists, etc.
Links to docs:
答案 1 :(得分:0)
Here is example which dynamically loads dllSample.dll and uses add function in it.
typedef int (__stdcall *dllAdd)(int, int);
int main()
{
HINSTANCE dllInstant;
dllAdd Add;
dllInstant = LoadLibrary(_T("dllSample.dll"));
if(dllInstant)
{
Add = (dllAdd)GetProcAddress(dllInstant, "add");
if(Add)
{
cout<<Add(3,4);
}
}
getch();
}