Solved

URGENT  Convert a CString to WCHAR

Posted on 2004-04-16
12
2,145 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
  • 6
  • 5
12 Comments
 
LVL 7

Expert Comment

by:leflon
Comment Utility
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
Comment Utility
#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
Comment Utility
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
 

Author Comment

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

Expert Comment

by:leflon
Comment Utility
hui,
needed a second look myself :)

leflon
0
 

Author Comment

by:Dj_Fx8
Comment Utility
Hi leflon

I got your code working, I don't quite understand it but it works
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 7

Accepted Solution

by:
leflon earned 500 total points
Comment Utility
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
Comment Utility
Thanks leflon, the points are yours
0
 
LVL 7

Expert Comment

by:leflon
Comment Utility
thanks for the grade!

cheers
leflon
0
 

Author Comment

by:Dj_Fx8
Comment Utility
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
Comment Utility
Doh! I just figured it out
0
 
LVL 7

Expert Comment

by:leflon
Comment Utility
sweet taste of success :-)

cheers again
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 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

10 Experts available now in Live!

Get 1:1 Help Now