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.

 Answer

Apart from the problem with pinned pointers going out of scope before being passed tosomeNativeFunction(), 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 andreinterpret_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 untilsomeNativeFunction() 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.

转自:http://stackoverflow.com/questions/7190987/conversion-of-c-cli-array-of-strings-to-native-c-char

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值