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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值