VS2010 DLL库生成和使用
一、生成dll文件(VS2010 Win32 程序)
CreateDll.h
// 下列 ifdef 块是创建使从 DLL 导出更简单的
// 宏的标准方法。此 DLL 中的所有文件都是用命令行上定义的 CREATEDLL_EXPORTS
// 符号编译的。在使用此 DLL 的
// 任何其他项目上不应定义此符号。这样,源文件中包含此文件的任何其他项目都会将
// CREATEDLL_API 函数视为是从 DLL 导入的,而此 DLL 则将用此宏定义的
// 符号视为是被导出的。
#ifdef CREATEDLL_EXPORTS
#define CREATEDLL_API __declspec(dllexport)
#else
#define CREATEDLL_API __declspec(dllimport)
#endif
// 此类是从 CreateDll.dll 导出的
class CREATEDLL_API CCreateDll {
public:
CCreateDll(void);
// TODO: 在此添加您的方法。
};
extern CREATEDLL_API int nCreateDll;
CREATEDLL_API int fnCreateDll(void);
CREATEDLL_API int printMax(int& x, int& y);
CREATEDLL_API int printMax(int& x, int& y, int& z);
// CreateDll.cpp : 定义 DLL 应用程序的导出函数。
//
#include "stdafx.h"
#include "CreateDll.h"
#include <iostream>
using namespace std;
// 这是导出变量的一个示例
CREATEDLL_API int nCreateDll = 0;
// 这是导出函数的一个示例。
CREATEDLL_API int fnCreateDll(void)
{
cout<<"the default function"<<endl;
return 42;
}
CREATEDLL_API int printMax(int& x, int& y)
{
cout<<"among("<<x<<","<<y<<")the bigger is:"<<(x > y ? x : y)<<endl;
return 0;
}
CREATEDLL_API int printMax(int& x, int& y, int& z)
{
cout<<"among("<<x<<","<<y<<","<<z<<")the max is:"<<((x > y ? x : y)> z ? (x > y ? x : y) : z)<<endl;
return 0;
}
// 这是已导出类的构造函数。
// 有关类定义的信息,请参阅 CreateDll.h
CCreateDll::CCreateDll()
{
cout<<"the default class."<<endl;
return;
}
CreateDll.def
LIBRARY CreateDLL
EXPORTS
pMaxA2 = ?printMax@@YAXAAH0@Z
pMaxA3 = ?printMax@@YAXAAH00@Z
二、使用Dll文件(VS2010 Win32 程序):
// UseDll.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <Windows.h>
#include <iostream>
using namespace std;
typedef void(* FUNA)(int&, int&);
typedef void(* FUNB)(int&, int&, int&);
int _tmain(int argc, _TCHAR* argv[])
{
const char* dllName = "CreateDLL.dll";
/*const char* funName1 = "printMax";
const char* funName2 = "printMax"; */
/*const char* funName1 = "?printMax@@YAXAAH0@Z";
const char* funName2 = "?printMax@@YAXAAH00@Z"; */
const char* funName1 = "pMaxA2";
const char* funName2 = "pMaxA3";
int x(100), y(200), z(300);
HMODULE hDLL = LoadLibraryA(dllName);
if(hDLL != NULL)
{
//怎样获取类:
//怎样获取参数:
FUNA fp1 = FUNA(GetProcAddress(hDLL,funName1));
if(fp1 != NULL)
{
std::cout<<"Input 2 Numbers:";
std::cin>>x>>y;
fp1(x,y);
}
else
{
std::cout<<"Cannot Find Function "<<funName1<<std::endl;
}
FUNB fp2 = FUNB(GetProcAddress(hDLL,funName2));
if(fp2 != NULL)
{
std::cout<<"Input 3 Numbers:";
std::cin>>x>>y>>z;
fp2(x,y,z);
}
else
{
std::cout<<"Cannot Find Function "<<funName2<<std::endl;
}
FreeLibrary(hDLL);
}
else
{
std::cout<<"Cannot Find "<<dllName<<std::endl;
}
system("pause");
return 0;
}
二、生成DLL和在C#中使用:
1、在stdafx.h中加入以下内容:
#ifndef DLL_EXPORT
#define DLL_EXPORT 1
#endif
2、ExportDll.h文件:
#ifndef __EXPORTDLL_H__
#define __EXPORTDLL_H__
//#ifdef EXPORTDLL_EXPORTS
#ifdef DLL_EXPORT
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif
//////////////////////////////////////////////////////////////////////////
//函数定义
extern "C" DLL_API int InitObject();
extern "C" DLL_API int UninitObject();
extern "C" DLL_API int ReadXML();
extern "C" DLL_API int WriteXML();
#endif
3、ExportDll.cpp文件:
// ExportDll.cpp : 定义 DLL 应用程序的导出函数。
//
#include "stdafx.h"
#include "ExportDll.h"
#include <string>
#include <iostream>
using namespace std;
//////////////////////////////////////////////////////////////////////////
//声明类
class Object
{
private:
string xml;
public:
Object(const char* xmlName);
bool ReadXML();
bool WriteXML();
~Object();
};
//////////////////////////////////////////////////////////////////////////
//定义类
Object::Object(const char* xmlName)
{
xml = xmlName;
}
bool Object::ReadXML()
{
cout<<"Read"<<xml<<endl;
return true;
}
bool Object::WriteXML()
{
cout<<"Write"<<xml<<endl;
return true;
}
Object::~Object()
{
//xml = nullptr;
}
//////////////////////////////////////////////////////////////////////////
//定义全局变量
Object* obj = nullptr ;
//////////////////////////////////////////////////////////////////////////
//导出函数
extern Object* obj;
extern "C" DLL_API int InitObject()
{
if (obj != nullptr)
{
return -1;
}
const char* xml = "Dll.Xml";
obj = new Object(xml);
//delete obj;
if (nullptr == obj)
{
return -1;
}
return 0;
}
extern "C" DLL_API int ReadXML()
{
if (nullptr == obj)
{
return -1;
}
obj->ReadXML();
return 0;
}
extern "C" DLL_API int WriteXML()
{
if (nullptr == obj)
{
return -1;
}
obj->WriteXML();
return 0;
}
extern "C" DLL_API int UninitObject()
{
if (nullptr == obj)
{
return -1;
}
delete obj;
cout<<obj<<endl;
obj = nullptr;
return 0;
}
4、将相应的lib、dll文件拷贝到工作目录下:
5、 [DllImport("ExportDll.dll", CharSet = CharSet.Unicode, EntryPoint = "InitObject", CallingConvention = CallingConvention.Cdecl)]
public static extern int InitObject();
[DllImport("ExportDll.dll", CharSet = CharSet.Unicode, EntryPoint = "ReadXML", CallingConvention = CallingConvention.Cdecl)]
public static extern int ReadXML();
[DllImport("ExportDll.dll", CharSet = CharSet.Unicode, EntryPoint = "WriteXML", CallingConvention = CallingConvention.Cdecl)]
public static extern int WriteXML();
[DllImport("ExportDll.dll", CharSet = CharSet.Unicode, EntryPoint = "UninitObject", CallingConvention = CallingConvention.Cdecl)]
public static extern int UninitObject();
static void Main(string[] args)
{
int a = InitObject();
int b = ReadXML();
int c = WriteXML();
int d = UninitObject();
d = UninitObject();
d = InitObject();
d = InitObject();
d = UninitObject();
}
VS2010 DLL库生成和使用的更多相关文章
- dll库生成和使用
抄自http://www.cnblogs.com/fangyukuan/archive/2010/06/20/1761464.html 1. VS2010中新建Win32-Win32项目,输入名称Dl ...
- vs2010 dll生成,使用问题[good]
VS2010 动态库开发——第一章 演练:创建和使用动态链接库 (C++) 转载自[http://www.cnblogs.com/sdlypyzq/archive/2012/01/17/2324215 ...
- #Lua:Lua调用C++生成的DLL库
Lua调用C++生成的DLL库 本文参考了某大佬的博客,写得十分详细,推荐!!! 需求: 在之前的求解器中添加了Lua库,使得程序可以在Lua脚本中实现自定义函数功能,考虑到未来可能需要与第三方程序库 ...
- [转] lib和dll 区别,生成及使用方法
lib 和 dll 的区别.生成以及使用详解 [目录] lib dll介绍 生成动态库 调用动态库 生成静态库 调用静态库 首先介绍一下静态库(静态链接库).动态库(动态链接库)的概念,首先两者都是代 ...
- VS2010动态链接库的生成及调用(C++)
一.动态链接库的生成 首先利用VS2010新建一个空的工程或者win32工程 2.在工程中添加头文件和源文件 3.工程属性配置 3.1 可以在解决方案目录下新建以下几个文件夹 bin (用于存放Rel ...
- boost库生成文件命名和编译
生成文件命名规则:boost中有许多库,有的库需要编译.而有的库不需要编译,只需包含头文件就可以使用.编译生成的文件名字普遍较长,同一个库根据编译链接选项不同,又可以生成多个不同名字的文件.生成的文件 ...
- Python 调用 C# dll库最简方法
1.为什么要跨平台编程?双平台编程或多平台编程,只是为提供更好开发更兼容的解决方案的一种手段,编程时服务于产品和客户的,也是因地制宜. 先安装python所需的库clr ,我这里已经安装了,可以去对应 ...
- Unity中调用DLL库
DLL -- Dynamic Link Library(动态链接库文件),这里以Window平台为例. Unity支持的两种语言生成的DLL库(C++.C#),这里以C#为例,C++网上可以搜索很详细 ...
- Visual Studio 进行Excel相关开发,Microsoft.Office.Interop.Excel.dll库
1. Interop.Excel.dll 的查找 本文中将 Microsoft.Office.Interop.Excel.dll库简称为Interop.Excel.dll库 其实在使用Visual S ...
随机推荐
- DLL Injection and Hooking
DLL Injection and Hooking http://securityxploded.com/dll-injection-and-hooking.php Three Ways to Inj ...
- java_Collection_详细介绍
转自:http://blog.sina.com.cn/s/blog_3fb3625f0101aref.html 1.类集框架 java.util 包中包含了一些在 Java 2 中新增加的最令人兴奋的 ...
- Git分支模型
转自:http://www.cnblogs.com/byeyear/archive/2012/11/28/2793374.html 本文介绍一种使用Git进行源代码管理的分支模型,着重于如何使用Git ...
- Android 获得view的宽和高
转自:http://blog.csdn.net/yangdeli888/article/details/25405263 在oncreate()中利用view.getWidth()或是view.get ...
- vim/vi的文件内、跨文件复制粘贴操作、替换操作
vi/vim 中可以使用 :s 命令来替换字符串 1.s/vivian/sky/ 替换当前行第一个 vivian 为 sky 2.:s/vivian/sky/g 替换当前行所有 vivian 为 sk ...
- java学习笔记7--抽象类与抽象方法
接着前面的学习: java学习笔记6--类的继承.Object类 java学习笔记5--类的方法 java学习笔记4--类与对象的基本概念(2) java学习笔记3--类与对象的基本概念(1) jav ...
- Android程序的反编译对抗研究
转自: http://www.freebuf.com/tools/76884.html 一.前言 对抗反编译是指让apk文件或者dex文件无法正常通过反编译工具,而且有可能导致工具异常或者崩溃,如ap ...
- Open DJ备份与恢复方案
最近接手了一个Cognos项目,第三方用户认证采用的是和Open DJ集成.本人之前很多采用的是cjap ,当然这和cjap相比起来简单的多了,最起码你不必具有Java的基础知识就可以完全驾驭了! 一 ...
- 【cocos2d-x 3.7 飞机大战】 决战南海I (七) 控制器的实现
控制器中的功能并不多,主要是以下这些 //对玩家分数的操作 CC_SYNTHESIZE_READONLY(SaveData *, m_saveData, SaveData); void update( ...
- [AngularJS] $scope.$warchCollection
For the $watch in last article, we watch 'user.password', actually it is a string. If you watch 'use ...