?
Solved

convert CString to const WCHAR *

Posted on 2004-08-21
4
Medium Priority
?
5,710 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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;
WCHAR path[MAX_PATH];
ConvertStringToWChar(dlgFile.GetPathName(), path);
DiskImage.FromFile(path);
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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…
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 video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

752 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