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

V

vladislavf

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!!
 
T

Tamas Demjen

vladislavf said:
//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
 
V

vladislavf

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.cpp 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
 
W

Willy Denoyette [MVP]

| 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.cpp 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.
 
V

vladislavf

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
 
W

Willy Denoyette [MVP]

| 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top