Link to home
Start Free TrialLog in
Avatar of Dj_Fx8
Dj_Fx8

asked on

URGENT Convert a CString to WCHAR

Hi

I have a CString m_szFileName, How do I put it into
WCHAR* Filename
Avatar of leflon
leflon
Flag of Germany image

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
Avatar of AlexFM
AlexFM

#include <atlbase.h>


USES_CONVERSION;

WCHAR s[100];

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

Avatar of Dj_Fx8

ASKER

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
Avatar of Dj_Fx8

ASKER

Sorry leflon, I just reread youd code, Now I see it
hui,
needed a second look myself :)

leflon
Avatar of Dj_Fx8

ASKER

Hi leflon

I got your code working, I don't quite understand it but it works
ASKER CERTIFIED SOLUTION
Avatar of leflon
leflon
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dj_Fx8

ASKER

Thanks leflon, the points are yours
thanks for the grade!

cheers
leflon
Avatar of Dj_Fx8

ASKER

No problem, I have another question I'm about to post, you may be able to help with it too
Avatar of Dj_Fx8

ASKER

Doh! I just figured it out
sweet taste of success :-)

cheers again