1,105,312 Community Members

How to convert from string to "PCWSTR"?

Member Avatar
Newbie Poster
6 posts since Oct 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]


I'm trying to use a string (filepath) with the windows IFileOperation interface.
Only, the CopyItem method calls for the filepath as a PCWSTR.

I've been googling and trying different things for half an hour but for the life of me I can't find out how to convert this string. I can barely find anything on the net mentioning PCWSTR. Apparently it's a WCHAR*?

I'm using Multibyte, not Unicode - if that makes a difference.

Hopefully someone can shed some light on this.
In the meantime, I'm still looking, thanks.

Member Avatar
Posting Virtuoso
1,674 posts since May 2010
Reputation Points: 793 [?]
Q&As Helped to Solve: 231 [?]
Skill Endorsements: 24 [?]

You need to convert from MultiByte to WideChar. Before doing that though, see if there is a CopyItemA method. The A reverts back to the non-Unicode form of the method (and exists for a lot of Microsoft methods)

Otherwise, here's some example code.

wstringstream sstr;
configFile >> serialNumber;
size_t numberOfCharsConverted = 0;
size_t originalLength = strlen(serialNumber.c_str()) + 1;
TCHAR* lpwstrSerialNumberW = (TCHAR*)malloc(sizeof(TCHAR) * originalLength);
mbstowcs_s(&numberOfCharsConverted, lpwstrSerialNumberW, originalLength, serialNumber.c_str(), _TRUNCATE);
serialNumberW = lpwstrSerialNumberW;

Some people will use MultiByteToWideChar but I've had problems with this method adding nonsense characters onto the end of Strings.

This article has been dead for over three months: Start a new discussion instead
Start New Discussion
Tags Related to this Article