locked
how to convert String^ to char* RRS feed

  • Question

  • Hi, I am writing a piece of code to open a file and retrieves its name:

    OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;
    if ( openFileDialog1-> ShowDialog() == System::Windows:: Forms:: DialogResult:: OK )
            System:: String^ fileName = openFileDialog1->FileName;

    but later I realized that the String^ fileName can't be used as char* directly, the compiler says:
    Cannot convert a managed type to an unmanaged type

    Does anyone know how I can do the conversion?

    Thanks a lot!
    Thursday, September 13, 2007 8:13 PM

Answers

  • Look in the MSDN library index for PtrToStringChars().  Marshal.StringToHGlobalAnsi() works too.
    Friday, September 14, 2007 1:48 AM

All replies

  • Look in the MSDN library index for PtrToStringChars().  Marshal.StringToHGlobalAnsi() works too.
    Friday, September 14, 2007 1:48 AM
  • Thanks a lot! it worked!
    Friday, September 14, 2007 3:26 PM