[CLI/C++].NET託管代碼和非託管代碼的相互調用

場景三:現有C++原代碼,包裝後供C#調用。

C++的原代碼,實際上能夠直接編譯成託管代碼。MFC也好ATL也好……這樣看起來在.NET中最強大的編程語言就是C++了:它不只能夠編寫託管 程序,甚至能夠將標準C++的代碼也編譯成託管程序!其實VC++最強大的地方不止如此,它還在於可以編寫混合了託管和非託管的代碼的程序!!!這樣最大 的好處不只能夠將關鍵代碼直接編譯成非託管的代碼,還能夠避免被反編譯。

假設現有C++代碼以下:ios

class UnmanagedClass {
public:
     LPCWSTR GetPropertyA()
{ return L"Hello!"; }
    
void MethodB( LPCWSTR ) {}
}
;



咱們只要再增長一個包裝類到工程文件中: c++

namespace wrapper
{
    
public ref class ManagedClass {
    
public:
        
// Allocate the native object on the C++ Heap via a constructor
         ManagedClass() : m_Impl( new UnmanagedClass ) {}

        
// Deallocate the native object on a destructor
        ~ManagedClass() {
             delete m_Impl;
         }


    
protected:
        
// Deallocate the native object on the finalizer just in case no destructor is called
        !ManagedClass() {
             delete m_Impl;
         }


    
public:
         property String
^   get_PropertyA {
             String
^ get() {
                
return gcnew String( m_Impl->GetPropertyA());
             }

         }


        
void MethodB( String ^ theString ) {
             pin_ptr
<const WCHAR> str = PtrToStringChars(theString);
             m_Impl
->MethodB(str);
         }


    
private:
         UnmanagedClass
* m_Impl;
     }
;
}


而後,改變編譯選項爲「使用公共語言擴展 /clr」就能夠了。這樣,咱們把代碼編譯成DLL文件就能夠供.NET其它語言調用了。
最後,C#中能夠象以下的代碼同樣調用C++類了:編程

ManagedClass mc = new ManagedClass();
mc.MethoB(
" Hello " );
string s = mc.get_PropertyA;

 

場景四:如何在託管C++代碼中混合託管和 非託管代碼

很簡單,只要從#pragma unmanaged編譯指示開始的程序,一率編譯成非託管代碼;要想恢復成託管代碼,只要使用#pragma managed就能夠了。如:app

#pragma unmanaged

#include
< iostream >
using namespace std;

template
< typename T >
void f(T t) {
     cout
<< t << endl;
}


#pragma managed

using namespace System;

void m(String ^ s) {
     Console::WriteLine(s);
}


void main() {
     f(
"Hello");
     m(
"World");
}


生成exe文件後,用反編譯程序查看 f 函數:編程語言

 

[PreserveSig, MethodImpl(MethodImplOptions.Unmanaged, MethodCodeType = MethodCodeType.Native), SuppressUnmanagedCodeSecurity]
public static unsafe void modopt(CallConvCdecl) f < char const *> ( sbyte modopt(IsSignUnspecifiedByte) modopt(IsConst) * );

看不到源碼,而方法屬性標記爲Unmanaged。
若是沒有加上#pragma unmanaged,反編譯獲得的 f 函數爲:
internal static unsafe void modopt(CallConvCdecl) f < char const *> ( sbyte modopt(IsSignUnspecifiedByte) modopt(IsConst) * t)
{
       std.basic_ostream
<char,std::char_traits<char> >.<<(std.operator<<<struct std::char_traits<char> >(*((basic_ostream<char,std::char_traits<char> >* modopt(IsImplicitlyDereferenced)*) &__imp_std.cout), t), (basic_ostream<char,std::char_traits<char> >* modopt(IsImplicitlyDereferenced) modopt(CallConvCdecl) *(basic_ostream<char,std::char_traits<char> >* modopt(IsImplicitlyDereferenced))) __unep@?endl@std@@$$FYAAAV?$basic_ostream@DU?$char_traits@D@std@@@1@AAV21@@Z);
}
其中的函數內容一目瞭然。若是你的函數沒有調用operator等很差理解的類庫,那麼反編譯出來的代碼簡直和源碼沒差異。 開心一刻:我只會C++不懂.NET不懂C#,怎麼編寫.NET程序? 很簡單,你照樣用你的C++寫你的程序,而後測試沒有錯誤後,將編譯選項改成/clr,好了,Rebuild,你的程序如今是.NET了。 惡搞:「我想問一下,在能將現有的C++代碼直接進行封裝,被C#進行調用,而不是去調用DLL,也就是不生成DLL,就在C#下能直接調用VC的工 程源文件不?」 我想,提問的人是否是指,現有c++源碼,但不想費勁去轉換成C#源碼,但又想能與C#一塊兒編譯。 因而我就給了一個極其變態的方法,不過,我的是不建議使用這種變態的方法啊。方法以下: 1 先將C++源碼,改用CLR編譯選項,編譯成.NET的Assembly(DLL文件)。 2 而後用reflector等反編譯軟件,反編譯成C#代碼,並導出(reflector有專門的導出插件)。 3 將導出的C#代碼,添加上新寫的C#代碼一塊兒編譯。 這種方法生成的代碼非常恐怖,強烈建議不要把C++源碼就這麼丟了,不然後果自負。
相關文章
相關標籤/搜索