473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

C# data structures

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?

Jan 18 '07 #1
3 1549
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/lib...rarraysanchor3
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?
Jan 18 '07 #2
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/lib...rarraysanchor3
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?
Jan 18 '07 #3
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#cpcondefaultmarshalingforarraysanchor 3
>
Thank you for that I will check out the references.
Jan 20 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: hzy_104 | last post by:
Please recommend book on data structures for searching(c++)?
1
by: Amit | last post by:
Hello, Can any of you recommend a really good book on data structures and more so, if it relates to STL data structures, and how they are used to build far more complex data structures. ...
5
by: el_roachmeister | last post by:
For being a good web programmer, is a course on data structures important? It seems php already has built-in functions for what they teach in a data structures course. On the other hand all...
4
by: Thomas Paul Diffenbach | last post by:
Can anyone point me to an open source library of /statically allocated/ data structures? I'm writing some code that would benefit from trees, preferably self balancing, but on an embedded system...
10
by: Bart Goeman | last post by:
Hi, I have a question about how to put redundant information in data structures, initialized at compile time. This is often necessary for performance reasons and can't be done at run time (data...
13
by: Leszek Taratuta | last post by:
Hello, I have several drop-down lists on my ASP.NET page. I need to keep data sources of these lists in Session State. What would be the most effective method to serialize this kind of data...
3
by: osp | last post by:
hi to every one.... i just started out with c++ and i think i am doing well.i use Robert Laffore to study. which book should i use for data structures ? please help. thank you with regards ...
11
by: efrat | last post by:
Hello, I'm planning to use Python in order to teach a DSA (data structures and algorithms) course in an academic institute. If you could help out with the following questions, I'd sure...
29
by: Mik0b0 | last post by:
Hallo to everyone. This fall I am going to start data structures as a part of C language course. The problem is I could not find any satisfying tutorial about structures in C. There are plenty of...
4
by: jehugaleahsa | last post by:
Hello: When developing data structures for C#, there is an obvious performance hit when utilizing primitive types. For instance, a recent hash table implementation I wrote works exceedingly fast...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.