如何在c#中将非托管结构指针转换为byte []? [英] How to convert a unmanaged structure pointer to byte[] in c#?
本文介绍了如何在c#中将非托管结构指针转换为byte []?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请帮我在c#中将非托管结构指针转换为byte []?
Please help me to convert a unmanaged structure pointer to byte[] in c#?
public struct struct1
{
public byte a;
public int b;
}
struct1 objstruct= new struct1();
struct1* ptrStruct=&objstruct;
ptrStruct->a=1;
ptrStruct->b=4;
如何我可以转换ptrStructo byte []吗?
请帮帮我
how can i convert ptrStructo byte[]?
please help me
推荐答案
为什么你要转换?只需创建自己的类,如下所示,并将其用于您的应用。
Why you have to convert ? Just create your own class is as below and use it for your app.
public class ClassName
{
public byte [] a { get; set; }
public int b { get; set; }
}
我希望这会对你有所帮助。
I hope this will help to you.
如果您需要将一个托管结构传递给非托管代码,您可以像在以下示例中那样进行(取自:MSDN [ ^ ]):
If you need to pass one managed structure to unmanaged code you could do it like in the foollowing example (taken from: MSDN[^]):
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)]
public struct Point {
public int x;
public int y;
}
[StructLayout(LayoutKind.Explicit)]
public struct Rect {
[FieldOffset(0)] public int left;
[FieldOffset(4)] public int top;
[FieldOffset(8)] public int right;
[FieldOffset(12)] public int bottom;
}
class Win32API {
[DllImport("User32.dll")]
public static extern bool PtInRect(ref Rect r, Point p);
}
如果你想将数组传递给非托管函数,这也有帮助: MSDN [ ^ ]
用于从Windows API中使用函数pinvoke.net/ [ ^ ]有非常有用的资源和您可以使用的示例代码。
If you want to pass the array to the unmanaged function this could also help: MSDN[^]
For consuming functions from windows APIs pinvoke.net/[^] has very usefull resources and sample code you could use.
这篇关于如何在c#中将非托管结构指针转换为byte []?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文