How do I use unmanaged DLL in C++ win32 application?
I have a 3rd party DLL that I am trying to use in a win32 C++ application. The DLL alone is all that I have. I believe this library is written in C and I assume is not exposed to COM. Is LoadLibrary() the function must commonly used for this task in Windows? If so can someone provide me with an example of how it is used?
I created a blank win32 in VS so I don't have any of the windows specific headers included etc.
Thanks!
UPDATE
I want t开发者_如何学Goo add that I am trying use the SDL Library which appears to be very widely used. It seems odd that the vendor would not provide more than the DLL if more is necessary. Simple DirectMedia Layer
Yes, you can use LoadLibrary
(actually if you only have a DLL, that is the only way) but it is not enough. To use a DLL you have to know exposed functions declarations (returned types as well as lists of arguments). In counterpart you'll not be able to use DLL.
Assuming that you do have access to a header file or have documentation that identifies the function and their parameters - here is what it looks like
Assume that the functions are as follows
int Func1 (int i1, int i2);
void Func2(void);
Now what follows is the example code
#include windows.h
#include stdio.h
// use either dumpbin or http://www.dependencywalker.com/ to confirm the functions
//Let us assume it has two functions with their prototypes known - Func1 & Func2
int Func1 (int i1, int i2);
void Func2(void);
typedef int (*FUNC1_PTR) (int, int);
typedef void (*FUNC2_PTR) (void);
const char NONAME_DLL[] = "some.dll";
int main(int argc, char *argv[]) {
FUNC1_PTR f1;
//FUNC2_PTR F2;
int i= 1, j = 2;
HMODULE hMod = LoadLibrary(NONAME_DLL);
if (!hMod) {
printf("LoadLibrary fails with error code %d \n", GetLastError());
return 1;
}
f1 = (FUNC1_PTR) GetProcAddress(hMod, "Func1");
if (f1) {
int ret = (*f1)(i, j);
}
FreeLibrary (hMod);
return 0;
}
If you do not have access to the header file or know the functions signature - there are perhaps few ways to proceed
- Run a program which uses this function and break with a debugger when this function is called and see what it does.
- Use interactive disassembler (for example, IDA Pro), it can show the signature elements such as how many parameters are passed into a function.
- Disassembling the function to analyze its prologue and epilogue. Time consuming and not a trivial task, more so because of the different compilers, different calling conventions, optimized code packers, and so on.
- I have heard people say that they have used winedump - you can check it out here http://www.winehq.org/docs/winedump - I have never used it though. Nirsoft.net's DLL Export Viewer is perhaps another tool.
- Also this is another tool- Visual dumpbin that uses UnDecorateSymbolName, the tool is available at http://code.entersources.com/f/Visual-Dumpbin-A-C--Visual-GUI-for-Dumpbin_2_1671_0.aspx. The DLL must be built by MS compiler. Check this link might give you some more clues, ...
You need to manually create an import library for your application to link against and and header files for you to include. Hopefully it's not C++. Here are basic instructions on how to get started: How To Create 32-bit Import Libraries Without .OBJs or Source
Probably the best thing to do here is to define a class to do the calling of LoadLibrary
for you, so that you can't forget to call FreeLibrary
later. For example, see https://bitbucket.org/BillyONeal/pevfind/src/ed3a0f5c37c4/pevFind/Win32RuntimeDynamicLinker.hpp
Note that you really could strip out the boost stuff here -- it's only there to ensure error messages get thrown at compile time if someone tries to use WindowsApi::RuntimeDynamicLinker::GetFunction<t>
where t
is not some form of function pointer.
You'll need more than just the DLL. At the least you'll need to know the functions, their parameters, and their semantics. Without that, you won't be able to make practical use of the DLL. Best place to start is to ask the DLL vendor.
But if you're stuck, then here are some ways you might get started...
Use dumpbin.exe to get some info about the DLL. Alternatively you can use depends.exe for a graphical view. Both of these programs will be somewhere in you Visual Studio folder.
dumpbin.exe /exports will show you the exported functions. These are the functions you can call from your application. Each function has a name and an ordinal, and you can use either of these later in GetProcAddress.
If you see functions called DllCanUnloadNow, DllRegisterServer, etc. then it's a COM DLL. In that case you'll need the corresponding type library. The DLL vendor will probably send you the type library if you ask them. Some DLLs store their type libraries as resources - you can open the DLL directly in Visual Studio and look for a resource of type "TYPELIB". If there is a typelibrary resource then you can export it and save it as whatever.tlb, and then open it in OleViewer or Visual Studio's type viewer - either way, you're ready to start playing around.
If it's not a COM DLL then you can use LoadLibrary to load it, and then GetProcAddress to get a pointer to the function. However, to use the function you'll have to cast the returned FARPROC pointer to a pointer to a function of the appropriate type - that's not straightforward if you're new to this kind of thing.
If a function name in the DLL is mangled, something like ?whatever@@YAHXZ then it's a C++ function and you'll have to use the same compiler that was used to build the DLL. (Not necessarily, but by and large.) But DLLs written in C++ often export functions with C linkage, so with luck you won't have to worry about this.
dumpbin.exe /dependents will show you the list of other DLLs that your DLL uses. If you see mscoree.dll then it uses .NET. In that case you'll have to have the appropriate .NET runtime installed on your user's computer.
精彩评论