方法1、使用头文件导出:
dlltest.h
#ifdef MYLIBAPI
#else
#define MYLIBAPI extern "C" _declspec(dllexport)
#endif
MYLIBAPI int add(int a,int b);
dlltest.cpp
#include <windows.h>
#include "dlltest.h"//必须包含头文件
int add(int a,int b)
{
return a+b;
}
方法2、生成.def文件
//dlltest4.def
LIBRARY "dlltest4"
EXPORTS
add @2
//dlltest4.cpp
#include <windows.h>
int add(int a,int b)
{
return a+b;
}
相比之下,第二种方法更简单
DLL函数调用
#include <iostream>
#include <windows.h>
#include "stdafx.h"
using namespace std;
typedef int (*add)(int,int);
int _tmain(int argc, _TCHAR* argv[])
{
HINSTANCE handle=LoadLibrary(L"dlltest4.dll");
if(!handle)
{
cout<<"load dll fail"<<endl;
}
add addfun=(add)GetProcAddress(handle,"add");
if(addfun)
{
int c =addfun(1,2);
cout<<c<<endl;
}
return 0;
}