473,239 Members | 1,584 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,239 software developers and data experts.

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 9992
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
2
by: vikas | last post by:
I have following structure in c++. typedef struct MMF_result_struct { int action; char text; int cols,rows; int month,day,year; } MMF_result; Now this structure is shared between C++ and C#...
7
by: sienaman | last post by:
I have a C# dll with a COM interface that is successfully call by a C++ client. One of the parameters is a string, the method looks like void Goofy(string strUserInput, out string strOutput); ...
1
by: joye | last post by:
Hello, How to convert an unmanaged string with char array type to a managed string with char array type? Thanks. Regards, Tsung-Yu
3
by: DG is a god.... | last post by:
Dear All , This is my first post - please go easy...! I have a DLL written in C++ that has the following function exported from it -: char** ListHandles(int *processID); The processID...
0
by: rkpatil | last post by:
have an unmanaged C++ DLL with following method: the DLL interface for unmanaged C++ is: SendCommand(void *h, char *c, char *d, char *b); the declaration in C# is: unsafe public static...
11
by: =?Utf-8?B?RGF0ZWxNb25rZXk5OQ==?= | last post by:
I have some c++ code that I am converting to C#. What I need to convert is the following: memcpy(&tmpshort, (pTmpDataIn+1), 2); This should copy two bytes of an char* to an int which then gets...
0
by: anuptosh | last post by:
Hi, I have been trying to run the below example to get a Oracle Array as an output from a Java code. This is an example I have found on the web. But, the expected result is that the code should...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.