By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,278 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,278 IT Pros & Developers. It's quick & easy.

PInvoke "Struct" Problem

P: n/a
Structure in C DLL
----------------------------
typedef struct IrData
{
unsigned short uiFormat;
unsigned short uiLength;
unsigned char* pchData;
} tagIrData;

Function in C DLL
----------------------------
short ConvertData(char* pchString, tagIrData* ptIrCode); ---This
function fills the allocated structure with the respective field
values.

How should we write the PInvoke for this ? I have tried the method
below (with both struct & class) but my structure elements go for a
toss. Please correct me.

[StructLayout(LayoutKind.Sequential)]
internal class IrData
{
public ushort IrFormat;
public ushort IrCodeLength;
public byte[] IrCodeData;
}

[DllImport("SomeDll.dll")]
private static extern short ConvertData(string strData, IrData
data);

Thx in Advance.

Jan 23 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
<an***************@gmail.comwrote in message
news:11**********************@l53g2000cwa.googlegr oups.com...
Structure in C DLL
----------------------------
typedef struct IrData
{
unsigned short uiFormat;
unsigned short uiLength;
unsigned char* pchData;
} tagIrData;

Function in C DLL
----------------------------
short ConvertData(char* pchString, tagIrData* ptIrCode); ---This
function fills the allocated structure with the respective field
values.

How should we write the PInvoke for this ? I have tried the method
below (with both struct & class)
You can use either. If using a class pass by value and it gets passed by
reference, if using a struct you need to pass by reference. I almost always
use class because you can add functions to the class etc. Once that is
working the only problem will be the char* but I think that will work as it
is. The only other thing to check would be the layout of your structure, use
the FieldOffset attribute to make sure it has the exact layout you require
(although I don't think this will be required).

Michael
Jan 23 '07 #2

P: n/a
I did this, but no luck !

[StructLayout(LayoutKind.Sequential)]
public struct IrData
{
public ushort IrFormat;
public ushort IrCodeLength;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256]
public byte[] IrCodeData;
}

[DllImport("SomeDLL.dll")]
private static extern short ConvertData(string strData, ref
IrData data);




Michael C wrote:
<an***************@gmail.comwrote in message
news:11**********************@l53g2000cwa.googlegr oups.com...
Structure in C DLL
----------------------------
typedef struct IrData
{
unsigned short uiFormat;
unsigned short uiLength;
unsigned char* pchData;
} tagIrData;

Function in C DLL
----------------------------
short ConvertData(char* pchString, tagIrData* ptIrCode); ---This
function fills the allocated structure with the respective field
values.

How should we write the PInvoke for this ? I have tried the method
below (with both struct & class)

You can use either. If using a class pass by value and it gets passed by
reference, if using a struct you need to pass by reference. I almost always
use class because you can add functions to the class etc. Once that is
working the only problem will be the char* but I think that will work as it
is. The only other thing to check would be the layout of your structure, use
the FieldOffset attribute to make sure it has the exact layout you require
(although I don't think this will be required).

Michael
Jan 23 '07 #3

P: n/a
igd
Replace 'struct' with 'class' and initialize at least the array (public
byte[] lrCodeData = new byte[256];)

an***************@gmail.com schrieb:
I did this, but no luck !

[StructLayout(LayoutKind.Sequential)]
public struct IrData
{
public ushort IrFormat;
public ushort IrCodeLength;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256]
public byte[] IrCodeData;
}

[DllImport("SomeDLL.dll")]
private static extern short ConvertData(string strData, ref
IrData data);
Jan 23 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.