473,218 Members | 1,904 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,218 software developers and data experts.

How to marshal char** in struct?

Hi.

I have struct in C++:

struct MY_STRUCT
{
int x;
int y;
char** arrNames; //array of strings
}

I have function GetMyStruct, which gives me pointer to this struct, but
I can't get the array of strings (MY_STRUCT.arrNames).

MY_STRUCT* GetMyStruct();

In C# :

public struct MY_STRUCT
{
public int x;
public int y;
public string[] arrNames; //array of strings
}

public class MyWrapper
{
[DllImport("MyDll.dll")]
private static extern IntPtr GetMyStruct();
public static MY_STRUCT GetMyStructW()
{
IntPtr p = GetMyStruct();

return (MY_STRUCT) Marshal.PtrToStructure(p, typeof(MY_STRUCT));
}
}
BUT MyWrapper.GetMyStructW() doesn't work. Plllease help.

Jan 16 '06 #1
1 4712
jurot,

There are a few problems with this.

First, to marshal your struct, you will have to declare the arrNames
field as an IntPtr, and then marshal the values manually. You will have a
problem because you don't have any indication (unless you have a null entry
in the array) of when the array ends.

Also, when you are returning the pointer to the structure in
GetMyStruct, you are not deallocating the memory. You need to do this
(using the appropriate method that corresponds to how you allocated it).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"jurot" <jt*****@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Hi.

I have struct in C++:

struct MY_STRUCT
{
int x;
int y;
char** arrNames; //array of strings
}

I have function GetMyStruct, which gives me pointer to this struct, but
I can't get the array of strings (MY_STRUCT.arrNames).

MY_STRUCT* GetMyStruct();

In C# :

public struct MY_STRUCT
{
public int x;
public int y;
public string[] arrNames; //array of strings
}

public class MyWrapper
{
[DllImport("MyDll.dll")]
private static extern IntPtr GetMyStruct();
public static MY_STRUCT GetMyStructW()
{
IntPtr p = GetMyStruct();

return (MY_STRUCT) Marshal.PtrToStructure(p, typeof(MY_STRUCT));
}
}
BUT MyWrapper.GetMyStructW() doesn't work. Plllease help.

Jan 16 '06 #2

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

Similar topics

0
by: William Stacey | last post by:
The following code works, but I can't figure out why. I take a struct with two members, a single byte and byte. I then marshal the whole struct to a byte. I create a new struct (without init'ing...
1
by: alfacom | last post by:
Hi, I have two C++ structures like these : typedef struct answer_series_item { int32_t contract_size_i; int32_t price_quot_factor_i; char ins_id_s ; char isin_code_s ; uint8_t suspended_c;
5
by: Daniel Brown | last post by:
I am coding a managed C# wrapper for an unmanaged C DLL and I am unable to marshal a structure that contains an array of structures. When executed, the following code throws an ArgumentException...
6
by: Howard Kaikow | last post by:
Given: private struct PROCESSENTRY32 { public int dwSize; public int cntUsage; public int th32ProcessID; public int th32DefaultHeapID; public int th32ModuleID;
2
by: RYoung | last post by:
Given this native struct: typedef struct vendor { char name; } VENDOR I want to make managed equivalent, so I did this: public value struct Vendor
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
1
by: Goran | last post by:
Hi all! I need to pass managed String from to C-style APIs. I see I can use Marshal::StringToXXX functions. Is this the best we have? I understand this will allocate a new string and create copy...
0
by: krascso | last post by:
Hi everybody. I have a structure in unmanaged C++: struct TLogonParams { char name; char pswd; char alias;
0
by: Charming12 | last post by:
Hi All, I have a strange problem and due to my inefficiency with IntPtr i am unable to figure it out. I have an structure something like: public struct Detail { public int age; public...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...

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.