Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.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# 如何在PINVOKE中用字符串数据传递struct? 我把通过C代码的字符串数据传递给C++的DLL。< /P>_C#_C++_Pinvoke - Fatal编程技术网

C# 如何在PINVOKE中用字符串数据传递struct? 我把通过C代码的字符串数据传递给C++的DLL。< /P>

C# 如何在PINVOKE中用字符串数据传递struct? 我把通过C代码的字符串数据传递给C++的DLL。< /P>,c#,c++,pinvoke,C#,C++,Pinvoke,c++代码 typedef struct { LPCSTR lpLibFileName; LPCSTR lpProcName; LPVOID pPointer1; LPVOID pPointer2; } ENTITY, *PENTITY, *LPENTITY; extern "C" __declspec(dllexport) int Test(LPENTITY entryList, int size); int Test(LPENTITY entryList,

c++代码

typedef struct 
{
    LPCSTR lpLibFileName;
    LPCSTR lpProcName;
    LPVOID pPointer1;
    LPVOID pPointer2;
} ENTITY, *PENTITY, *LPENTITY;
extern "C" __declspec(dllexport) int Test(LPENTITY entryList, int size);

int Test(LPENTITY entryList, int size)
{
    for (int i = 0; i < size; i++)
    {
        ENTITY e = entryList[i];
        // the char* value doesn't get passed correctly.
        cout << e.lpLibFileName << e.lpProcName << endl;
    }
    return 0;
}
PINVOKE已触发,但无法正确传递lpLibFileName和lpProcName等char*数据。我错过什么了吗?如何纠正


谢谢。

当传递自定义结构数组之类的参数时,请在定义自己的数据结构时使用“struct”而不是“class”。我把它改回struct后,一切都很好

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
private struct Entity
{
    public string lpLibFileName;
    public string lpProcName;
    public IntPtr pPointer1;
    public IntPtr pPointer2;
}
您的代码将C#类映射到本机结构。因为C#类是引用类型,所以它将被封送为引用。因此,您的代码传递一个引用数组,该数组被封送到本机端的指针数组

但是本机代码需要指向值类型的结构数组的指针。因此,最简单的解决方案是将
实体
的声明更改为
结构
,而不是

我可以看到的其他问题:

  • 本机代码似乎正在使用
    cdecl
    调用约定。您需要更改C代码以匹配
  • 您正在用
    Out
    装饰数组参数。您不能将对
    字符串
    字段的修改封送回托管代码
  • 您需要确保传递给
    GetFunctionPointerForDelegate
    的委托保持活动状态,以停止收集它们

  • [DllImport]属性中缺少必需的CallingConvention属性。默认是STDCLASK,但您的函数是Cdecl。是的,您是对的,我应该在C++代码中使用STDCLAK而不是CDDEL。但真正阻碍我的是,我在c#代码中将我的结构定义为“类”,而不是“结构”。将其更改回struct后,一切正常。谢谢
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    private struct Entity
    {
        public string lpLibFileName;
        public string lpProcName;
        public IntPtr pPointer1;
        public IntPtr pPointer2;
    }