Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用std::string封送结构_C#_C++_Marshalling - Fatal编程技术网

C# 使用std::string封送结构

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 =

我尝试将结构封送到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 = "";
        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包装器。