您的位置:首页 > 其它

测试下dll的编写和调用

2016-04-09 23:15 316 查看
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;

}

///其他都不用设置,
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: