Answered by:
Conversion between char* to LPCWSTR

Question
-
Here is a code snippet.
#define LOADLIBRARY(lib) LoadLibrary(lib)
......
char* _pszLibName;
.......
if((gpDSM=LOADLIBRARY(_pszLibName)) != 0) //=====> Error!!!
If compile, error occurs at LOADLIBRARY like following.
error C2664: 'LoadLibraryW' : cannot convert parameter 1 from 'char *' to 'LPCWSTR'
If I do casting explicitly like following , compile error is once removed but I'm not sure if this is correct.
if((gpDSM=LOADLIBRARY((LPCWSTR)_pszLibName)) != 0)
- Edited by Jeff0803 Monday, August 31, 2015 10:45 PM
Monday, August 31, 2015 10:26 PM
Answers
-
If compile, error occurs at LOADLIBRARY like following.
error C2664: 'LoadLibraryW' : cannot convert parameter 1 from 'char *' to 'LPCWSTR'
If I do casting explicitly like following , compile error is once removed but I'm not sure if this is correct.
if((gpDSM=LOADLIBRARY((LPCWSTR)_pszLibName)) != 0)
Almost certainly not correct. You're lying to the compiler. You're telling it that you
know what you're doing, when you probably don't. (If you did, you probably wouldn't be
here.)
LoadLibrary maps to LoadLibraryW when Unicode is the character set for the project,
as it is for yours. LoadLibraryW expects wide character (wchar_t) arguments.
LoadLibrary maps to LoadLibraryA when Unicode is NOT the character set for the project,
LoadLibraryA expects narrow character (char) arguments.
You're passing a narrow character string to a function that expects a wide character string.
By using a cast you're telling the compiler to pretend that you're not really passing a
narrow string - but of course you are, aren't you?
If you really do want to convert the char string to a wchar_t string, look at:
mbstowcs, _mbstowcs_l
https://msdn.microsoft.com/en-us/library/k1f9b8cy.aspx
mbstowcs_s, _mbstowcs_s_l
https://msdn.microsoft.com/en-us/library/eyktyxsx.aspx
- Wayne
- Proposed as answer by Michael Koster Tuesday, September 1, 2015 5:59 AM
- Marked as answer by Shu 2017 Tuesday, September 15, 2015 4:57 AM
Tuesday, September 1, 2015 2:52 AM
All replies
-
You should be able to figure this one out. Have you looked up the error message?
Why do you think the compiler is complaining about a function named LoadLibraryW?
What is an LPCWSTR and why do you think the compiler is rejecting an ASCII char * parameter?
What are you trying to accomplish with the #define LOADLIBRARY macro?
Monday, August 31, 2015 10:41 PM -
If compile, error occurs at LOADLIBRARY like following.
error C2664: 'LoadLibraryW' : cannot convert parameter 1 from 'char *' to 'LPCWSTR'
If I do casting explicitly like following , compile error is once removed but I'm not sure if this is correct.
if((gpDSM=LOADLIBRARY((LPCWSTR)_pszLibName)) != 0)
Almost certainly not correct. You're lying to the compiler. You're telling it that you
know what you're doing, when you probably don't. (If you did, you probably wouldn't be
here.)
LoadLibrary maps to LoadLibraryW when Unicode is the character set for the project,
as it is for yours. LoadLibraryW expects wide character (wchar_t) arguments.
LoadLibrary maps to LoadLibraryA when Unicode is NOT the character set for the project,
LoadLibraryA expects narrow character (char) arguments.
You're passing a narrow character string to a function that expects a wide character string.
By using a cast you're telling the compiler to pretend that you're not really passing a
narrow string - but of course you are, aren't you?
If you really do want to convert the char string to a wchar_t string, look at:
mbstowcs, _mbstowcs_l
https://msdn.microsoft.com/en-us/library/k1f9b8cy.aspx
mbstowcs_s, _mbstowcs_s_l
https://msdn.microsoft.com/en-us/library/eyktyxsx.aspx
- Wayne
- Proposed as answer by Michael Koster Tuesday, September 1, 2015 5:59 AM
- Marked as answer by Shu 2017 Tuesday, September 15, 2015 4:57 AM
Tuesday, September 1, 2015 2:52 AM -
Furthermore, casting a pointer to an ASCII character string (char *) to a pointer to a UNICODE character string (LPCWSTR) doesn't convert it to UNICODE. It may get you past the compiler syntax checking but your code will pass an improperly formatted parameter to the LoadLibraryW function and a runtime error will likely be the result.
Tuesday, September 1, 2015 8:51 AM -
Did you tried considering TCHAR instead of char.
Thanks
Rupesh Shukla
Tuesday, September 1, 2015 3:42 PM