测试下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;
}
///其他都不用设置,
#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;
}
///其他都不用设置,
相关文章推荐
- 创业的第一百六十二天
- 栈溢出笔记1.2 覆盖EIP
- 栈的压入、弹出序列
- 第6周作业 邱鹏 2013551628
- SODBASE实时大数据基础(一):实时同步Mysql数据库到Kafka
- R遗漏
- 第六周作业
- nefu 117 素数定理
- IOS逆向学习之路之iPod touch4 越狱心得
- 图片轮播学习记录
- EXC_BAD_ACCESS
- 自己动手搭建DNS服务器 推荐
- 梦想中的图文发布——UEditor使用
- C++ 切割string splite方法的实现
- Spinner VS popwindow
- 实习总结系列3
- Linux虚拟机配置本地yum源
- 重构第17天提取父类(Extract SuperClass)
- [置顶] Android开发,java开发程序员常见面试题,求100-200之间的质数,java逻辑代码
- 2006 Asia - Kaohsiung Perfect Service 树形DP (uvaLive3685) ★