数据转换调用。net的marshal。。using System.Runtime.InteropServices; using namespace System; using namespace System::Runtime::InteropServices;public value struct Point { public: property int X; property int Y; }; extern bool CloseHandle(IntPtr h);int main() { // Demonstrate the use of public static fields of the Marshal // class. Console::WriteLine( "SystemDefaultCharSize={0},SystemMaxDBCSCharSize={1}", Marshal::SystemDefaultCharSize, Marshal::SystemMaxDBCSCharSize); // Demonstrate the use of the SizeOf method of the Marshal // class. Console::WriteLine("Number of bytes needed by a Point object: {0}", Marshal::SizeOf(Point::typeid)); Point point; Console::WriteLine("Number of bytes needed by a Point object: {0}", Marshal::SizeOf(point)); // Demonstrate how to call GlobalAlloc and // GlobalFree using the Marshal class. IntPtr hglobal = Marshal::AllocHGlobal(100); Marshal::FreeHGlobal(hglobal); // Demonstrate how to use the Marshal class to get the Win32 // error code when a Win32 method fails. bool isCloseHandleSuccess = CloseHandle(IntPtr(-1)); if (!isCloseHandleSuccess) { Console::WriteLine( "CloseHandle call failed with an error code of: {0}", Marshal::GetLastWin32Error()); } };// This is a platform invoke prototype. SetLastError is true, // which allows the GetLastWin32Error method of the Marshal class // to work correctly. [DllImport("Kernel32", ExactSpelling = true, SetLastError = true)] extern bool CloseHandle(IntPtr h);// This code produces the following output. // // SystemDefaultCharSize=2, SystemMaxDBCSCharSize=1 // Number of bytes needed by a Point object: 8 // Number of bytes needed by a Point object: 8 // CloseHandle call failed with an error code of: 6
C++/CLI可以实现MixCode,C++和C++/CLI的代码混用,C++/CLI的程序集可以直接被C#工程所引用string --> std::string无法直接转换
不过用C++/CLI的话可以这样写:
std::string cstring;
String^ cliString = gcnew String(cstring.c_str());
要引用一个类库。
using System.RunTime.Inter...[DllImport("xxxdll.dll")]
private static extern int xxx(value:string);
具体一点呢,能给个例子吗?比如说://Car.h
class Car{
public:
void Car(std::string name);
void Run(int length);
private:
...
}class BMW : public Car{
public:
...
private:
...
}//Car.cpp
.........
怎么编译成DLL并在C#里调用?
现实的项目中,我们自己的要求是能避免做PInvoke就避免.使用C++/CLI效率比起PInvoke不是一倍两倍的关系,而是有本质性的提高.这是一张测试数据表:重复次数 C# 程序 C++程序
50,000 61 10
500,000 600 70
1,000,000 1162 140
5,000,000 6369 721 C++/CLI:第一流的CLI语言
c#调用DLL
参考的资料主要来自msdn,google,百度,论坛等。
using namespace System;
using namespace System::Runtime::InteropServices;public value struct Point
{
public:
property int X;
property int Y;
};
extern bool CloseHandle(IntPtr h);int main()
{
// Demonstrate the use of public static fields of the Marshal
// class.
Console::WriteLine(
"SystemDefaultCharSize={0},SystemMaxDBCSCharSize={1}",
Marshal::SystemDefaultCharSize,
Marshal::SystemMaxDBCSCharSize); // Demonstrate the use of the SizeOf method of the Marshal
// class.
Console::WriteLine("Number of bytes needed by a Point object: {0}",
Marshal::SizeOf(Point::typeid));
Point point;
Console::WriteLine("Number of bytes needed by a Point object: {0}",
Marshal::SizeOf(point)); // Demonstrate how to call GlobalAlloc and
// GlobalFree using the Marshal class.
IntPtr hglobal = Marshal::AllocHGlobal(100);
Marshal::FreeHGlobal(hglobal); // Demonstrate how to use the Marshal class to get the Win32
// error code when a Win32 method fails.
bool isCloseHandleSuccess = CloseHandle(IntPtr(-1));
if (!isCloseHandleSuccess)
{
Console::WriteLine(
"CloseHandle call failed with an error code of: {0}",
Marshal::GetLastWin32Error());
}
};// This is a platform invoke prototype. SetLastError is true,
// which allows the GetLastWin32Error method of the Marshal class
// to work correctly.
[DllImport("Kernel32", ExactSpelling = true, SetLastError = true)]
extern bool CloseHandle(IntPtr h);// This code produces the following output.
//
// SystemDefaultCharSize=2, SystemMaxDBCSCharSize=1
// Number of bytes needed by a Point object: 8
// Number of bytes needed by a Point object: 8
// CloseHandle call failed with an error code of: 6
在c++里写个简单的函数出口,只要几个参数就行的。
然后在这个函数内部调用C++类,就行实例化,等操作。
这样就可以避免C#调用C++的类。