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

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



MS wrote:
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
.



Relevant Pages

  • VirtualAllocEx returns a bad pointer in some processes
    ... (ByVal hWnd As IntPtr, _ ... (ByVal hWndParent As IntPtr, _ ... ByVal lParam As String) As Integer ... Dim aihWnds As ArrayList ...
    (microsoft.public.vc.mfc)
  • Virtual memory C++ problem
    ... (ByVal hWnd As IntPtr, _ ... (ByVal hWndParent As IntPtr, _ ... ByVal lParam As String) As Integer ... Dim aihWnds As ArrayList ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Unable to properly set the InteractiveProcess property on a windows service
    ... public static extern IntPtr ... IntPtr OpenSCManager(string lpMachineName, string ... OpenService(IntPtr hSCManager, string lpServiceName, ... installer code looks like this... ...
    (microsoft.public.dotnet.languages.csharp)
  • SetPrinter for network printers
    ... Public dmDeviceName As String ... Public pSecurityDescriptor As IntPtr ... Dim pPrinterInfo As IntPtr ...
    (microsoft.public.dotnet.languages.vb)
  • MAPISendMail Hangs
    ... Without breakpoint the app hangs forever on MAPISendMail. ... ByVal username As String, _ ... ByRef session As IntPtr) As Integer ... Private Structure MapiRecipient ...
    (microsoft.public.dotnet.framework.windowsforms)