我刚买了一个带有dll文件的设备。我想使用Visual C++对设备进行编程。如何将.dll文件加载到我的项目中?
发布于 2012-02-22 02:47:04
DLL是一个库文件,它包含编译的程序逻辑,就像EXE一样。你不能单独执行它,但就像EXE文件一样,你也不能把它‘加载’到你的项目中。
您将需要使用像Load Library这样的函数来加载库,然后使用GetProcAddress来查找要调用的函数。
编辑:
当你在评论中澄清了你的问题后,你正在尝试编写一个windows程序,而不是在你的设备上运行的程序。
我写了一些示例代码来向您展示如何开始:
#include <windows.h> // This is a windows header file. The functions I mentioned above are declared here
#include "mpusbapi.h" // This is the header file supplied. It declares the function prototypes that are defined in the DLL
int main(int argc, char* argv)
{
// Try to load the library
HMODULE mpbusDLL = NULL;
mpbusDLL = LoadLibrary(L"mpusbapi.dll");
if (mpbusDLL != NULL) {
// If the library could be loaded, then load the functions using GetProcAddress()
// Load the function 'MPUSBOpen' from the DLL
MPUSBOpen = (HANDLE(*)(DWORD, PCHAR, PCHAR, DWORD, DWORD)) GetProcAddress(mpbusDLL, "_MPUSBOpen");
...
MPUSBOpen(...);
}
}这段C代码将加载您的库,然后尝试加载在您的DLL中实现的函数MPUSBOpen。
您将需要以相同的方式加载头文件中定义的其他函数(至少当您想要使用它们时)。
https://stackoverflow.com/questions/9383271
复制相似问题