Solved

URGENT  Convert a CString to WCHAR

Posted on 2004-04-16
12
2,214 Views
Last Modified: 2013-11-20
Hi

I have a CString m_szFileName, How do I put it into
WCHAR* Filename
0
Comment
Question by:Dj_Fx8
[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
  • 6
  • 5
12 Comments
 
LVL 7

Expert Comment

by:leflon
ID: 10844612
Hi Dj_Fx8,

you may try this

CString str = "SomeFileName" ;
LPWSTR lpszW = new WCHAR[255];

LPTSTR lpStr = str.GetBuffer( str.GetLength() );
int nLen = MultiByteToWideChar(CP_ACP, 0,lpStr, -1, NULL, NULL);
MultiByteToWideChar(CP_ACP, 0, lpStr, -1, lpszW, nLen);

//do your stuff here

delete[] lpszW;

hth
leflon
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10844613
#include <atlbase.h>


USES_CONVERSION;

WCHAR s[100];

#ifdef _UNICODE
    _tcscpy(s, m_szFileName);
#else
    _tcscpy(s, A2W(m_szFileName));
#endif

0
 

Author Comment

by:Dj_Fx8
ID: 10844765
Hi

leflon -- Ummm Forgive me if I am in error but I fail to see where the WCHAR variable is at  in your code

Alex I get the following error for this line  _tcscpy(xx, A2W(m_szFileName));
error C2664: 'strcpy' : cannot convert parameter 1 from 'unsigned short [100]' to 'char *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
0
Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

 

Author Comment

by:Dj_Fx8
ID: 10844809
Sorry leflon, I just reread youd code, Now I see it
0
 
LVL 7

Expert Comment

by:leflon
ID: 10844836
hui,
needed a second look myself :)

leflon
0
 

Author Comment

by:Dj_Fx8
ID: 10844877
Hi leflon

I got your code working, I don't quite understand it but it works
0
 
LVL 7

Accepted Solution

by:
leflon earned 500 total points
ID: 10844976
i will try to explain it a little bit

// just dummy definitions to get it running
CString str = "SomeFileName" ;
LPWSTR lpszW = new WCHAR[255];  // the hidden WCHAR :-)

LPTSTR lpStr = str.GetBuffer( str.GetLength() );                                   // get the pointer to the underlying C-char buffer of the CString object
int nLen = MultiByteToWideChar(CP_ACP, 0,lpStr, -1, NULL, NULL);       // call to the conversion function to determine the required size of the buffer, used in the next call to func
// maybe it is better to allocate the lpszW here
// LPWSTR lpszW = new WCHAR[nLen];
MultiByteToWideChar(CP_ACP, 0, lpStr, -1, lpszW, nLen);                    // do the conversion to WCHAR

//do your stuff here

delete [] lpszW;                                                                               // free the dynamically allocated memory

hth
leflon
0
 

Author Comment

by:Dj_Fx8
ID: 10845055
Thanks leflon, the points are yours
0
 
LVL 7

Expert Comment

by:leflon
ID: 10845078
thanks for the grade!

cheers
leflon
0
 

Author Comment

by:Dj_Fx8
ID: 10845108
No problem, I have another question I'm about to post, you may be able to help with it too
0
 

Author Comment

by:Dj_Fx8
ID: 10845136
Doh! I just figured it out
0
 
LVL 7

Expert Comment

by:leflon
ID: 10845211
sweet taste of success :-)

cheers again
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

617 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