首页 新闻 会员 周边

C# 怎么传递二维数组的指针到C++的DLL库函数中

0
悬赏园豆:60 [已解决问题] 解决于 2014-04-01 10:29

直接上代码吧

这个是一个用LABVIEW写的数字图像处理的库,头文件是

#include "extcode.h"
#pragma pack(push)
#pragma pack(1)

#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
    int32_t dimSizes[2];
    uint8_t elt[1];
    } Uint8ArrayBase;
typedef Uint8ArrayBase **Uint8Array;

/*!
 * Test
 */
int32_t __cdecl Test(Uint8Array *Image, double *Distance, int16_t *RightBool, 
    int16_t *LeftBool, int16_t *OK);

long __cdecl LVDLLStatus(char *errStr, int errStrLen, void *module);

/*
* Memory Allocation/Resize/Deallocation APIs for type 'Uint8Array'
*/
Uint8Array __cdecl AllocateUint8Array (int32 *dimSizeArr);
MgErr __cdecl ResizeUint8Array (Uint8Array *hdlPtr, int32 *dimSizeArr);
MgErr __cdecl DeAllocateUint8Array (Uint8Array *hdlPtr);

#ifdef __cplusplus
} // extern "C"
#endif

#pragma pack(pop)

现在需要在C#中引用Test函数 把

[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public unsafe struct Uint8ArrayBase
{
public unsafe int[] X;

public unsafe byte[] Value;
}结构定义

Uint8ArrayBase[,] imgdata= new Uint8ArrayBase[4096, 1024];

传递到Test函数的参数里面去,老是传不下去,不知道是参数问题还是啥,情况比较急,求大侠们指导指导 QQ821881145

大芝麻的主页 大芝麻 | 初学一级 | 园豆:4
提问于:2014-03-31 13:46
< >
分享
最佳答案
0

void Test(ref IntPtr param1);

收获园豆:60
Launcher | 高人七级 |园豆:45045 | 2014-03-31 14:15

void Test(strc* param1)

这种呢,我弄错了 不是一个指向指针的指针

这种应该怎么写

void Test(IntPtr param1);这样吗

大芝麻 | 园豆:4 (初学一级) | 2014-03-31 14:28

@大芝麻: 对

Launcher | 园豆:45045 (高人七级) | 2014-03-31 14:33

@Launcher:  Uint8ArrayBase[,] str = new Uint8ArrayBase[2,2];

搞半天还是要指向指针的指针

void Test(ref IntPtr param1);这个方法怎么把str传进去了 编译说不能把 IntPtr pt = (IntPtr)(str);

 

错误 1 无法将类型“WindowsFormsApplication2.Uint8ArrayBase[*,*]”转换为“System.IntPtr” G:\测试项目\WindowsFormsApplication2\WindowsFormsApplication2\Form1.cs 32 33 WindowsFormsApplication2

 

求指教

大芝麻 | 园豆:4 (初学一级) | 2014-03-31 22:19

@大芝麻: Marshal.UnsafeAddrOfPinnedArrayElement

Launcher | 园豆:45045 (高人七级) | 2014-04-01 09:14

@Launcher: 大侠能Q聊一下吗,我这样弄了还是有点问题。821881145

大芝麻 | 园豆:4 (初学一级) | 2014-04-01 09:58

@Launcher: 拜托了

大芝麻 | 园豆:4 (初学一级) | 2014-04-01 10:03

@大芝麻: 你应该用下面这几个函数来创建你的数组:

Uint8Array __cdecl AllocateUint8Array (int32 *dimSizeArr);
MgErr __cdecl ResizeUint8Array (Uint8Array *hdlPtr, int32 *dimSizeArr);
MgErr __cdecl DeAllocateUint8Array (Uint8Array *hdlPtr);

Launcher | 园豆:45045 (高人七级) | 2014-04-01 10:30
其他回答(1)
0

将这个复杂格式用C/C++简化写一个方法(易于C#调用的方法),然后由C#调用

迅捷网络[来送福利] | 园豆:576 (小虾三级) | 2014-03-31 13:54
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册