Conversion of String^ to const char* in C++/CLI

Posted on 2006-04-26
Last Modified: 2008-01-09

I am trying to figure out how to convert a variable of type String^ to a const char* in C++/CLI.

I tried this, but it doesn't seem to do the job:

array<Char>^ inputFile = inputFileName->ToCharArray();

Question by:Zaynul
    LVL 19

    Assisted Solution

    by:Daniel Van Der Werken
    I might not be understanding this issue:

          System::String ^inputFileName = gcnew System::String("AFileName.TXT");

          array<System::Char> ^inputFile = inputFileName->ToCharArray();

    Works for me.  What exacly is "doing the job" here?


    Author Comment

    Sorry, what I meant was that that statement compiles, but I need "inputFile" to be of type const char* because I need to pass it into a C++ method that takes a const char* as a parameter.  When I run the following code:
    String^ inputFileName = "blahblah";
    String^ outputFileName = "yadayada";

    array<Char>^ inputFile = inputFileName->ToCharArray();
    array<Char>^ outputFile = inputFileName->ToCharArray();

    myObject->PerformScan(inputFile, outputFile);

    //PerformScan is defined like this: PerformScan(const char *originalFileName,  const char *outputFileName)
    I get the error below:

    Error      2      error C2664: 'MyClass::PerformScan' : cannot convert parameter 1 from 'cli::array<Type> ^' to 'const char *'      ...

    Thanks Dan.

    LVL 48

    Accepted Solution

    String^ inputFileName = "blahblah";

    IntPtr ptr = Marshal::StringToHGlobalAnsi(inputFileName);



    Author Comment

    Thanks AlexFM.

    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    tripleUp challenge 7 55
    countClumps  challenge 10 70
    Choosing the right language for new project 8 49
    Copy Files - Python 7 32
    Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
    Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now