Marshalling char** in p/invoke?

G

Guest

Howdy,

I have a legacy DLL for which I have a problem marshalling a parameter type
of char**.

The function header (C++) is as so;

extern "C" __declspec(dllexport) int __stdcall GetChildren(GetChildrenParm
*, Results *);

typedef struct _GetChildrenParm {
char *id_parent;
int num_results_max;
} GetChildrenParm;

typedef struct _Results {
char **id_object;
int num_results;
} Results;


My current C# code looks like;

[DllImport("objapi.dll")]
extern static int GetChildren (ref GetChildrenParm inParam, ref Results
outParam);

struct GetChildrenParm
{
string id_parent;
int num_results_max;

public ObjGetFolderChildrenParm(
string _id_parent,
int _num_results_max)
{
id_parent = _id_parent;
num_results_max = _num_results_max;
}
};


Marshalling the GetChildrenParm (inParam) is no problem, but marshalling the
Results (outParam) is giving me a headache due to the char** datatype within
the structure.

Any ideas how I can make this work? I can't find any examples anywhere of
Marshalling a char** type, let alone one within a struct.... ???
 
L

Light

I had the same problem. The advice given to me is to use unsafe code
and ptr or intptr but I haven't seen an example. If you get this
working then I'd like to see it.
Curtis
http://www.ghostclip.com
The Premier Help System For Developers
 
G

Guest

In the end, I've decided to use the Marshal class with IntPtr's to manually
translate the data. Since this is an out parameter, and the DLL I'm calling
into does the memory allocation, this works. My final solution involves
building a simple implementor for the ICustomMarshaler interface. It works
for now, so that'll do. :p

I'm not sure it will be applicable for all cases though...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top