#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;
}