#include <vcclr.h>
#include <vector>
#include <memory>
struct char_pp{
static std::auto_ptr<char> cstr(System::String^ s){
pin_ptr<const wchar_t> wch = PtrToStringChars(s);
size_t convertedChars = 0;
size_t sizeInBytes = ((s->Length + 1) * 2);
std::auto_ptr<char> ch(new char[sizeInBytes]);
errno_t err = wcstombs_s(&convertedChars, ch.get(), sizeInBytes, wch, sizeInBytes);
return ch;
}
std::vector<char*> v;
char_pp(array<System::String^>^ s){
v.reserve(s->Length);
try{
for each(System::String^ s in s){ v.push_back(cstr(s).release()); }
}
catch(...){
for(std::vector<char*>::iterator it=v.begin();it!=v.end();it++){ delete *it; }
throw;
}
}
~char_pp(){
for(std::vector<char*>::iterator it=v.begin();it!=v.end();it++){ delete *it; }
}
char** get(){return &v[0];}
};
void f(char* hogehoge[]){
for(char** it=hogehoge;*it!=NULL;it++){
printf_s("%s\n", *it);
}
}
int main(array<System::String^>^ args){
cli::array<System::String^> ^hoge = gcnew cli::array<System::String^>{L"a", L"b", L"c"};
char_pp p(hoge);
p.v.push_back(NULL); //NULL終端配列が必要な場合
f(p.get());
return 0;
}
I2luY2x1ZGUgPHZjY2xyLmg+CiNpbmNsdWRlIDx2ZWN0b3I+CiNpbmNsdWRlIDxtZW1vcnk+CgpzdHJ1Y3QgY2hhcl9wcHsKCXN0YXRpYyBzdGQ6OmF1dG9fcHRyPGNoYXI+IGNzdHIoU3lzdGVtOjpTdHJpbmdeIHMpewoJCXBpbl9wdHI8Y29uc3Qgd2NoYXJfdD4gd2NoID0gUHRyVG9TdHJpbmdDaGFycyhzKTsKCQlzaXplX3QgY29udmVydGVkQ2hhcnMgPSAwOwoJCXNpemVfdCBzaXplSW5CeXRlcyA9ICgocy0+TGVuZ3RoICsgMSkgKiAyKTsKCQlzdGQ6OmF1dG9fcHRyPGNoYXI+IGNoKG5ldyBjaGFyW3NpemVJbkJ5dGVzXSk7CgkJZXJybm9fdCBlcnIgPSB3Y3N0b21ic19zKCZjb252ZXJ0ZWRDaGFycywgY2guZ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