Solved

convert CString to const WCHAR *

Posted on 2004-08-21
4
5,423 Views
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;
DiskImage.FromFile(dlgFile.GetPathName());

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

Expert Comment

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

#include "afxpriv.h"

DiskImage.FromFile(A2CW(dlgFile.GetPathName()));

Take a look to this MSDN tech note:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_mfcnotes_tn059.asp
0
 

Author Comment

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

Expert Comment

by:jkr
ID: 11860149
Try

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

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

DiskImage.FromFile(pwsz);

delete [] pwsz;
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 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;
WCHAR path[MAX_PATH];
ConvertStringToWChar(dlgFile.GetPathName(), path);
DiskImage.FromFile(path);
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

705 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

18 Experts available now in Live!

Get 1:1 Help Now