克里特岛 crete 文明:关于 DLL 的显示加载问题??

来源:百度文库 编辑:高校问答 时间:2024/04/28 14:52:29
我写了一个 DLL 和一个调用它的程序,代码如下:
//TheDll.cpp
#include <windows.h>
__declspec(dllexport) int WINAPI MyAdd(int a,int b){
return (a+b);
}

BOOL WINAPI DllMain(HANDLE, DWORD, LPVOID){
return TRUE;
}

//TheApp.cpp
#include <windows.h>
#include <stdio.h>

__declspec(dllimport) int __stdcall MyAdd(int a,int b);
typedef int (WINAPI *FUNC)(int ,int);

int main(int argc,char **argv){

HMODULE hInst = LoadLibrary("TheDll.dll");
if(hInst == NULL){
printf("LoadLibrary Error.\n");
return 0;
}

FUNC pFunc = NULL;
pFunc = (FUNC)GetProcAddress(hInst,"MyAdd");
if (pFunc == NULL){
printf("GetProcAddress Error.\n");
}else{
int res = pFunc(4,5);
printf("Result is %d\n",res);
}

if(hInst != NULL)
FreeLibrary(hInst);

return 1;
}

运行 TheApp.exe 时提示 : GetProcAddress Error

请问这个问题怎么解决??

添加一个thedll.def文件。
里面写上:

LIBRARY "thedll"
DESCRIPTION 'thedll Windows Dynamic Link Library'
EXPORTS
MyAdd @1