Calling C++ Unmanaged Class from C#

原创 2005年04月30日 11:36:00
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
            CTestClass( void );
            int DoSomething( int i );
            int m_member;
    [ DllImport( "..//LIB//PinvokeLib.dll",
    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 );


Calling C++ Unmanaged Class from C#

 This may be a no-brainer, but I thought Id post it anyway.If you have a C++ class in an unmanaged d...
  • i_mimi
  • i_mimi
  • 2008年07月02日 16:29
  • 445

Calling C/C++ functions from C#

Dot net framework is profound for coding, features the integrated API with service. However, some pe...
  • u013606170
  • u013606170
  • 2016年04月29日 15:11
  • 706

managed code和unmanaged code混合debug

首先: 1. managed code的项目属性中debug页里,"Enable unmanaged code debugging"选上。 2. unmanaged code项目属性的de...
  • chuwachen
  • chuwachen
  • 2011年09月20日 13:21
  • 2753

Calling Managed .NET C# COM Objects from Unmanaged C++ Code

  C#创建、使用组件都非常简单,但像我这样的偏执狂自然想用C#创建一个传统的com server,终于找到了一篇文章,创建是可以不过比较麻烦,专用名词叫Calling Managed .NET C#...
  • win2ks
  • win2ks
  • 2008年06月17日 22:16
  • 889

Calling C# Methods From C++

In the previous article, I have demonstrated how to call C/C++ libraries from C#. In this post, I w...
  • u013606170
  • u013606170
  • 2016年05月09日 22:59
  • 676

Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK

今天在项目中碰到的一个错误: 项目中涉及到很多Activity之间相互跳转的代码,如下所示的代码,感觉很繁琐,因此想到了封装一下: Intent intent = new Intent(Splas...
  • watermusicyes
  • watermusicyes
  • 2015年04月09日 18:54
  • 2269

Interop with PROPVARIANTs in .NET -- C# 调用C++ COM 系列笔记

  • u011569793
  • u011569793
  • 2016年03月16日 19:51
  • 483

关于运行时异常:Calling startActivity() from outside of an Activity

当我们在Adapter或者其他地方(如Service、BroadCastReceiver、Widget等)中使用startActivity(),会报一个异常:                     ...
  • u011150924
  • u011150924
  • 2016年09月19日 15:05
  • 678

异常Calling startActivity() from outside of an Activity的解决方法

android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity Caused by...
  • u013224148
  • u013224148
  • 2014年11月08日 16:00
  • 2213

managed, unmanaged (托管,非托管)

•概述:         托管代码是可由公共语言运行库检查和管理的代码。托管代码可以访问托管对象。使用 /clr 编译的应用程序可能包含托管数据,也可能不包含托管数据。 •注释:      ...
  • shejiannan
  • shejiannan
  • 2013年06月17日 11:30
  • 1371
您举报文章:Calling C++ Unmanaged Class from C#