Dll导出函数的使用

来源:百度知道 编辑:UC知道 时间:2024/06/01 04:57:36
自己写了一个小程序,但是发现有问题
//Dll.cpp
#include <stdio.h>

void report()
{
printf("Hello from Dll call\n");
}
//Dll.def
LIBRARY Dll

EXPORTS
report @ 1
编译生成的Dll.dll和Dll.lib复制到了应用程序中
//TestDll.cpp
#pragma comment(lib,"Dll.lib")

extern "C" _declspec(dllimport) report();

int main()
{
report();
return 0;
}
改成动态调用就OK
#pragma comment(lib,"Dll.lib")

#define DYNAMIC_CALL

#ifdef DYNAMIC_CALL
#include <windows.h>
#else
extern "C" _declspec(dllimport) report();
#endif

typedef (*lpDll)();

int main()
{
#ifdef DYNAMIC_CALL
HINSTANCE hDll;
hDll = LoadLibrary("E:\\TestDll\\Dll.dll");
if(hDll)
{
lpDll temp;
temp = (lpDll)GetProcAddress(hDll,"report");

#include <stdio.h>

extern "C" void report() //命名约定要一值
{
printf("Hello from Dll call\n");
}
//Dl

extern "C" _declspec(dllimport) report(); 这个声明成 void report();试试