一 引子
1)使用VS2005,Native C++,C#,CLI/C++。
2)主要介紹CLI/C++和Native C++, C#的交互,最總能夠實現以CLI/C++為中介的Native C++與C#的互調。
二 實例
1)實現一個CLI/C++的Dll,在此Dll中包含一個RefClass能夠被C#的exe調用。(實現過程:在VS中建立C++ CLR Class library 的一個Project,然后增加一個類RefClass)代碼如下:
refclass.h文件:
#pragma once

namespace ClrCppDll


{
public ref class RefClass

{
public:

RefClass()
{}
RefClass(int _x, int _y) : x(_x), y(_y)

{
}
void PrintSelf();
private:
int x;
int y;
};
}
refclass.cpp文件:
#include "stdafx.h"
#include "refclass.h"

using namespace System;
using namespace ClrCppDll;

void RefClass::PrintSelf()


{
System::Console::WriteLine("hello, i am a ref cpp class");
System::Console::WriteLine("x is {0}",x);
System::Console::WriteLine("y is {0}",y);
}
2)能夠調用上面第1)步中的CLI/C++的Dll中class的C#的exe。(實現過程:在VS建立C#的console Application,然后reference 前面的1)生成的Dll)代碼如下:
Program.cs文件:
using System;
using System.Collections.Generic;
using System.Text;

namespace CsharpTest


{
class Program

{
static void Main(string[] args)

{
ClrCppDll.RefClass refClass = new ClrCppDll.RefClass(2,4);
refClass.PrintSelf();
}
}
}
上面2步的代碼下載:
http://www.shnenglu.com/Files/mzty/CSharpCPPCLI1.rar (實現CLI/C++的dll能夠被C#調用)
3)對1)實現的CLI/C++的Dll中增加能夠被Native C++調用的NativeClass。代碼如下:
NativeClass.h文件:
#pragma once

namespace ClrCppDll


{
public class NativeClass

{
public:

NativeClass()
{}
NativeClass(int _x, int _y) : x(_x), y(_y)

{}

void printSelf();

private:
int x;
int y;
};
}
NativeClass.cpp文件:
#include "stdafx.h"
#include <iostream>
#include "nativeclass.h"

using namespace ClrCppDll;

void NativeClass::printSelf()


{
std::cout<<"hello,i am a native cpp class!"<<std::endl;
std::cout<<"x is "<<x<<std::endl;
std::cout<<"y is "<<y<<std::endl;
}
問題: 如果我們直接在NativeC++的exe調用上面CLI/C++中的NativeClass,會有問題,Error 1 error C3381: 'CppClrDll::NativeClass' : assembly access specifiers are only available in code compiled with a /clr option d:\cppandcsharpinteractivetest\csharpcppcli\clrcppdll\nativeclass.h 8,這是為什么那,我們想想我們一般的Native C++的DLL的調用,都要將要被調用的Class或funtion導出才可以調用,想到這里我們也對我們的NativeClass進行導出。(怎么導出可以看下面的代碼)但是當增加了對NativeClass的導出,調用的時候仍然有上面的error,Error 1 error C3381: 'CppClrDll::NativeClass' : assembly access specifiers are only available in code compiled with a /clr option d:\cppandcsharpinteractivetest\csharpcppcli\clrcppdll\nativeclass.h 9,最后只有查找資料(沒有找到)問別人(沒有問到),最后想到使用導出函數試試,一試果然可行啊,哈哈哈,so Happy!
其實后來發現是我加的導出__declspec(dllexport)的位置不對,應該是在class關鍵字后面,總之導出類也是可以的。導出類的代碼可以下載新的sample,增加導出和導出的函數代碼如下:
(導出不久需要下面的2文件,還的定義宏 CLR_CPP_DLL在編譯選項中,或直接寫在文件中)
clrcppdll.h
// ClrCppDll.h

#pragma once

#ifdef CLR_CPP_DLL
#define DLLIMPEXP __declspec(dllexport)
#else
#define DLLIMPEXP
#endif

namespace ClrCppDll


{
DLLIMPEXP void CallNativeClassPrintSelf();
}
clrcppdll.cpp文件:
// This is the main DLL file.

#include "stdafx.h"
#include "NativeClass.h"
#include "ClrCppDll.h"

DLLIMPEXP void ClrCppDll::CallNativeClassPrintSelf()


{
ClrCppDll::NativeClass test(10,20);
test.printSelf();
}
4)建立一個NativeC++的exe來調用1)生成的CLI/C++的Dll中的3)增加的NativeClass(實現過程:建立一個Native C++的console application)代碼如下:
cpptest.cpp文件:(經過上一步問題的反復折磨,終于搞定)(同時別忘了要在編譯選項中指定lib和lib的路徑)
// CppTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "../ClrCppDll/clrcppdll.h"

int _tmain(int argc, _TCHAR* argv[])


{

/**//*ClrCppDll::NativeClass test(5,6);
test.PrintSelf();*/

ClrCppDll::CallNativeClassPrintSelf();
return 0;
}
上面四步總代碼下載:
http://www.shnenglu.com/Files/mzty/CSharpCPPCLI2.rar (實現CLI/C++的dll同時被Native C++和C#的調用)
5)擴展,增加在NativeClass中調用RefClass的funtion, 和RefClass中調用NativeClass的function。
代碼下載:
http://www.shnenglu.com/Files/mzty/CSharpCPPCLI3.rar (最后代碼下載,包含NativeClass與RefClass的互調)
http://www.shnenglu.com/Files/mzty/CSharpCPPCLI32.zip (包含native class的導出也調用)
三 總結
1) CLI/C++中如果想導出讓NativeC++使用,則最好導出函數,也可以導出類。
2) CLI/C++的DLL可以沒有像一般的NativeC++的Dll中的DllMain()函數。
3) CLI/C++如果想導出讓C#使用,則一般是ref class。