Reputation: 2685
I have something like this:
if(GetFileAttributesW("C:\\Directory")!="INVALID_FILE_ATTRIBUTES") {...}
I get error: cannot convert 'const char*' to 'const WCHAR*' for argument '1' to 'DWORD GetFileAttributesW(const WCHAR*)'
How to convert const char*
to const WCHAR*
?
Upvotes: 3
Views: 3179
Reputation: 13298
You're mixing text literals of one kind of codification with function calls with another kind of codification.
All this confusion is due the obscure chain of #define
s and typedef
s that Microsoft puts on his headers.
One of this annoying Microsofts tricks is to declare two function calls differenced with a letter A
or W
depending of the project settings. There's a macro that transforms the GetFileAttributes
to GetFileAttributesA
or GetFileAttributesW
if UNICODE
is defined.
In your case, you're getting rid of the macro and calling the UNICODE
version directly (the one ended with the letter W
) but in your call you're using a no-widestring literal, you can fix it easily appending a L
to the literal (as suggested by other users):
if(GetFileAttributesW(L"C:\\Directory")!="INVALID_FILE_ATTRIBUTES") {...}
So, if you're calling the version ended with A
, you can use a no-widestring literal:
if(GetFileAttributesA("C:\\Directory")!="INVALID_FILE_ATTRIBUTES") {...}
Other way to fix it is using the _T
macro (Check this answer):
if(GetFileAttributesW(_T("C:\\Directory"))!="INVALID_FILE_ATTRIBUTES") {...}
But if UNICODE
isn't defined, your original problem will rise again; finally, you can surrender to the Microsoft way of doing things and use all the macros provided:
// Note the lack of W or A, it's a macro, not a function call!!
if(GetFileAttributes(_T("C:\\Directory"))!="INVALID_FILE_ATTRIBUTES") {...}
Using the two macros (the one that changes GetFileAttributes
with the UNICODE
or no-UNICODE
version and the one that appends the L
to the literal), there's no need to worry about the project settings because the macros takes this responsability for you.
Edit.
My bad, i almost forget the most important part.
As pointed by other users, you're comparing the return value of GetFileAttributes
with a text literal; it returns a DWORD and according to the Microsoft documentation:
A DWORD is a 32-bit unsigned integer (range: 0 through 4294967295 decimal).
So, in the end, you're comparing an integer with a char[24]
, the comparison is possible but, it will never be true! You must read about the function and how to use it ;)
Upvotes: 1
Reputation: 294437
Don't use GetFileAttributesW
, use GetFileAttributes
. Add _T
to all const strings, and use _TCHAR*
. And INVALID_FILE_ATTRIBUES
is definitely not a string...
if(GetFileAttributes(_T("C:\\Directory"))!= INVALID_FILE_ATTRIBUTES )
{...}
See Unicode Programming Summary.
Upvotes: 3
Reputation: 20884
You are trying to compare the pointers address rather than the comtent!
Check function: memcmp(...)
Upvotes: 0