-
LPCSTR to LPCWSTR
I'm trying to perform the type conversion as shown in the subject title. Below shows the working copy of my code relevant to this conversion so far:
Code:
LPCSTR lpszOutput = "c:\\PDFTron_before.pdf";
SHELLEXECUTEINFOW File;
File.cbSize = sizeof(SHELLEXECUTEINFOW);
File.hwnd = NULL;
File.lpVerb = L"open";
File.lpFile = lpszOutput;
If I try using the green highlighted code, I'm getting the following error message:
Code:
1>c:\pdftron_vprint\shellscript\shellscript.cpp(20) : error C2440: '=' : cannot convert from 'LPCSTR' to 'LPCWSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
If I try replacing the highlighted code with:
Code:
int byteWritten = MultiByteToWideChar(CP_ACP,
MB_PRECOMPOSED,
lpszOutput,
-1,
(LPWSTR) File.lpFile,
0);
I'm getting a runtime error saying that File was used without being initialized. Does it imply that I should be allocating memory to File.lpFile?
-
I think I have found a potential workaround for this. Below shows what I'm trying to do right now:
Code:
wstring fn = wstring(lpszOutput);
However, I'm experiencing a problem with the conversion of LPCSTR to wstring. Any pointers on general methods on getting this conversion accomplished?
-
nvm on this question. I did some hacks to get around this problem.
-
Why not just declare lpszOutput as a LPWSTR?
Additionally, yes, you need allocate memory to File.lpFile, and that cast should not be needed. There is a way to call MultiByteToWideChar such that it will tell you how much memory to allocate. (Read MSDN docs)
-
Here is my own ascii to utf16 function:
Code:
Strings::CStringExW AnsiToUTF16(const Strings::CStringExA& strFrom)
{
Strings::CStringExW strTo;
int nLen = MultiByteToWideChar(CP_ACP, 0, strFrom.get(), -1, NULL, NULL);
wchar_t* strBuffer = strTo.GetBuffer(nLen + 1);
strBuffer[0] = 0;
CHECK_ERROR( MultiByteToWideChar(CP_ACP, 0, strFrom.get(), -1, strBuffer, nLen) );
strTo.ReleaseBuffer();
return strTo;
}
Study the code well and all should be fine you'll see.
-
Code:
WCHAR lpszOutput[] = L"c:\\PDFTron_before.pdf";
SHELLEXECUTEINFOW File;
File.cbSize = sizeof(SHELLEXECUTEINFOW);
File.hwnd = NULL;
File.lpVerb = L"open";
File.lpFile = lpszOutput;
Now it should work.
-
Declare your variables with the Unicode compatibility declarations (and macros).
Then you can flip between Ascii and Unicode without having to hack the code.
http://msdn.microsoft.com/en-us/libr...61(VS.85).aspx
-
You can include the <tchar.h> header and use TCHAR szString [] = _T("I am a string"); or use the TEXT("") macro ...
Greetz
Greenhorn