Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I marshal a pointer to a series of null-terminated strings in C#?
    text
    copied!<p>I need some help with the following. I've got a c++ API (no access to source) and I'm struggling with the methods returning char* attributes, or returned structures containing char* attributes. According to the API's documentation the return value is as follows: </p> <p>Return Values<br> If the function succeeds, the return value is a pointer to a series of null-terminated strings, one for each project on the host system, ending with a second null character. The following example shows the buffer contents with <code>&lt;null&gt;</code> representing the terminating null character:</p> <pre><code>project1&lt;null&gt;project2&lt;null&gt;project3&lt;null&gt;&lt;null&gt; </code></pre> <p>If the function fails, the return value is NULL</p> <p>The problem I'm having is that the returned pointer in C# only contains the first value... project1 in this case. How can I get the full list to be able to loop through them on the managed side?</p> <p>Here's the c# code:</p> <blockquote> <pre><code> [DllImport("vmdsapi.dll", EntryPoint = "DSGetProjectList", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr DSGetProjectList(); </code></pre> </blockquote> <p>Calling method:</p> <blockquote> <pre><code> IntPtr ptrProjectList = DSAPI.DSGetProjectList(); string strProjectList = Marshal.PtrToStringAnsi(ptrProjectList).ToString(); </code></pre> </blockquote> <p>strProjectList only contains the first item.<br> Here's the info from the API's header file...</p> <blockquote> <pre><code> DllImport char *DSGetProjectList dsproto((void)); </code></pre> </blockquote> <p>Here's some sample code from a c++ console app which I've used for testing purposes...</p> <blockquote> <pre><code> char *a; a = DSGetProjectList( ); while( *a ) { printf("a=%s\n", a); a += 1 + strlen(a); } </code></pre> </blockquote> <p>Each iteration correctly displays every project in the list.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload