1、生成dll文件(VS2010 Win32 程序)ios
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);
spa
// 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.defcode
LIBRARY CreateDLL
EXPORTS
pMaxA2 = ?printMax@@YAXAAH0@Z
pMaxA3 = ?printMax@@YAXAAH00@Z
xml
2、使用Dll文件(VS2010 Win32 程序):ci
// 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;
}
string
2、生成DLL和在C#中使用:it
一、在stdafx.h中加入如下內容:io
#ifndef DLL_EXPORT
#define DLL_EXPORT 1
#endif
二、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
三、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;
}
四、將相應的lib、dll文件拷貝到工做目錄下:
五、 [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(); }