Chris
Chris

Reputation: 165

How would the following c(++)-struct be converted to C# for p/invoke usage

Im trying to wrap an older dll and am running into issues representing a structure it uses in C#. Nothing I have tried seems to be working. Any magicians able to help?

typedef struct _PARAM_BYNAME_DATA 
{ 
    n_char *szPntName; /* (in) point name */ 
    n_char *szPrmName; /* (in) parameter name */ 
    n_long nPrmOffset; /* (in) parameter offset */ 
    PARvalue *pupvValue; /* (in/out) parameter value union */ 
    n_ushort nType; /* (in/out) value type */ 
    n_long fStatus; /* (out) status of each value access */ 
} PARAM_BYNAME_DATA; 

If it helps the below is a VB port.

Type param_byname_data 
    point_name As String
    param_name As String
    param_offset As Long
    padding1 As Long 'for byte alignment between VB and C 
     param_value As Variant
    param_type As Integer
    padding2 As Integer 'for byte alignment between VB and C 
    status As Long status As Long 
End Type

And the following Delphi as well...

PARAM_BYNAME_DATA=record
    PntName:pchar;       // (in) point name
    PrmName:pchar;       // (in) parameter name
    PrmOffset:longword;  // (in) parameter offset
    pValue:pointer;      // (out) parameter value union
    nType:word;          // (out) value type
    fStatus:longword;    // (out) status of each value access */
end;

Upvotes: 2

Views: 226

Answers (1)

Brian ONeil
Brian ONeil

Reputation: 4339

The struct should look something like this...

[StructLayout(LayoutKind.Sequential)]
public struct MyStruct
{
    public string point_name;
    public string param_name;
    public Int32 param_offset;
    public VariantWrapper param_value;
    public Int32 param_type;
    public Int32 status;
};

Here is a good article that talks about structs and alignment that should help. The main thing is the struct layout and the bit alignment. It has been a while since I have had to marshal values out of C++ but I hope this helps.

Upvotes: 2

Related Questions