Solved

URGENT  Convert a CString to WCHAR

Posted on 2004-04-16
12
2,172 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
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

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: 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. …
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.

777 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