473,408 Members | 2,477 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,408 software developers and data experts.

Problem with importing nested C struct with char*

Need help quick!!! Am really stuck on this problem!

I have a C dll, and it uses a nested struct. (see below) The struct has 2
layers of nested structs. The first field in the first struct is a char*.
I used StructLayout.Sequential to port each of the structs to C#, and I used
UnmanagedType.LPStr to marshal the char* to C# string, but the resulting
struct data are stored wrong when passed into the dll's api call. The char*
is returning ok because it's at the first field, but none of the other
fields are correct. One field actually showed up where another field should
have been in the assembly. The results are the same if I don't use
UnmanagedType.LPStr marshaling. Can someone explain why my coding have
things all messed up? Is there a "right" way to do what I am doing?

C struct:
typedef struct Control
{
struct
{
char *version;

struct
{
long version;
long option;
} mystruct3;

} mystruct1;

struct
{
long version;

struct
{
long version;
char revision;
long subRevision;
} mystruct4;

} mystruct2;
}

C#:
[StructLayout(LayoutKind.Sequential)]
public struct Control
{
public s1 mystruct1;
public s2 mystruct2;
}

[StructLayout(LayoutKind.Sequential)]
public struct s1
{
[MarshalAs(UnmanagedType.LPStr)]
public string version;
public s3 mystruct3;
}

[StructLayout(LayoutKind.Sequential)]
public struct s3
{
public int version;
public int option;
}

[StructLayout(LayoutKind.Sequential)]
public struct s2
{
public int version;
public s4 mystruct4;
}

[StructLayout(LayoutKind.Sequential)]
public struct s4
{
public int version;
public byte revision;
public int subRevision
}
Nov 15 '05 #1
0 1326

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

Similar topics

11
by: Alfonso Morra | last post by:
Hi, I have the ff data types : typedef enum { VAL_LONG , VAL_DOUBLE , VAL_STRING , VAL_DATASET }ValueTypeEnum ;
14
by: ser | last post by:
hi this is the code that i have but i seem to have 2 errors and i dont understand were they are so can some one please help me. thanks this is the code #include<iostream.h> #include<conio.h> ...
6
by: Computer Wizard | last post by:
Hello, I am really scrwed up with the following stuff: struct employee { char* employee_name; }; struct department {
2
by: samba | last post by:
I having a nested structures which are referenced with pointer variables #include <stdio.h> struct test { int i; char *mytest; }; struct sample {
3
by: Amit_Basnak | last post by:
Dear Friends I have the follwoing function "tss_fe_get_own_info" which has the arguments as shows below tss_fe_get_own_info(char *user_id, tss_user_profile_t **p_buf_UserSecInfo, error_status_t...
2
by: yannick.beot | last post by:
Hi, I have a problem with nested namespace in a schema. I defined a schema A. A is importing a schema B with the namespace nsB and a schema C with the namespace nsC. But both B and C are...
3
by: Vasu | last post by:
Hi! Can anybody there help me in analysis of the following code, which is a structure of customer's details, asks user to fill in the no. of customers and then their details. When I put in no. of...
4
by: Sheldon | last post by:
Hi, I have a unique case where I need an array of structs that grows and within this array is another struct that grows in some cases. I'm having trouble allocating memory. Since I have never...
5
by: bitstreamer | last post by:
Greetings. The emulation fever has conquered all kinds of Internet communities, and hence I've decided to write a tool in C which can read and modify a disk image. (However, I won't tell which...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.