新建Win32工程,选择类型为DLL,新建空项目。。。
头文件写法(关键):
// jiarenyf.h
extern "C" {
__declspec(dllexport) void Method(int jiarenyf);
}
- 源文件写法:
// jiarenyf.cpp
#include "jiarenyf.h"
void Method(int jiarenyf)
{
// 随便写...
}
- 编译即可生成DLL
- 调用DLL
#include "windows.h "
#include <iostream>
#include <string>
using namespace std;
int main()
{
HINSTANCE hInstance = LoadLibrary(TEXT("*.dll"));
if (!hInstance)
{
cout << "DLL加载失败..." << endl;
FreeLibrary(hInstance);
system("PAUSE");
return -1;
}
typedef void* (*Method) (int jiarenyf);
Method method = (Method)GetProcAddress(hInstance, "Method");
if (!method)
{
cout << "函数Method加载失败..." << endl;
system("PAUSE");
FreeLibrary(hInstance);
return -1;
}
else
{
//调用函数
method(123);
}
system("PAUSE");
//FreeLibrary(hInstance);
return 0;
}