新建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;
}