1、dll
#pragma once
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
__declspec(dllexport) int add(int a, int b);
#ifdef __cplusplus
}
#endif // __cplusplus
#include <iostream>
#include "DllTest.h"
int add(int a, int b)
{
return a + b;
}
2 、cpp 用dll 动态加载方法
#include<iostream>
#include <Windows.h>
#include "include\DllTest.h"
using namespace std;
typedef int(*ADDPROC)(int a, int b);
int main(int argc, char **argv)
{
HINSTANCE hInst = (HINSTANCE)LoadLibrary("ConsoleApplication1.dll");
if (hInst == NULL)
{
cout << "LoadLibrary error!" << endl;
return -1;
}
ADDPROC Add = (ADDPROC)GetProcAddress(hInst, "add");
if (NULL == Add)
{
cout << "GetProcAddress error!" << endl;
system("pause");
return -1;
}
cout << Add(20, 7) << endl;
FreeLibrary(hInst);
system("pause");
return 0;
}
///其他都不用设置,