Solved

convert CString to const WCHAR *

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
bobThere java chalenge 45 96
difference between String.subString() and String.subSequence() 6 269
Not needed 13 129
Bartender label printing - switch on and off graphics 3 74
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

733 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