C# 使用std::string封送结构
我尝试将结构封送到DLL的函数。我的DLL是用C++编写的,接受ANSI字符串。C# 使用std::string封送结构,c#,c++,marshalling,C#,C++,Marshalling,我尝试将结构封送到DLL的函数。我的DLL是用C++编写的,接受ANSI字符串。 // C++ signature __declspec(dllexport) unsigned long __stdcall fn_func(struct st_Mystruct* stMystruct); // The Structure in C++ #pragma pack(1) struct st_Mystruct{ st_Mystruct() { string1 =
// C++ signature
__declspec(dllexport) unsigned long __stdcall fn_func(struct st_Mystruct* stMystruct);
// The Structure in C++
#pragma pack(1)
struct st_Mystruct{
st_Mystruct()
{
string1 = "";
string2 = "";
string3 = "";
}
std::string string1;
std::string string2;
std::string string3;
};
#pragma pack()
// C# Import
[DllImport("MyDll.dll", CallingConvention = CallingConvention.StdCall)]
public static extern uint fn_func([In, MarshalAs(UnmanagedType.LPStruct, SizeParamIndex = 1)] ref st_Mystruct stMystruct);
如何在C#中声明和封送结构?我认为您不能。P/Invoke只对C风格的函数和结构真正有用。如果需要McReC++调用,则需要使用C++、.NET或COM。这是不可能的,只有C++编译器才能生成创建STD::String对象所需的代码。你必须改为写一个C++/CLI包装器。我认为你做不到。P/Invoke只对C风格的函数和结构真正有用。如果需要McReC++调用,则需要使用C++、.NET或COM。这是不可能的,只有C++编译器才能生成创建STD::String对象所需的代码。您必须改为编写C++/CLI包装器。