Click here to Skip to main content
15,885,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please help me to convert a unmanaged structure pointer to byte[] in c#?

C#
public struct struct1
{
    public byte a; 
    public int b; 
}

struct1 objstruct= new struct1();
struct1* ptrStruct=&objstruct;
ptrStruct->a=1;
ptrStruct->b=4;


how can i convert ptrStructo byte[]?

please help me
Posted
Updated 26-Sep-13 20:36pm
v2

Why you have to convert ? Just create your own class is as below and use it for your app.

C#
public class ClassName
   {
    public byte [] a { get; set; }
    public int b { get; set; }
   }



I hope this will help to you.
 
Share this answer
 
If you need to pass one managed structure to unmanaged code you could do it like in the foollowing example (taken from: MSDN[^]):

C#
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);
}


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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900