Solved

convert CString to const WCHAR *

Posted on 2004-08-21
4
5,515 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
while loop over for loop 7 115
NotAlone Challenge 20 84
sumHeights  challenge 17 74
wordmultiple challenge 12 141
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…
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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 a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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