Sign in to follow this  
RIGHT_THEN

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

Recommended Posts

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]

Share this post


Link to post
Share on other sites

[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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this