URGENT Convert a CString to WCHAR

Hi

I have a CString m_szFileName, How do I put it into
WCHAR* Filename
Dj_Fx8Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
leflonConnect With a Mentor Commented:
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
 
leflonCommented:
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
 
AlexFMCommented:
#include <atlbase.h>


USES_CONVERSION;

WCHAR s[100];

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

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Dj_Fx8Author Commented:
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
 
Dj_Fx8Author Commented:
Sorry leflon, I just reread youd code, Now I see it
0
 
leflonCommented:
hui,
needed a second look myself :)

leflon
0
 
Dj_Fx8Author Commented:
Hi leflon

I got your code working, I don't quite understand it but it works
0
 
Dj_Fx8Author Commented:
Thanks leflon, the points are yours
0
 
leflonCommented:
thanks for the grade!

cheers
leflon
0
 
Dj_Fx8Author Commented:
No problem, I have another question I'm about to post, you may be able to help with it too
0
 
Dj_Fx8Author Commented:
Doh! I just figured it out
0
 
leflonCommented:
sweet taste of success :-)

cheers again
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.