如何使用指向另一个结构的指针编组结构(从C ++到C#) [英] How do I marshal a struct with a pointer to another struct (from C++ to C#)
问题描述
问候!
正如Tittle所说,我需要在指向另一个结构的指针内编组一个结构(实际上,是另一个结构的向量)。在某些消息之后得到那个消息(相关问题 这里 ),地址为LParam。
它的定义是在第三方制作的.dll文件中(不要有源代码),用C ++编写。这是一个例子:
Greetings!
As tittle says, I need to marshal an struct within a pointer to another struct (actually,. a vector to another struct). That one is got after certain message (related question here), with it address as LParam.
It definition is in a .dll file made by a thirdparty (dont have source code), made in C++. Here's an example:
typedef struct _cImage
{
BYTE *Buffer;
int Lenght;
} cImage;
typedef struct _Images
{
DWORD DocID;
cImage *Imgs;
BOOL Enabled;
} Images;
根据手册......
According to manual...
它是指向cImage结构向量的指针:图像为5个元素,片段为10个。
It's the pointer to a vector of cImage structure: 5 elements for images and 10 for snippets.
我尝试了什么:
首先我尝试在内部数组中定义结构:
What I have tried:
First I tried to define the struct within an inner array:
[StructLayout(LayoutKind.Sequential, Pack = 0)]
public struct cImage
{
public IntPtr Buffer;
public int Lenght;
};
[StructLayout(LayoutKind.Sequential, Pack = 0)]
public struct Images
{
public int DocID;
[MarshalAs(UnmanagedType.ByValArray)]
public cImage[] Imgs;
[MarshalAs(UnmanagedType.Bool)]
public bool Enabled;
};
以下是我尝试提取信息的方法(它在ScanMgr类中)
Here is how I try to extract the information (it's in a class "ScanMgr")
public bool getImage(IntPtr imgPtr, out Images img)
{
try
{
img = (Images)Marshal.PtrToStructure(imgPtr, typeof(Images));
return true;
}
catch { img = new Images(); }
return false;
}
这里是我调用函数的地方(扫描仪是ScanMgr的一个对象):
And here is where I call the function (scanner is an object of ScanMgr):
switch ((int)msg.WParam)
{
/* Other cases */
case (int)WMPAR.ImagesReady:
Images img;
if (scanner.getImage(m.LParam, out img))
/* Here I should get access and use the struct members */
break;
}
然后我尝试用IntPtr替换数组到内部结构,以便稍后编组:
I then tried replacing the array with an IntPtr to the inner struct, to marshal it later:
[StructLayout(LayoutKind.Sequential, Pack = 0)]
public struct Images
{
public int DocID;
public IntPtr Imgs;
[MarshalAs(UnmanagedType.Bool)]
public bool Enabled;
};
////////////
switch ((int)msg.WParam)
{
/* Other cases */
case (int)WMPAR.ImagesReady:
Images img;
if (scanner.getImage(m.LParam, out img))
{
cImage cImg = (cImage)Marshal.PtrToStructure(img.Imgs, typeof(cImage));
/* Access/Use the struct members */
}
break;
}
我的问题是:什么是编组结构内部向量的正确方法?如果是IntPtr,那么我该如何提取这些成员呢? (因为只是要处理图像,我会坚持使用5的向量)
提前谢谢
My question is: What is the right way to marshal that inner vector of structs? If is the IntPtr, then how can I extract those members? (since just gonna work with images, I'll stick with a vector of 5)
Thanks in advance
推荐答案
自己解决问题(是的,我是图书馆老鼠!)
我将指向vector的指针定义为一个IntPtr(参见上一篇文章中的第二个C#结构实现)。然后:
Solving stuff by myself (yup, I'm a library mice!)
I defined the pointer to vector as an IntPtr (see 2nd C# struct implementation in previous post). Then:
switch ((int)msg.WParam)
{
/* Other cases */
case (int)WMPAR.ImagesReady:
Images img;
if (scanner.getImage(m.LParam, out img))
{
var size = Marshal.SizeOf(typeof(cImage));
cImage[] ar = new cImage[5];
for (int i = 0; i < 5; i++)
{
IntPtr p = new IntPtr(img.Imgs.ToInt32() + i * size);
ar[i] = (cImage)Marshal.PtrToStructure(p, typeof(cImage));
logInfo.Items.Add(ar[i].Buffer + "-" + ar[i].Lenght);
}
}
break;
}
这篇关于如何使用指向另一个结构的指针编组结构(从C ++到C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!