1.创建dll的工程为dlltest
//dlltest.h
#ifdef DLLTEST_EXPORTS
#define DLLTEST_API __declspec(dllexport)
#else
#define DLLTEST_API __declspec(dllimport)
#endif
//dlltest.cpp
#include "stdafx.h"
#include"dllTest.h"
#ifdef _MANAGED
#pragma managed(push, off)
#endif
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
#ifdef _MANAGED
#pragma managed(pop)
#endif
//add_Fun.cpp
#include "stdafx.h"
#include"dlltest.h"
DLLTEST_API int add_Fun( int *a,
int *b)
{
return (*a+*b);
}
用viewdll2.0测试工具可以看到dll要导出的函数名
2.工程testdll,调用生成的dlltest.dll
先把dlltest.dll放在工程testdll的子目录debug下
//testdll.cpp
#include "stdafx.h"
#include<windows.h>
typedef int (*AddFun)(int *a,int *b);
int _tmain(int argc, _TCHAR* argv[])
{
HINSTANCE hDLL;
hDLL=LoadLibrary(_T("dlltest.dll"));//单步调试,hDLL不是空指针
if(hDLL!=NULL){
AddFun addfun=(AddFun)GetProcAddress(hDLL,"add_Fun"); //单步调试,addfun为空指针???
int a=3;
int b=4;
int c;
if(addfun!=NULL){
c=addfun(&a,&b);
printf("%d",c);
}
}
FreeLibrary(hDLL);//卸载MyDll.dll文件;
return 0;
} |