Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

URGENT  Convert a CString to WCHAR

Posted on 2004-04-16
12
Medium Priority
?
2,228 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
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!

 

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 2000 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

Technology Partners: 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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Screencast - Getting to Know the Pipeline
Suggested Courses

927 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