C# data structures


K

KeithR

I am something of a newbie in the world of C# data structures, but I am
writing an application that needs to call a legacy DLL using .net 1.1. That
is no big deal mostly, but I am having trouble with one function. It is
defined in the original C as

UINT32 __stdcall GetState(
UINT32 timeout,
UINT32 client_id,
void *status_array
);

status is defines as a pointer to 64 status records (structs) they are
defined as

typedef struct status_rec {
UINT32 type;
UINT32 state;
UINT32 version;
UINT32 flags;
CHAR ip_address[15];
CHAR filler[9];
} status, *p_status

I defined this in C# as

[StructLayout=LayoutType.Sequential, Pack=1, CharSet = CharSet.Ansii]
public struct status_rec {
public UINT32 type;
public UINT32 state;
public UINT32 version;
public UINT32 flags;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.U1,
SizeConst=15)]
public CHAR ip_address[15];
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.U1,
SizeConst=15)]
public CHAR filler[9];
}

I made my call as

status_rec[] status_array = new status_rec[64];
GetState(
timeout,
client_id,
ref status_array[0]
);

What I am getting in Status_array is the correct data in the first element,
and all zeroes thereafter.

Can anybody please point me to my mistake?
 
Ad

Advertisements

D

DeveloperX

This one is good too :)

http://www.codeproject.com/csharp/csharpripper.asp?df=100&forumid=26234&exp=0&select=1180022

I think this may be useful

http://www.codeproject.com/csharp/groupandmembers.asp

There is a difference in that the example code allocates the space for
the buffer in the p/invoked dll, but the use of sizeof and Marshal
class may help.

This is also has a good explanation.

http://msdn2.microsoft.com/en-us/library/z6cfh6e6(VS.80).aspx#cpcondefaultmarshalingforarraysanchor3

I am something of a newbie in the world of C# data structures, but I am
writing an application that needs to call a legacy DLL using .net 1.1. That
is no big deal mostly, but I am having trouble with one function. It is
defined in the original C as

UINT32 __stdcall GetState(
UINT32 timeout,
UINT32 client_id,
void *status_array
);

status is defines as a pointer to 64 status records (structs) they are
defined as

typedef struct status_rec {
UINT32 type;
UINT32 state;
UINT32 version;
UINT32 flags;
CHAR ip_address[15];
CHAR filler[9];
} status, *p_status

I defined this in C# as

[StructLayout=LayoutType.Sequential, Pack=1, CharSet = CharSet.Ansii]
public struct status_rec {
public UINT32 type;
public UINT32 state;
public UINT32 version;
public UINT32 flags;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.U1,
SizeConst=15)]
public CHAR ip_address[15];
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.U1,
SizeConst=15)]
public CHAR filler[9];
}

I made my call as

status_rec[] status_array = new status_rec[64];
GetState(
timeout,
client_id,
ref status_array[0]
);

What I am getting in Status_array is the correct data in the first element,
and all zeroes thereafter.

Can anybody please point me to my mistake?
 
Ad

Advertisements


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