一、生成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.hCCreateDll::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#endif2、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();#endif3、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(); }