1、设置dll延迟加载
属性->配置属性->链接器->输入->延迟加载的Dll-> 添加MyDll.dll
属性->配置属性->链接器->输入->附加依赖项-> 添加MyDll.lib
属性->配置属性->链接器->高级->卸载延迟加载的dll-> 是
属性->配置属性->c/c++->预处理器-》预处理器定义,添加_WIN32_WINNT=0x0502;否则SetDllDirectory失败
//代码中加入SetDllDirectory
void CJWCTApp::SetDllPath()
{
//exe与dll不在同一目录下,需要设置延迟加载,
//设置延迟加载的dll的路径
CString str = theApp.m_strExePath + _T("\\dll\\");
SetDllDirectory(str);
str = theApp.m_strExePath + _T("\\dll\\basler\\");
SetDllDirectory(str);
}
2、引入头文件即可
#define _WIN32_WINNT 0x0502 // need for SetDllDirectory
#include <stdio.h>
#include <Windows.h>
#include "Wrapper.h"
void main()
{
SetDllDirectory(L"bin\\");//wrapper.dll在当前目录的bin文件夹中
TestWrapper();
/*HMODULE h = LoadLibrary(L"Wrapper.dll");
if(h)
{
typedef void (*PTest)();
PTest p = (PTest)GetProcAddress(h, "TestWrapper");
if(p)
{
p();
printf("\n-------------\nok!!!\n");
}
else
{
printf("\nGetProcAddress('TestWrapper') failed");
}
}
else
printf("\nLoad Wrapper.dll failed");*/
system("pause");
原文链接:https://blog.csdn.net/fuhanghang/article/details/122219588