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.