Need help converting/marshalling char** from c++ dll to c# string array

M

MS

I have a dll with function exports I need to import into a c# app. The
signature of the c++ function is this:

extern char** WINAPI Flex_GetFeatureList(LM_HANDLE* hJob, int fSearchAll);

I am importing this in my C# app with this declaration:

[System.Runtime.InteropServices.DllImportAttribute(DllName, EntryPoint =
"Flex_GetFeatureList")]
public static extern System.IntPtr Flex_GetFeatureList(IntPtr hJob, Int32
fSearchAll);

When I try to read this into a string array in c#, I don't get anything that
looks right. It is just a string of garbled characters.



Can someone give me some pointers. I am really stuck.

Adrian
 
A

Arne Vajhøj

MS said:
I have a dll with function exports I need to import into a c# app. The
signature of the c++ function is this:

extern char** WINAPI Flex_GetFeatureList(LM_HANDLE* hJob, int fSearchAll);

I am importing this in my C# app with this declaration:

[System.Runtime.InteropServices.DllImportAttribute(DllName, EntryPoint =
"Flex_GetFeatureList")]
public static extern System.IntPtr Flex_GetFeatureList(IntPtr hJob, Int32
fSearchAll);

When I try to read this into a string array in c#, I don't get anything that
looks right. It is just a string of garbled characters.

If we assume that the function returns 3 strings then try something
like:

IntPtr p = ...;
IntPtr p0 = Marshal.ReadIntPtr(p, 0);
IntPtr p1 = Marshal.ReadIntPtr(p, 4);
IntPtr p2 = Marshal.ReadIntPtr(p, 8);
string s0 = Marshal.PtrToStringAnsi(p0);
string s1 = Marshal.PtrToStringAnsi(p1);
string s2 = Marshal.PtrToStringAnsi(p2);

there are obviously a bazillion variants, but ...

Arne
 
M

MS

Thanks Arne

That clarified the whole thing up for me. I have it working now

Adrian

Arne Vajhøj said:
MS said:
I have a dll with function exports I need to import into a c# app. The
signature of the c++ function is this:

extern char** WINAPI Flex_GetFeatureList(LM_HANDLE* hJob, int
fSearchAll);

I am importing this in my C# app with this declaration:

[System.Runtime.InteropServices.DllImportAttribute(DllName, EntryPoint =
"Flex_GetFeatureList")]
public static extern System.IntPtr Flex_GetFeatureList(IntPtr hJob, Int32
fSearchAll);

When I try to read this into a string array in c#, I don't get anything
that looks right. It is just a string of garbled characters.

If we assume that the function returns 3 strings then try something
like:

IntPtr p = ...;
IntPtr p0 = Marshal.ReadIntPtr(p, 0);
IntPtr p1 = Marshal.ReadIntPtr(p, 4);
IntPtr p2 = Marshal.ReadIntPtr(p, 8);
string s0 = Marshal.PtrToStringAnsi(p0);
string s1 = Marshal.PtrToStringAnsi(p1);
string s2 = Marshal.PtrToStringAnsi(p2);

there are obviously a bazillion variants, but ...

Arne
 

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