简体   繁体   中英

Conversion of C++/CLI array of strings to native C++ char**

In C++/CLI, What's the most efficient way to convert an array of strings to native char**?

I am doing this:

array<String^>^ tokenArray = gcnew array<String^> {"TokenONE", "TokenTWO"};
int numTokens = tokenArray->Length;
char** ptr = new char* [numTokens];
for(int i = 0; i < numTokens; i++)
    {
        // See: http://stackoverflow.com/questions/6596242/
        array<Byte>^ encodedBytes = Text::Encoding::UTF8->GetBytes(tokenArray[i]);
        pin_ptr<Byte> pinnedBytes = &encodedBytes[0];
        ptr[i] = reinterpret_cast<char*>(pinnedBytes);
    }
int myResult = someNativeFunction(ptr, numTokens);
delete ptr;
// ...

What, if anything should be improved? Is this ok from a memory management point of view? I can change the parameters of someNativeFunction if need be.

Thank you.

Apart from the problem with pinned pointers going out of scope before being passed to someNativeFunction() , the code can be simplified for better clarity especially if you're using MSVC2008 or newer. See this page for information on how to convert a single string (extending to an array should be trivial).

Edited:

If you need ANSI strings const char* then making a copy is inevitable since .NET Strings are Unicode (UTF-16). On MSVC2008 and newer, your code may look as follows:

#include <msclr/marshal.h>
using namespace msclr::interop;

marshal_context context;
array<String^>^ tokenArray = gcnew array<String^> {"TokenONE", "TokenTWO"};
char** tokensAsAnsi = new char* [tokenArray->Length];

for(int i = 0; i < tokenArray->Length; i++)
{
    tokensAsAnsi[i] = context.marshal_as<const char*>(tokenArray[i]);
}
int myResult = someNativeFunction(ptr, tokensAsAnsi);

// The marshalled results are freed when context goes out of scope
delete[] tokensAsAnsi;    // Please note you must use delete[] here!

This does similar job to your code sample but without the need of pointer pinning and reinterpret_cast -ing.

If you are willing to deal with wide string const wchar_t* in someNativeFunction() , you can use the (pinned) internal data directly, However, you'll have to ensure the pointers remain pinned until someNativeFunction() returns which, as pointed out in the comments, may negatively influence the GC performance.

If you're about to marshall many strings and performance is of utmost concern, you could split the marshalling across several threads before passing everything to someNativeFunction() . Before doing that, I'd reccommend profiling your application to see if the conversion really is a bottleneck or whether it's better to focus efforts elsewhere.

Edited #2:

To get the native string in UTF-8 encoding, you can do with a modified version of your code:

array<String^>^ tokenArray = gcnew array<String^> {"TokenONE", "TokenTWO"};
char** tokensAsUtf8 = new char* [tokenArray->Length];

for(int i = 0; i < tokenArray->Length; i++)
{
    array<Byte>^ encodedBytes = Text::Encoding::UTF8->GetBytes(tokenArray[i]);

    // Probably just using [0] is fine here
    pin_ptr<Byte> pinnedBytes = &encodedBytes[encodedBytes->GetLowerBound(0)];

    tokensAsUtf8[i] = new char[encodedBytes->Length + 1]; 
    memcpy(
        tokensAsUtf8[i], 
        reinterpret_cast<char*>(pinnedBytes),
        encodedBytes->Length
        );

    // NULL-terminate the native string
    tokensAsUtf8[i][encodedBytes->Length] = '\0'; 

}
int myResult = someNativeFunction(ptr, tokensAsAnsi);

for(int i = 0; i < tokenArray->Length; i++) delete[] tokensAsUtf8[i];
delete[] tokensAsUtf8;    

If you're concerned about speed, you could pre-allocate a large buffer for the native strings (if you know there will only be a limited amount) or use pool storage.

Edited #3:(OG Dude) Just fixed some minor typos.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM