Another C header conversion help.

A

Andre Azevedo

Hi all,

I'm translation some C headers to C# but it doesn't work.
I need to fill the Call struct and pass it in encode function as *pdu
parameter. The function will encode and fill the PrivateData struct in *priv
parameter filling the data field but it return some wrong values. I've the
original C application and I know what must return but I don't know what is
wrong.

Can anyone help me?

TIA,

--
Andre

-------------- C header -----------------

encode ( int pdunum, CONST_PARAM void FAR * pdu, PrivateData FAR * priv );

typedef struct Call {
Boolean priorityCalling;
short maxRings;
AnswerTreat answerTreat; ---------> ---------------> simple enum like
ProtocolType !
DeviceID_t destRoute; -----------> char[64] in C! I'm using C#
string with managedAs=ByValStr
UserInfo userInfo;
} Call ;

typedef struct UserInfo {
ProtocolType type;
struct {
short length;
unsigned char value[129];
} data;
} UserInfo ;

typedef enum ProtocolType {
NONE = -1,
USER_SPECIFIC = 0,
ASCII = 4
} ProtocolType;

typedef struct PrivateData {
char vendor[32];
unsigned short length;
char data[1024];
} PrivateData ;
 
N

Nicholas Paldino [.NET/C# MVP]

Andre,

Here are the definitions that you want:

public enum ProtocolType
{
NONE = -1,
USER_SPECIFIC = 0,
ASCII = 4
}

[StructLayout(LayoutKind.Sequential)]
public struct DataStruct
{
public short length
[MarshalAs(UnmanagedType.ByValArray, SizeConst=129)]
byte[] value;
}

[StructLayout(LayoutKind.Sequential)]
public struct UserInfo
{
public ProtocolType type;

public DataStruct data;
}

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct PrivateData
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string vendor;
[MarshalAs(UnmanagedType.U2)]
public short length;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=1024)]
public string data;
}

[StructLayout(Layoutkind.Sequential, CharSet=CharSet.Ansi)]
public struct Call
{
// This assumes you are using BOOL, which is a four byte value. This
might need tweaking.
public bool priorityCalling;

public short maxRings;

public AnswerTreat answerTreat;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
string destRoute

UserInfo userInfo;
}

[DllImport("somedll.dll")]
public static extern int encode (
int pdunum,

// What type is this?
// CONST_PARAM void FAR * pdu,

ref PrivateData priv );

Hope this helps.
 
A

Andre Azevedo

Hi,

// What type is this?
// CONST_PARAM void FAR * pdu,

This is the Call struct that must be passed. I think in this case that the
parameter will be an object parameter with a UnmanagedType.AsAny.
I'll try now.

Thanks,
--
Andre


Nicholas Paldino said:
Andre,

Here are the definitions that you want:

public enum ProtocolType
{
NONE = -1,
USER_SPECIFIC = 0,
ASCII = 4
}

[StructLayout(LayoutKind.Sequential)]
public struct DataStruct
{
public short length
[MarshalAs(UnmanagedType.ByValArray, SizeConst=129)]
byte[] value;
}

[StructLayout(LayoutKind.Sequential)]
public struct UserInfo
{
public ProtocolType type;

public DataStruct data;
}

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct PrivateData
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string vendor;
[MarshalAs(UnmanagedType.U2)]
public short length;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=1024)]
public string data;
}

[StructLayout(Layoutkind.Sequential, CharSet=CharSet.Ansi)]
public struct Call
{
// This assumes you are using BOOL, which is a four byte value. This
might need tweaking.
public bool priorityCalling;

public short maxRings;

public AnswerTreat answerTreat;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
string destRoute

UserInfo userInfo;
}

[DllImport("somedll.dll")]
public static extern int encode (
int pdunum,

// What type is this?
// CONST_PARAM void FAR * pdu,

ref PrivateData priv );

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Andre Azevedo said:
Hi all,

I'm translation some C headers to C# but it doesn't work.
I need to fill the Call struct and pass it in encode function as *pdu
parameter. The function will encode and fill the PrivateData struct in
*priv
parameter filling the data field but it return some wrong values. I've
the
original C application and I know what must return but I don't know what
is
wrong.

Can anyone help me?

TIA,

--
Andre

-------------- C header -----------------

encode ( int pdunum, CONST_PARAM void FAR * pdu, PrivateData FAR *
priv );

typedef struct Call {
Boolean priorityCalling;
short maxRings;
AnswerTreat answerTreat; ---------> ---------------> simple enum like
ProtocolType !
DeviceID_t destRoute; -----------> char[64] in C! I'm using C#
string with managedAs=ByValStr
UserInfo userInfo;
} Call ;

typedef struct UserInfo {
ProtocolType type;
struct {
short length;
unsigned char value[129];
} data;
} UserInfo ;

typedef enum ProtocolType {
NONE = -1,
USER_SPECIFIC = 0,
ASCII = 4
} ProtocolType;

typedef struct PrivateData {
char vendor[32];
unsigned short length;
char data[1024];
} PrivateData ;
 
N

Nicholas Paldino [.NET/C# MVP]

Andre,

I wouldn't use the AsAny. You should use:

ref Call pdu

instead.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Andre Azevedo said:
Hi,

// What type is this?
// CONST_PARAM void FAR * pdu,

This is the Call struct that must be passed. I think in this case that the
parameter will be an object parameter with a UnmanagedType.AsAny.
I'll try now.

Thanks,
--
Andre


Nicholas Paldino said:
Andre,

Here are the definitions that you want:

public enum ProtocolType
{
NONE = -1,
USER_SPECIFIC = 0,
ASCII = 4
}

[StructLayout(LayoutKind.Sequential)]
public struct DataStruct
{
public short length
[MarshalAs(UnmanagedType.ByValArray, SizeConst=129)]
byte[] value;
}

[StructLayout(LayoutKind.Sequential)]
public struct UserInfo
{
public ProtocolType type;

public DataStruct data;
}

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct PrivateData
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string vendor;
[MarshalAs(UnmanagedType.U2)]
public short length;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=1024)]
public string data;
}

[StructLayout(Layoutkind.Sequential, CharSet=CharSet.Ansi)]
public struct Call
{
// This assumes you are using BOOL, which is a four byte value. This
might need tweaking.
public bool priorityCalling;

public short maxRings;

public AnswerTreat answerTreat;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
string destRoute

UserInfo userInfo;
}

[DllImport("somedll.dll")]
public static extern int encode (
int pdunum,

// What type is this?
// CONST_PARAM void FAR * pdu,

ref PrivateData priv );

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Andre Azevedo said:
Hi all,

I'm translation some C headers to C# but it doesn't work.
I need to fill the Call struct and pass it in encode function as *pdu
parameter. The function will encode and fill the PrivateData struct in
*priv
parameter filling the data field but it return some wrong values. I've
the
original C application and I know what must return but I don't know what
is
wrong.

Can anyone help me?

TIA,

--
Andre

-------------- C header -----------------

encode ( int pdunum, CONST_PARAM void FAR * pdu, PrivateData FAR *
priv );

typedef struct Call {
Boolean priorityCalling;
short maxRings;
AnswerTreat answerTreat; ---------> ---------------> simple enum like
ProtocolType !
DeviceID_t destRoute; -----------> char[64] in C! I'm using C#
string with managedAs=ByValStr
UserInfo userInfo;
} Call ;

typedef struct UserInfo {
ProtocolType type;
struct {
short length;
unsigned char value[129];
} data;
} UserInfo ;

typedef enum ProtocolType {
NONE = -1,
USER_SPECIFIC = 0,
ASCII = 4
} ProtocolType;

typedef struct PrivateData {
char vendor[32];
unsigned short length;
char data[1024];
} PrivateData ;
 
A

Andre Azevedo

Ok, I understand.
But there's a lot of another structs that maybe passed in the same
parameter. I was trying to passing an IntPtr buffer.

--
Andre


Nicholas Paldino said:
Andre,

I wouldn't use the AsAny. You should use:

ref Call pdu

instead.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Andre Azevedo said:
Hi,

// What type is this?
// CONST_PARAM void FAR * pdu,

This is the Call struct that must be passed. I think in this case that
the parameter will be an object parameter with a UnmanagedType.AsAny.
I'll try now.

Thanks,
--
Andre


Nicholas Paldino said:
Andre,

Here are the definitions that you want:

public enum ProtocolType
{
NONE = -1,
USER_SPECIFIC = 0,
ASCII = 4
}

[StructLayout(LayoutKind.Sequential)]
public struct DataStruct
{
public short length
[MarshalAs(UnmanagedType.ByValArray, SizeConst=129)]
byte[] value;
}

[StructLayout(LayoutKind.Sequential)]
public struct UserInfo
{
public ProtocolType type;

public DataStruct data;
}

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct PrivateData
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string vendor;
[MarshalAs(UnmanagedType.U2)]
public short length;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=1024)]
public string data;
}

[StructLayout(Layoutkind.Sequential, CharSet=CharSet.Ansi)]
public struct Call
{
// This assumes you are using BOOL, which is a four byte value. This
might need tweaking.
public bool priorityCalling;

public short maxRings;

public AnswerTreat answerTreat;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
string destRoute

UserInfo userInfo;
}

[DllImport("somedll.dll")]
public static extern int encode (
int pdunum,

// What type is this?
// CONST_PARAM void FAR * pdu,

ref PrivateData priv );

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hi all,

I'm translation some C headers to C# but it doesn't work.
I need to fill the Call struct and pass it in encode function as *pdu
parameter. The function will encode and fill the PrivateData struct in
*priv
parameter filling the data field but it return some wrong values. I've
the
original C application and I know what must return but I don't know
what is
wrong.

Can anyone help me?

TIA,

--
Andre

-------------- C header -----------------

encode ( int pdunum, CONST_PARAM void FAR * pdu, PrivateData FAR *
priv );

typedef struct Call {
Boolean priorityCalling;
short maxRings;
AnswerTreat answerTreat; ---------> ---------------> simple enum
like
ProtocolType !
DeviceID_t destRoute; -----------> char[64] in C! I'm using C#
string with managedAs=ByValStr
UserInfo userInfo;
} Call ;

typedef struct UserInfo {
ProtocolType type;
struct {
short length;
unsigned char value[129];
} data;
} UserInfo ;

typedef enum ProtocolType {
NONE = -1,
USER_SPECIFIC = 0,
ASCII = 4
} ProtocolType;

typedef struct PrivateData {
char vendor[32];
unsigned short length;
char data[1024];
} PrivateData ;
 
N

Nicholas Paldino [.NET/C# MVP]

Andre,

I would recommend making separate declarations then for each type that
could be passed in. This way, you can enforce type-safety.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Andre Azevedo said:
Ok, I understand.
But there's a lot of another structs that maybe passed in the same
parameter. I was trying to passing an IntPtr buffer.

--
Andre


Nicholas Paldino said:
Andre,

I wouldn't use the AsAny. You should use:

ref Call pdu

instead.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Andre Azevedo said:
Hi,

// What type is this?
// CONST_PARAM void FAR * pdu,

This is the Call struct that must be passed. I think in this case that
the parameter will be an object parameter with a UnmanagedType.AsAny.
I'll try now.

Thanks,
--
Andre


in message Andre,

Here are the definitions that you want:

public enum ProtocolType
{
NONE = -1,
USER_SPECIFIC = 0,
ASCII = 4
}

[StructLayout(LayoutKind.Sequential)]
public struct DataStruct
{
public short length
[MarshalAs(UnmanagedType.ByValArray, SizeConst=129)]
byte[] value;
}

[StructLayout(LayoutKind.Sequential)]
public struct UserInfo
{
public ProtocolType type;

public DataStruct data;
}

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct PrivateData
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]
public string vendor;
[MarshalAs(UnmanagedType.U2)]
public short length;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=1024)]
public string data;
}

[StructLayout(Layoutkind.Sequential, CharSet=CharSet.Ansi)]
public struct Call
{
// This assumes you are using BOOL, which is a four byte value.
This might need tweaking.
public bool priorityCalling;

public short maxRings;

public AnswerTreat answerTreat;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
string destRoute

UserInfo userInfo;
}

[DllImport("somedll.dll")]
public static extern int encode (
int pdunum,

// What type is this?
// CONST_PARAM void FAR * pdu,

ref PrivateData priv );

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hi all,

I'm translation some C headers to C# but it doesn't work.
I need to fill the Call struct and pass it in encode function as *pdu
parameter. The function will encode and fill the PrivateData struct in
*priv
parameter filling the data field but it return some wrong values. I've
the
original C application and I know what must return but I don't know
what is
wrong.

Can anyone help me?

TIA,

--
Andre

-------------- C header -----------------

encode ( int pdunum, CONST_PARAM void FAR * pdu, PrivateData FAR *
priv );

typedef struct Call {
Boolean priorityCalling;
short maxRings;
AnswerTreat answerTreat; ---------> ---------------> simple enum
like
ProtocolType !
DeviceID_t destRoute; -----------> char[64] in C! I'm using C#
string with managedAs=ByValStr
UserInfo userInfo;
} Call ;

typedef struct UserInfo {
ProtocolType type;
struct {
short length;
unsigned char value[129];
} data;
} UserInfo ;

typedef enum ProtocolType {
NONE = -1,
USER_SPECIFIC = 0,
ASCII = 4
} ProtocolType;

typedef struct PrivateData {
char vendor[32];
unsigned short length;
char data[1024];
} PrivateData ;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top