By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,758 Members | 1,250 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,758 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a

"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

P: n/a
Thank you for your help. It's working.

Feb 2 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.