convert CString to const WCHAR *

Posted on 2004-08-21
Medium Priority
Last Modified: 2013-11-20
I'm using CFileDialog to let the user open a file and it returns a CString with the file name.
I want to use that file name to load an Image (Class), but the Image(Class) parameter uses const WCHAR *

If I use this:

Image DiskImage;

The compiler returns an C2664 error: can't convert parameter one from 'CString' to const WCHAR *
Whats wrong?
Question by:juanjo_dic
  • 2
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11859931
I think you need A2CW() macro:

#include "afxpriv.h"


Take a look to this MSDN tech note:

Author Comment

ID: 11860006
It doesn´t work. It throws a bunch of errors.
LVL 86

Expert Comment

ID: 11860149

CString str = dlgFile.GetPathName();
int nLen = str.GetLength() + 1;
WCHAR* pwsz = new WCHAR [ nLen];

mbstowcs ( pwsz, (LPTSTR) str, nLen);


delete [] pwsz;
LVL 55

Accepted Solution

Jaime Olivares earned 2000 total points
ID: 11860192
hmm, needs some configurations, to simplify, you can build your own function:

ConvertStringToWChar (LPCTSTR str, WCHAR *buffer)
       for (i=0; str[i]; i++)
             buffer[i] = (WCHAR)str[i];
       buffer[i] = 0;

use it as:

Image DiskImage;
ConvertStringToWChar(dlgFile.GetPathName(), path);

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
If you are looking for an automated solution for backup single or multiple Office 365 user mailboxes to Outlook data file, then you can use Kernel Office 365 Backup & Restore tool. Go through the video to check out the steps to backup single or mult…

624 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