Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.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# 如何从非托管DLL访问csharp中包含动态数组的结构?_C#_C_Dll_Marshalling_Unmanaged - Fatal编程技术网

C# 如何从非托管DLL访问csharp中包含动态数组的结构?

C# 如何从非托管DLL访问csharp中包含动态数组的结构?,c#,c,dll,marshalling,unmanaged,C#,C,Dll,Marshalling,Unmanaged,-在我的c代码中,我有一个结构,它在非托管dll(c代码)中包含许多大小未知的数组 -我需要将此结构的一个实例的数据封送到c#,稍后我会将其发送回非托管c代码 -一旦数据到达csharp,我就不需要对其进行操作,只需保留/存储一段时间(这样它就可以保留在字节数组中) -我不想使用关键字“不安全”,因为这是一个大项目,这只是一个小部分,我不想这样编译 我尝试将它作为lpArray进行封送,所有内容看起来都很好,但是当我回到csharp后查看内容时,它总是空的。这种封送处理样式适用于各种类型的动态数

-在我的c代码中,我有一个结构,它在非托管dll(c代码)中包含许多大小未知的数组

-我需要将此结构的一个实例的数据封送到c#,稍后我会将其发送回非托管c代码

-一旦数据到达csharp,我就不需要对其进行操作,只需保留/存储一段时间(这样它就可以保留在字节数组中)

-我不想使用关键字“不安全”,因为这是一个大项目,这只是一个小部分,我不想这样编译

我尝试将它作为lpArray进行封送,所有内容看起来都很好,但是当我回到csharp后查看内容时,它总是空的。这种封送处理样式适用于各种类型的动态数组,但不适用于结构

搜索网页是空白的,比我自己的情况复杂得多,但如果有人看到这样的链接,请张贴在这里,我会非常棒

谢谢

--这里的更新或多或少是我的代码结构:

c#:

c:

你说C代码不需要操纵结构。这使它成为一个很容易解决的问题。可以将结构指针视为不透明指针,即
IntPtr

首先,在本机代码中添加一个新函数:

pMystrucObj CreateStruct(void)
{
    pMystrucObj res = malloc(sizeof(*res));
    return res;
}
然后在你的C代码中你这样称呼它:

[DllImport("mydll.dll", CallingConvention=CallingConvention.Cdecl)]
private static extern IntPtr CreateStruct();
[DllImport("mydll.dll", CallingConvention=CallingConvention.Cdecl)]
private static extern int W_Thread_Connect_NET(
    IntPtr theStructPtr,
    string IPAddress, 
    int DevicePort,
    ....
);
IntPtr theStructPtr = CreateStruct();
int res = W_Thread_Connect_NET(theStructPtr, IPAddress, DevicePort, ...);
现在声明
W\u Thread\u Connect\u NET
如下:

[DllImport("mydll.dll", CallingConvention=CallingConvention.Cdecl)]
private static extern IntPtr CreateStruct();
[DllImport("mydll.dll", CallingConvention=CallingConvention.Cdecl)]
private static extern int W_Thread_Connect_NET(
    IntPtr theStructPtr,
    string IPAddress, 
    int DevicePort,
    ....
);
IntPtr theStructPtr = CreateStruct();
int res = W_Thread_Connect_NET(theStructPtr, IPAddress, DevicePort, ...);
就这样说吧:

[DllImport("mydll.dll", CallingConvention=CallingConvention.Cdecl)]
private static extern IntPtr CreateStruct();
[DllImport("mydll.dll", CallingConvention=CallingConvention.Cdecl)]
private static extern int W_Thread_Connect_NET(
    IntPtr theStructPtr,
    string IPAddress, 
    int DevicePort,
    ....
);
IntPtr theStructPtr = CreateStruct();
int res = W_Thread_Connect_NET(theStructPtr, IPAddress, DevicePort, ...);

当然,您还需要添加另一个名为
DestroyStruct
的函数,以便在使用完该结构后释放该结构的内存。

显示该结构的C声明,以及接收该结构的本机函数的C声明。然后我们可以向你展示你所需要的pinvoke。谢谢David-我添加了一些与我的工作类似的代码。当然,我不能将实际的代码粘贴到这里,因为它又大又乱,而且不允许我分享。让我知道,如果有任何其他信息,我需要包括,以帮助指导我的答案或更多信息的链接。!结构起源于哪里?C字符串是如何分配的?方法W_Thread_Connect_NET创建一个结构实例,用数据填充它。因此,即使W_Thread_Connect_NET返回一个int,strucReturn_句柄将是指向结构数据的指针,而这正是我在封送时遇到的问题。如果可能的话,我想通过引用从参数中获取数据,就像IpAddress被封送一样(那一个正在工作)。很公平,谢谢大家。