Calling C++ Unmanaged Class from C#

This may be a no-brainer, but I thought I'd post it anyway.

If you have a C++ class in an unmanaged dll what is the best way to call it from C#?

1. You need to wrap the C++ class in a COM object or expose the class through dll exports. Managed code can call methods on COM object through COM Interop. And it can call dll exports through P/Invoke.

2. There is another way to do this using P/Invoke. Here is the sample code from the SDK.
The best way to do this is to write a thin wrapper in MC++ around your MFC classes and expose managed interfaces from it to your C# code. If you can use Whidbey, you could even compile your MC++ wrapper and rest of the C# code in same DLL using new linker.
   
// ClassMethods.cs
using System;
using System.Text;
using System.Runtime.InteropServices;
public class LibWrap
{
    /*
    class PINVOKELIB_API CTestClass
    {
        public:
            CTestClass( void );
            int DoSomething( int i );
        private:
            int m_member;
    };
    */
    [ DllImport( "..//LIB//PinvokeLib.dll",
    EntryPoint="?DoSomething@CTestClass@@QAEHH@Z",
    CallingConvention=CallingConvention.ThisCall )]
    public static extern int TestThisCalling( IntPtr ths, int i );
    // CTestClass* CreateTestClass();
    [DllImport( "..//LIB//PinvokeLib.dll" )]
    public static extern IntPtr CreateTestClass();
    // void DeleteTestClass( CTestClass* instance )
    [ DllImport( "..//LIB//PinvokeLib.dll" )]
    public static extern void DeleteTestClass( IntPtr instance );
}
public class App
{
    public static void Main()
    {
        IntPtr instancePtr = LibWrap.CreateTestClass();
        int res = LibWrap.TestThisCalling( instancePtr, 9 );
        Console.WriteLine( "/nResult: {0} /n", res );
        LibWrap.DeleteTestClass( instancePtr );
    }
}


原文参见:http://blogs.msdn.com/sanpil/archive/2004/07/07/175855.aspx

你可能感兴趣的:(C++,object,C#,Class,dll,wrapper)