#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);
for each(System::String^ s in s){ v.push_back(cstr(s).release()); }
}
~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+IGNoKG5ldyBjaGFyW3NpemVJbkJ5dGVzXSk7CgkJZXJybm9fdCBlcnIgPSB3Y3N0b21ic19zKCZjb252ZXJ0ZWRDaGFycywgY2guZ2V0KCksIHNpemVJbkJ5dGVzLCB3Y2gsIHNpemVJbkJ5dGVzKTsKCQly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