I'm a bit confused how to get a char * Bar[]
of an array<String^>^
.
My unmanaged function look's kinda this:
void NativeClass::Foo(char * Bar[])
{
SomeAPIFunction(Bar);
}
Managed Part (should only give an idea of what i am trying to achieve):
void ManagedClass::Foo(array<String^>^ Bar)
{
NativeClass * MyNativeClass = new NativeClass();
MyNativeClass->Foo(Bar);
}
pin_ptr
. Can't get it to work. String^
to char *
i use this: char * NewFoo = (char*)(void*)Marshal::StringToHGlobalAnsi(Foo);
. Maybe there is an opportunity to go in such a way?SomeAPIFunction(Bar)
.Bar
is not fixed.There are utility methods to convert a single string for you, but for the array, you'll need to do it yourself.
This is what I would do: First, convert to C++ classes, because they're convenient and handle most of the memory allocation that you need to deal with. Then, create the char*[]
to point at the C++ objects.
void ManagedClass::Foo(array<String^>^ managedArray)
{
std::vector<std::string> vec;
for each (String^ managedStr in managedArray)
vec.push_back(marshal_as<std::string>(managedStr));
char** unmanagedArray = new char*[vec.size()];
for (size_t i = 0; i < vec.size(); i++)
unmanagedArray[i] = vec[i].c_str();
NativeClass * MyNativeClass = new NativeClass();
MyNativeClass->Foo(unmanagedArray);
delete[] unmanagedArray;
}
Disclaimer: I'm not at a compiler at the moment, there may be minor syntax errors.