469,271 Members | 1,711 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

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

Hi,

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

cheers

Mark
Nov 17 '05 #1
5 9717
"Mark Ingram" <ma*********@nexsan.com> wrote in message
news:Sk*********************@newsfep2-win.server.ntli.net...
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


Nov 17 '05 #2
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#?
Nov 17 '05 #3
"Skute" <mark.ingram@[nospam.]nexsan.com> wrote in message
news:85**********************************@microsof t.com...
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[i] = 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[i] = 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[i]);
}
}
}

Regards,
Will

P.S. I'll be a happy camper when a single managed executable can contain
modules written in .Net different languages.



Nov 17 '05 #4
Thanks for quick reply, ive saved the text and ill have a run through it shortly. Cheer

Mark
Nov 17 '05 #5
Just tried out your code and it worked great, just what im looking for. Thanks alot!
Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by DG is a god.... | last post: by
11 posts views Thread by =?Utf-8?B?RGF0ZWxNb25rZXk5OQ==?= | last post: by
5 posts views Thread by =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.