473,378 Members | 1,493 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,378 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 10003
"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);"...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

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.