fork download
  1. #include <vcclr.h>
  2. #include <vector>
  3. #include <memory>
  4.  
  5. struct char_pp{
  6. static std::auto_ptr<char> cstr(System::String^ s){
  7. pin_ptr<const wchar_t> wch = PtrToStringChars(s);
  8. size_t convertedChars = 0;
  9. size_t sizeInBytes = ((s->Length + 1) * 2);
  10. std::auto_ptr<char> ch(new char[sizeInBytes]);
  11. errno_t err = wcstombs_s(&convertedChars, ch.get(), sizeInBytes, wch, sizeInBytes);
  12. return ch;
  13. }
  14. std::vector<char*> v;
  15. char_pp(array<System::String^>^ s){
  16. v.reserve(s->Length);
  17. try{
  18. for each(System::String^ s in s){ v.push_back(cstr(s).release()); }
  19. }
  20. catch(...){
  21. for(std::vector<char*>::iterator it=v.begin();it!=v.end();it++){ delete *it; }
  22. throw;
  23. }
  24. }
  25. ~char_pp(){
  26. for(std::vector<char*>::iterator it=v.begin();it!=v.end();it++){ delete *it; }
  27. }
  28. char** get(){return &v[0];}
  29. };
  30.  
  31. void f(char* hogehoge[]){
  32. for(char** it=hogehoge;*it!=NULL;it++){
  33. printf_s("%s\n", *it);
  34. }
  35. }
  36.  
  37. int main(array<System::String^>^ args){
  38. cli::array<System::String^> ^hoge = gcnew cli::array<System::String^>{L"a", L"b", L"c"};
  39. char_pp p(hoge);
  40. p.v.push_back(NULL); //NULL終端配列が必要な場合
  41. f(p.get());
  42. return 0;
  43. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty