[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++源码就这么丢了,不然后果自负。