473,395 Members | 1,696 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,395 software developers and data experts.

How to pass strings array from C++/CLI to unmanaged C++

Hi All,

I need to pass array of strings from C++/CLI to unmanaged C++ function.

(The unmanaged API signatire is : int Combine(int NumOfInputFiles,
wchar_t **names) and I want to call it from C++/CLI code by using C++
Interop )
But unfortunately I have only one day experience in C++/CLI so all my
attempts are failing.

I'm trying something like that:

array<String^> ^fileNames = gcnew array<String^>(3);
fileNames[0]="vladi";
fileNames[1]="anna";

//convert .net string to wchar_t ptr
pin_ptr<const wchar_t> wch = PtrToStringChars(str);

then I call unmanged code (which's in separate dll)
int retVal = Combine(3, &wch);

But it fails in compilation.

Please help !

Thank you in advance!!

Feb 1 '06 #1
6 10260
vladislavf wrote:
//convert .net string to wchar_t ptr
pin_ptr<const wchar_t> wch = PtrToStringChars(str);

then I call unmanged code (which's in separate dll)
int retVal = Combine(3, &wch);

But it fails in compilation.


pin_ptr<wchar_t> is a pointer by itself. You don't have to take its
address. So I would say try

Combine(3, wch);

If you still have a problem, we have to see the exact error message.

Tom
Feb 1 '06 #2
Hi Tom,

Thank you for your answer.
If I'm removing '&' then I have the following error:

Error 24 error C2664: 'Combine' : cannot convert parameter 2 from
'cli::pin_ptr<Type>' to 'wchar_t
**' c:\Code\DotNet\Csharp2CLI\Csharp2CLI\Csharp2CLI.cp p 34

The point is : an unmanged API expects to have its second parameter to
be **w_char_t
And I don't know how to cast the .NET array<String^> to the
**w_char_t?

THANK YOU!
Vladi

Feb 2 '06 #3

"vladislavf" <vl**********@nice.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
| Hi Tom,
|
| Thank you for your answer.
| If I'm removing '&' then I have the following error:
|
| Error 24 error C2664: 'Combine' : cannot convert parameter 2 from
| 'cli::pin_ptr<Type>' to 'wchar_t
| **' c:\Code\DotNet\Csharp2CLI\Csharp2CLI\Csharp2CLI.cp p 34
|
| The point is : an unmanged API expects to have its second parameter to
| be **w_char_t
| And I don't know how to cast the .NET array<String^> to the
| **w_char_t?
|
| THANK YOU!
| Vladi
|
Not sure why you would need to pass managed strings and string arrays by
reference, managed strings are immutable so you can't safely change them
anyway. What you should do is marshal the strings to/from unmanaged.

Willy.
Feb 2 '06 #4
Hi Willy,
Thank you for your help.

This is exactly what I'd like to know : how to marshal the strings to
unmanaged .

Thank you,
Vladi

Feb 2 '06 #5

"vladislavf" <vl**********@nice.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
| Hi Willy,
| Thank you for your help.
|
| This is exactly what I'd like to know : how to marshal the strings to
| unmanaged .
|
| Thank you,
| Vladi
|

Hmmm.. I guess there are many many ways to do this, here is one of them...

[unmanaged C++]
void PassAnArray(wchar_t* a[], int c) {
for (int i=0; i<c; i++)
printf_s(" %ls\n", *a++);
}
[C++/CLI]
array<String^>^ strs= gcnew array<String^>(3);
strs[0] = "test1";
strs[1] = "test2";
strs[2] = "test3";

wchar_t * strings[strs.Length];
int elem = 0;
for each(String^ s in nums)
{
strings[elem++] =
static_cast<wchar_t*>(Marshal::StringToHGlobalUni( s).ToPointer());
}
PassAnArray(strings, strs.Length);
// Need to "FreeHGlobal" the alloc'd memory when done with it!!!

To marshal ther array back, you need to use Marshal::PtrToStringUni or one
of it's variants depending on the char encoding.

Willy.
Feb 2 '06 #6
Thank you for your help. It's working.

Feb 2 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the...
8
by: TJ | last post by:
I need to be able to pass a pointer to a (managed code) Stream object to a COM method so it can serialize its data into the stream. I have a C# application that makes uses of Randolf Duke's...
5
by: apm | last post by:
Any and all: Is there an efficient way to pass a large array from .NET to COM? Can references (or pointer) be passed from COM to NET and NET to COM without the object it refers to being copied?...
7
by: Klaus Bonadt | last post by:
I have an existing VC6 application using the MFC. I am able to pass CString and other parameters from such a VC6 dll to an unmanaged MFC dll (compiled in Visual Studio .NET). Now I want to use...
0
by: songie D | last post by:
How can I pass an array from an unmanaged function to a managed one Say I've go #pragma unmanage int GetArray(int* return_array) //return number of member return_array = 1 return_array = 2...
7
by: Bob Rock | last post by:
Hello, converting from the managed to the unmanaged world (and viceversa strings) and byte arrays is something I do often and I'd like to identify the most correct and efficient way to do it....
4
by: kimberly.walker | last post by:
Im very new to coding in C++ so use to coding in C#. My question is how to pass some values (string) from unmanaged code to managed code. I have two source files on a win32 console application one...
9
by: =?Utf-8?B?U2hhcm9u?= | last post by:
In my managed code (C#) I have a some dozens of single dimension byte array. I get each of this arrays from another unmanaged code (first DLL). I need to stitch these arrays to a dual dimensions...
1
by: vipinsoni | last post by:
Hi all, I am stucked with a problem. I have to paas a System.Array of object to my unmanaged C code as a void* I have a class of 'PointZ' which stores x, y,z coordinate and have some methods.I...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.