测试下dll的编写和调用

来源:互联网 发布:js中的大于等于 编辑:程序博客网 时间:2024/06/11 22:25

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;
}
///其他都不用设置,




0 0