CString array and char *

Posted on 2009-04-22
Last Modified: 2013-11-20
hi, I saved the file names in a CString array.  and I need to pass these file names to a function "someFunc" which takes const char* .  but the code does not work.  the error says:

cannot convert parameter 1 from 'CString' to 'const char *'      

I also try to cast it into char* but failed too.

Any suggestions?
#define MAX_IMAGE_NUMBER 1000

CString fileName[MAX_IMAGE_NUMBER];




          fileName[i] = (LPCTSTR)finder.GetFilePath();

          someFunc((const char*)fileName[i]);




Open in new window

Question by:CodingCat
    LVL 86

    Expert Comment

    You can do that by using a 'LPCTSTR' cast as you are already using, e.g.
              fileName[i] = (LPCTSTR)finder.GetFilePath();

    Open in new window

    LVL 86

    Accepted Solution

    BTW, in case you are compiling your app as UNICODE, you'll need to convert it to ANSI (char*) first, e.g.
              fileName[i] = (LPCTSTR)finder.GetFilePath();
              char tmp[MAX_PATH];

    Open in new window

    LVL 39

    Expert Comment

    CString has an operator LPCTSTR, which might be either wide (wchar_t *) or narrow (char *) depending upon your compile time settings. If you are building with Unicode enabled that your CString will be wide so there operator LPCTSTR will not be automatically invoked when you try and cast it to char *.

    Try this...


    But note, if somefunc is only narrow (char *) and LPCTSTR is wide (wchar_t *) then the call to the function will fail.

    You can convert wide to narrow using wcstombs

    Author Closing Comment

    It's the UNICODE issue again.

    Author Comment

    thanks for the link, evilrix.  wcstombs is necessary in my case.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Suggested Solutions

    In this article you will get to know about pros and cons of storage drives HDD, SSD and SSHD.
    Lithium-ion batteries area cornerstone of today's portable electronic devices, and even though they are relied upon heavily, their chemistry and origin are not of common knowledge. This article is about a device on which every smartphone, laptop, an…
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

    737 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

    22 Experts available now in Live!

    Get 1:1 Help Now