[.net] Struct Conversions For c# p/Invoke

Started by
3 comments, last by RIGHT_THEN 16 years, 1 month ago
GentleMen, How does one convert this struct to usable c# type

typedef struct _FPO_DATA {
    DWORD ulOffStart;      // offset 1st byte of function code
    DWORD cbProcSize;      // number of bytes in function
    DWORD cdwLocals;       // number of bytes in locals/4
    WORD cdwParams;        // number of bytes in params/4
    WORD cbProlog : 8;     // number of bytes in prolog
    WORD cbRegs   : 3;     // number of registers saved
    WORD fHasSEH  : 1;     // TRUE for structured exception handling
    WORD fUseBP   : 1;     // TRUE if EBP register has been allocated
    WORD reserved : 1;     // reserved for future use
    WORD cbFrame  : 2;     // frame type
} FPO_DATA, *PFPO_DATA;
 

I could go this far beyound that i am confused

unsafe public struct FPO_DATA
            {
                public System.UInt32 ulOffStart;
                public System.UInt32 cbProcSize;
                public System.UInt32 cdwLocals;
                public System.UInt16 cdwParams;

                //System.UInt16 cbProlog : 8 ;      
                //System.UInt16 cbRegs   : 3 ;      
                //System.UInt16 fHasSEH  : 1 ;      
                //System.UInt16 fUseBP   : 1 ;      
                //System.UInt16 reserved : 1 ;      
                //System.UInt16 cbFrame  : 2 ;      
            }  ;


2) Do you think i converted this struct correctly


typedef struct _IMAGE_SECTION_HEADER {
    BYTE    Name[IMAGE_SIZEOF_SHORT_NAME];
    union {
        DWORD   PhysicalAddress;
        DWORD   VirtualSize;
    } Misc;
    DWORD   VirtualAddress;
    DWORD   SizeOfRawData;
    DWORD   PointerToRawData;
    DWORD   PointerToRelocations;
    DWORD   PointerToLinenumbers;
    WORD    NumberOfRelocations;
    WORD    NumberOfLinenumbers;
    DWORD   Characteristics;
} IMAGE_SECTION_HEADER, *PIMAGE_SECTION_HEADER;
 
public enum IMAGE_SIZEOF_SHORT_NAME 
{
   IMAGE_SIZEOF_SHORT_NAME = 8 
}

unsafe public struct Misc
            {
                public System.UInt32 PhysicalAddress;
                public System.UInt32 VirtualSize;
            } ;
unsafe public struct IMAGE_SECTION_HEADER
            {
                [MarshalAs(UnmanagedType.ByValArray, SizeConst =  IMAGE_SIZEOF_SHORT_NAME.IMAGE_SIZEOF_SHORT_NAME )]  
                public System.Byte Name; //[ IMAGE_SIZEOF_SHORT_NAME ] ;  
                public Misc Misc;
                public System.UInt32 VirtualAddress;
                public System.UInt32 SizeOfRawData;
                public System.UInt32 PointerToRawData;
                public System.UInt32 PointerToRelocations;
                public System.UInt32 PointerToLinenumbers;
                public System.UInt16 NumberOfRelocations;
                public System.UInt16 NumberOfLinenumbers;
                public System.UInt32 Characteristics;
            } ;




Gratefully RIGHT_THEN [Edited by - RIGHT_THEN on March 2, 2008 11:00:38 AM]
Advertisement
[StructLayout(LayoutKind.Sequential)]unsafe public struct FPO_DATA{public uint OffStart; // Drop the Hungarian notation, it's useless ;)public uint ProcSize;public uint Locals;public ushort Params;private ushort raw_bytes;public int Prolog{    get { return (int)(raw_bytes & 0xFF00) >> 8; }}//System.UInt16 cbProlog : 8 ; //System.UInt16 cbRegs : 3 ; //System.UInt16 fHasSEH : 1 ; //System.UInt16 fUseBP : 1 ; //System.UInt16 reserved : 1 ; //System.UInt16 cbFrame : 2 ; }


You get the drift. Use masks and shift operators to select the correct bits and return the necessary data.

[OpenTK: C# OpenGL 4.4, OpenGL ES 3.0 and OpenAL 1.1. Now with Linux/KMS support!]

WHat about the second part of the question did i do it right

ThankYou
RIGHT_THEN
When using structs and interop you should place the following above each struct declaration

[StructLayout(LayoutKind.Sequential)]


This packs the struct so it can be passed to API functions. There are plenty of examples of how you should use p/invoke @ pinvoke.net website.

Another thing of note is the Union. A union means that the two members point to the same location in memory. To do that in C# you have to explicitly define the members.

[StructLayout(LayoutKind.Explicit)]public struct Misc{	[FieldOffset(0)]	public System.UInt32 PhysicalAddress;	[FieldOffset(0)]	public System.UInt32 VirtualSize;};


Also remove the "unsafe" keyword. You are not using unsafe code. The rest looks okay to me.
Sir Mr Headkaze

Thankyou So much

RIGHT_THEN

This topic is closed to new replies.

Advertisement