471,330 Members | 1,637 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 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 4484
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by William Stacey | last post: by
1 post views Thread by alfacom | last post: by
5 posts views Thread by Daniel Brown | last post: by
6 posts views Thread by Howard Kaikow | last post: by
1 post views Thread by Goran | last post: by
reply views Thread by krascso | last post: by

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.