Returning char* / System.String array from unmanaged c++ dll into a c# application

  • Thread starter Thread starter Mark Ingram
  • Start date Start date
M

Mark Ingram

Hi,

how can i return an array of strings from an unmanaged c++ dll into a c#
application?

cheers

Mark
 
Mark Ingram said:
how can i return an array of strings from an unmanaged c++ dll into a c#
application?

I'm not sure what problem you are having. But if it is the same as I had, it
has to do with getting rid of the notion that an array is the same thing as
a pointer to its first element. In native C and C++, it is said that an
array can "decay into" a such pointer, in managed C++, that's not the case.

If that's the problem you need a function that looks something like this

String *getStrings()[];

If not, post again with some more detail and someone will likely help you
out.

Regards,
Will
 
Well i havent even got around to designing the function yet because i have no idea where to start! As you said though i was thinking about the pointer to the first item in an array but couldnt work out how to 'separate' the items as such.

Is there any chance you could give a definition of the function in unmanaged c++ and the DLLImport statement in c#?
 
Skute said:
Well i havent even got around to designing the function yet
because i have no idea where to start! As you said though
i was thinking about the pointer to the first item in an array
but couldnt work out how to 'separate' the items as such.

Is there any chance you could give a definition of the function
in unmanaged c++ and the DLLImport statement in c#?

Perhaps.

I'm to busy to right now to cook up anything but a minimalist sample. That's
especially so because the interoperation between managed and unmanaged code
is a large enough topic to warrant its own book. If the reference that Larry
gave you is the book by Challa and Laksberg, then I heartily recommend it.

Now, if your question has more to do with syntax it easy to cook up a
sample. Because I tend to follow the path of least resistance I created a
class in MC++ which makes consuming it from C# a trivial matter. And I'm
using "it just works" (aka IJW) to have managed code call unmanaged code
because it is so, so simple.

This is a quick hack that demonstrates how a method in an MC++ class can
return an array of managed strings and in the process call an unmanaged
function to get the strings' data:

#using <mscorlib.dll>

#pragma unmanaged

void getString(char *pBuf, char c, int size)
{
for ( int i = 0; i < size - 1; i++ )
pBuf = c;

pBuf[size - 1] = 0;
}

#pragma managed

public __gc class StringArrayTest
{
private:

int count;
System::String *stringArray __gc [];

public:

StringArrayTest(int c)
{
char buff __nogc [21];

count = c;
stringArray = new System::String __gc * [count];

for ( int i = 0; i < count; i++ )
{
getString(buff, i + '0', sizeof(buff));
stringArray = new System::String(buff);
}
}

System::String *getStrings()[]
{
return stringArray;
}
};

This is a quick hack of a C# console application that uses the MC++ class
above.

using System;
using System.IO;

namespace CSharpTest
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
int i;
StringArrayTest test = new StringArrayTest(5);

String [] array = test.getStrings();

for ( i = 0; i < 5; i++ )
Console.WriteLine("String #" + (i + 1) + " is " + array);
}
}
}

Regards,
Will

P.S. I'll be a happy camper when a single managed executable can contain
modules written in .Net different languages.
 
Thanks for quick reply, ive saved the text and ill have a run through it shortly. Cheer

Mark
 
Back
Top