How to marshall an an array of strings between C# and a C++ dll?

R

Rajesh

Hi All,

I'm having problems marshalling an array of strings between C# and C++

C#

[DllImport"ABC.dll", EntryPoint = "Func",ExactSpelling = false)]
static extern void Func(out IntPtr num, ref IntPtr str);

IntPtr strPtr = IntPtr.Zero;
IntPtr num = IntPtr.Zero;
Func(out num, ref strPtr);
n = num.ToInt64();


C++ signature is
extern "C" __declspec(dllexport) void Func(long &num,BSTR * strings)

When I traced the code, I could see the strings array in C++ populated
correctly. But once in the C# side, I can only access the first string
with Marshal.PtrToStringBSTR(strPtr) I need to know if there is
something basic i'm missing out here. I also tried Marshal.ReadIntPtr(
) to traverse through the IntPtr but that didn't help.

Thanks.
Raj
 
M

Mattias Sjögren

C++ signature is
extern "C" __declspec(dllexport) void Func(long &num,BSTR * strings)

When I traced the code, I could see the strings array in C++ populated
correctly. But once in the C# side, I can only access the first string
with Marshal.PtrToStringBSTR(strPtr) I need to know if there is
something basic i'm missing out here.

The C++ signature is written in such a way that only a single pointer
will be returned. If you want to return an array of unknown size you
probably need another level of indirection.

BTW long maps to int in C#, not IntPtr (for the first parameter).


Mattias
 

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