Calling C++ Unmanaged Class from C#
来源:互联网 发布:淘宝的ppc是什么意思 编辑:程序博客网 时间:2024/05/14 14:35
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
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
- Calling C++ Unmanaged Class from C#
- Calling C++ Unmanaged Class from C#
- Calling Managed .NET C# COM Objects from Unmanaged C++ Code
- Calling C/C++ functions from C#
- Call Unmanaged DLLs from C#
- Calling Java Class Methods from C with JNI
- Calling Ruby From C
- Calling Java program from C#
- Calling C# Methods From C++
- Consuming Unmanaged C++ Class Libraries from .NET Clients
- Consuming Unmanaged C++ Class Libraries from .NET Clients
- Consuming Unmanaged C++ Class Libraries from .NET Clients
- Consuming Unmanaged C++ Class Libraries from .NET Clients
- Calling C Programs from IDL
- Calling a Java Class from PowerBuilder 10
- Calling C From Java Is Easy
- chapter 27 Calling C from Lua
- calling lua from c in linux
- 如何准备微软的面试?
- 乱码算法大全
- 日历单元
- 在wince中开机启动自己的应用程序
- DELPHI程序编码规范
- Calling C++ Unmanaged Class from C#
- [cp] 词法分析器的自动生成
- 青海
- Consuming Unmanaged C++ Class Libraries from .NET Clients
- [cp] 正规式到自动机的转换
- 稳挂csdn周榜一周,纪念~~
- "Programming Microsoft DirectShow for Digital Video and Television"中简介的翻译
- 什么是ASP.NET?
- jazz