Converting String into char *

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm using the VC++ 2005 beta. I've seen several other suggestions on the
message boards, but none even compiled for me....here is my solution:

args[0] is a managed String, and the first argument passed into my program.
It is supposed to be a filename. I have a legacy library which is unmanaged
code that requires an old c-style char array.

System::Text::ASCIIEncoding ascii_enc;
int len = ascii_enc.GetByteCount(args[0]->ToCharArray(),0,args[0]->Length);
array<unsigned char,1> ^fname_array = gcnew array<unsigned char,1>(len+1);
ascii_enc.GetBytes(args[0],0,args[0]->Length,fname_array,0);
char *fname = new char[len+1];
for (int i=0;i<len;i++)
fname = fname_array;
fname[len] = '\0';
old_library_call(fname);
delete fname;

1.) was there an easier way to do this?
2.) Is there anything inherently wrong with what I've done?
 
Well...that was actually quite helpful. The marshalling functions work...you
know it helps if you use the right namespace :)

On a side note, I think the context sensitive help in VC++ 2005 needs a
little work...

Thanks.
 
Back
Top