Calling C++ Unmanaged Class from C#
2005-04-30 11:36
676 查看
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 Managed .NET C# COM Objects from Unmanaged C++ Code
- Problem in calling C++ dll from C# Code
- Consuming Unmanaged C++ Class Libraries from .NET Clients
- Calling C# Methods From C++
- Consuming Unmanaged C++ Class Libraries from .NET Clients
- Inheriting From a Native C++ Class in C#
- Calling C/C++ functions from C#
- Calling C++ code from C# z
- Consuming Unmanaged C++ Class Libraries from .NET Clients
- <From C++ To C#> 更强大的值类型
- <From C++ To C#>显示一下吧
- Calling C and C++ from IDL (二)——变量传递
- Call C# Code from C++ and read an array of struct which contains strings
- Calling Lua function from C++
- Call C# code from C++
- Calling Fortran function and subroutines from a C or C++ function
- Linker error calling C-Function from Objective-C++
- C# p/invoke: marshaling class between C# and C++ class
- struct和class 在C/C++/C#
- What all is inherited from parent class in C++?