PC Review


Reply
Thread Tools Rate Thread

C# data structures

 
 
KeithR
Guest
Posts: n/a
 
      18th Jan 2007
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?

 
Reply With Quote
 
 
 
 
DeveloperX
Guest
Posts: n/a
 
      18th Jan 2007
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


KeithR wrote:
> 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?


 
Reply With Quote
 
DeveloperX
Guest
Posts: n/a
 
      18th Jan 2007
This one is good too

http://www.codeproject.com/csharp/cs...select=1180022


DeveloperX wrote:
> 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
>
>
> KeithR wrote:
> > 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?


 
Reply With Quote
 
KeithR
Guest
Posts: n/a
 
      20th Jan 2007
DeveloperX wrote:

> 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/z6cfh6e

(VS.80).aspx#cpcondefaultmarshalingforarraysanchor3
>
>

Thank you for that I will check out the references.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
converting tabular structures in a Word document into an actual tableor reading data from the tabular structures using VBA code s Microsoft Word Document Management 9 3rd Jun 2010 11:30 PM
data structures =?Utf-8?B?dml2bWFoYQ==?= Microsoft Excel Programming 2 26th Jun 2007 10:18 PM
Two Structures, Same Data Dennis Woos Microsoft VB .NET 1 30th Jun 2006 05:18 AM
Data structures =?Utf-8?B?ZnJlZGR5?= Microsoft C# .NET 2 20th Nov 2004 11:13 PM
Data Structures =?Utf-8?B?ZnJlZGR5?= Microsoft C# .NET 2 15th Nov 2004 10:15 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:29 AM.