前言:為了介紹C#寫界面,C++寫算法的快捷開發方式,C#與C++的交互,首先介紹c++,C#內部的DLL,COM調用。
一, 靜態的Lib:靜態的lib經過編譯后只有.h和.lib文件,沒有dll,因為實現部分也包含在lib中,這就是與動態dll的區別。還有在寫靜態lib的時候不需要在使用導出關鍵字_declspec(dllexport)。一般有2中方法調用靜態lib,如下實例:
靜態lib:CPPLib->test.h
#pragma once

class CTest
{
public:
CTest(void);
public:
~CTest(void);
public:
int Add(int x, int y);
int Square(int x);
};

//函數的實現必須寫在.cpp文件中,否則編譯有錯,說重復定義
int Max(int x, int y);
靜態lib的實現文件: CPPLib->test.cpp
#include "StdAfx.h"
#include "Test.h"

CTest::CTest(void)
{
}

CTest::~CTest(void)
{
}

int CTest::Add(int x, int y)
{
return x+y;
}
int CTest::Square(int x)
{
return x*x;
}

int Max(int x, int y)
{
return x;
}
client調用CPPLibClient->CPPibClient.cpp
#include "stdafx.h"
//#include "test.h"
//#include <windows.h>
//#include <string>
//#include <assert.h>


//#include "../CppLib/test.h"
//#pragma comment(lib,"../debug/CppLib.lib")
//#pragma 使用法


//#include "test.h"
//修改編譯選項調用靜態庫
//需要修改:include的path,lib的path,和加入lib的名字,如下:
//C++->General->additional include directories
//Linker->General->additional library directories
//linker->input->additional dependencies

//不能動態加載靜態的lib庫
//HMODULE m_handle = LoadLibrary(L"../debug/CppLib.Lib");
//GetProcAddress(m_handle, "Max");
//FreeLibrary(m_handle);

int _tmain(int argc, _TCHAR* argv[])
{
CTest test;
int a = test.Add(10,20);
printf("the result is :%d\n",a);
a = Max(10,20);
printf("the result is :%d\n",a);
return 0;
}

調用方法:可以看出對于靜態的只可以使用修改編譯選項和pragma comment()來調用,不能使用loadlibrary()來調用。
二 ,動態DLL:在動態dll中,可以導出變量,函數和整個類。編譯后有.h,.lib和dll文件,真正的實現包含在dll中,所以在client調用動態dll的時候,必須要使用dll,最后和client的放在同意目錄下。要導出必須使用導出關鍵字_declspec(dllexport)。有時還使用extern “C”,為了使導出能夠與C兼容,一般我們都加extern “c”。
一般調用有3中方法,實例如下:
實例1:演示了導出變量和函數,和前2中調用方法,修改編譯選項和pragma comment().
動態dll:CPPdll->test.h
#pragma once


extern "C" _declspec(dllexport) int nCppDll;
extern "C" _declspec(dllexport) int fnCppDll(void);

extern "C" _declspec(dllexport) int Max(int a, int b);
extern "C" _declspec(dllexport) int Min(int a, int b);
動態dll的實現:CPPDLL->test.cpp
#include "StdAfx.h"
#include "Test.h"


// This is an example of an exported variable
int nCppDll=100;

// This is an example of an exported function.
int fnCppDll(void)
{
return 42;
}

int Max(int a, int b)
{
if(a>=b)return a;
else
return b;
}
int Min(int a, int b)
{
if(a>=b)return b;
else
return a;
}
client的調用:cppclient->cppclient.cpp
#include "stdafx.h"

#pragma comment(lib, "../debug/CppDll.lib")
extern "C" int Max(int a,int b);//_declspec(dllimport)
extern "C" int Min(int a,int b); //_declspec(dllimport)
extern "C" _declspec(dllimport) int nCppDll;
extern "C" int fnCppDll(void);

//#include "test.h"
//修改編譯選項調用靜態庫
//需要修改:include的path,lib的path,和加入lib的名字,如下:
//C++->General->additional include directories
//Linker->General->additional library directories
//linker->input->additional dependencies

int _tmain(int argc, _TCHAR* argv[])
{
int a;
a =Min(8,10);
printf("比較的結果為 %d\n",a);
a= Max(8,10);
printf("比較的結果為%d\n",a);
printf("導出的變量:%d\n",nCppDll);

a = fnCppDll();
printf("fnCppDll的結果:%d\n",a);

return 0;
}
上面演示了對一般變量和函數的導出的調用方法中的其中的2中,修改編譯選項和pragma comment(),當使用pragma comment()的使用,應當注意:
使用#pragma隱式加載動態庫
對于變量,必須申明且不能include頭文件。extern "C" _declspec(dllimport) int nCppDll;
對于函數,或include頭文件,或是申明。extern "C" int fnCppDll(void);
對于類,最好使用函數封裝導出指針供使用。
參考:http://www.shnenglu.com/mzty/archive/2006/07/24/10419.html
實例2:演示類的導出和使用動態加載來調用。
動態dll的類導出:CPPDll2->test.h
#pragma once
//#include "boost/shared_ptr.hpp"

class Test
{
public:
virtual ~Test() {}
virtual void DoIt() =0;
};

//extern "C" _declspec(dllexport) std::auto_ptr<Test> CreateTest();
//extern "C" _declspec(dllexport) boost::shared_ptr<Test> CreateTest();
extern "C" _declspec(dllexport) Test* CreateTestPtr();

extern "C" _declspec(dllexport) void DeleteTestPtr(Test*);
動態dll的類導出的實現:CPPDll2->test.cpp
//test.cpp
#include "stdafx.h"
#include "Test.h"
#include <stdio.h>
//#include <memory>
//#include "boost/shared_ptr.hpp"


class CTest : public Test
{
public:
virtual void DoIt()
{ printf("Should do something\n"); }
};

//std::auto_ptr<Test> CreateTest()
//{
// return std::auto_ptr<Test>(new CTest);
//}


//boost::shared_ptr<Test> CreateTest()
//{
// return boost::shared_ptr<Test>(new CTest);
//}

Test* CreateTestPtr()
{
return new CTest();
}

void DeleteTestPtr(Test* t)
{
if(t != NULL)
{
delete t;
t = NULL;
}

}
對loadlibrary的分裝,可以作為tools:
//library.h
#pragma once
#include <windows.h>
#include <string>
#include <assert.h>

class Library
{
public:

explicit Library(const wchar_t* name)
{
m_handle = LoadLibrary(name);
assert(m_handle);
if (!m_handle)
throw std::runtime_error(std::string("Could not find library file:"));
}

~Library()
{
FreeLibrary(m_handle);
}

void* GetProc(const char* name)
{
void* proc = ::GetProcAddress(m_handle, name);
assert(proc);
return proc;
}

private:
HMODULE m_handle;
};
client的調用:

#include "stdafx.h"
#include "library.h"
#include "../CppDll2/test.h"

int _tmain(int argc, _TCHAR* argv[])
{
typedef Test* (*CREATE)();
typedef void (*DEL)(Test*);

Library lib(L"CppDll2.dll");
//std::auto_ptr<Test> test = ((std::auto_ptr<Test> ) lib.GetProc("CreateTest"));
Test* test = (((CREATE)(lib.GetProc("CreateTestPtr")))());
test->DoIt();
((DEL)(lib.GetProc("DeleteTestPtr")))(test);
return 0;
}
上面的是對類的動態調用,注意需要include頭文件哦!
//通過API動態加載動態庫
//對于類的導出,最好使用函數封裝,導出類的指針。
//動態加載dll, 如果導出的函數只使用 extern,而沒有使用extern "C" 則GetProcAdress會找不到函數的指針,要想找到可以使用真正要找的函數原型哦,可能是函數名后加@@。。。,也可以使用編號來找到需要的函數地址。
//但是如果導出函數使用extern "C"的話,導出函數的返回值不能是auto_ptr<>或shared_ptr<>哦,但是我們仍然可以使用智能指針哦,采用的方法是不使用return返回,使用函數的參數返回哦。//使用智能指針導出的更好的實現,請參考 http://www.shnenglu.com/eXile
//參考: http://www.shnenglu.com/eXile/archive/2007/04/19/22262.html
//更多dll類型:http://www.vckbase.com/document/viewdoc/?id=1116
//調用約定:http://blog.chinaunix.net/u/21790/showart_265932.html
三 資源DLL
在C++中,我們可以建立純資源的動態dll,比如說我們建立了一個動態的資源dll,里面增加一個string: id為IDS_APPLICATION,值為:aaa,
則我們可以在client動態調用如下:
#include "stdafx.h"
#include <windows.h>
#include "../ResDll/resource.h"

int _tmain(int argc, _TCHAR* argv[])
{
HMODULE hModule=LoadLibrary(L"../debug/ResDll.dll");
if( NULL != hModule)
{
TCHAR szName[200];
::LoadString(hModule,IDS_APPLICATION,szName,200);
FreeLibrary(hModule);
}
return 0;
}
資源還可以是其他的比如是icon,bitmap。。。。等,有對應的load。()函數去調用。
四,總結
熟悉dll調用的3中方法,其中對靜態的調用只有2中方法,一般對類的導出調用要使用函數封裝哦!:~
代碼下載:http://www.shnenglu.com/Files/mzty/DLLTest.rar