Reputation: 6386
when i use CreateFile function like below ...it gives me valid handle
HANDLE hDevice = CreateFile (TEXT("\\\\.\\G:"),
0,FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode
NULL, OPEN_EXISTING, 0, NULL);
if( hDevice == INVALID_HANDLE_VALUE )
{
qDebug()<<"In valid handle";
}
else
{
qDebug()<<"valid handle";
}
when i use like below ...it gives me invalid handle..
WCHAR Drive[4];
qDebug ()<<QString::fromWCharArray ( Drive );
The above prints like "G:\"
HANDLE hDevice = CreateFile ( Drive,
0,FILE_SHARE_READ | FILE_SHARE_WRITE, // share mode
NULL, OPEN_EXISTING, 0, NULL);
if( hDevice == INVALID_HANDLE_VALUE )
{
qDebug()<<"In valid handle";
}
else
{
qDebug()<<"valid handle";
}
How can i change the wchar to LPCWSTR
Thank you
Upvotes: 1
Views: 10712
Reputation: 8171
LPCWSTR
is a pointer (LP) to a constant (C) wide character (W) string (STR). In other words, this is a const WCHAR*
WCHAR Drive[4];
is a wide character array, which can also be called a wide character string.
Any array of a certain type can implicitly convert to a pointer to that same type. Furthermore, a pointer of a certain type can implicitly convert to a constant pointer of the same type, especially in the case of a function call.
Thus passing Drive
to that function implicitly converts to LPCWSTR
.
Your problem in not in that conversion. Your problem is most likely in the contents of your strings, as humbagumba's answer already explained.
Upvotes: 2
Reputation: 5538
You can either use the toWCharArray function or try something like this:
handle = CreateFile((LPCWSTR) fileName.constData(), FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
or this:
handle = CreateFile((LPCWSTR) fileName.utf16(), FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
Upvotes: 2
Reputation: 2074
The problem is not the conversion of the string, but the contents of the string. You can't open a volume (I guess that's what you're trying to do) with "G:\". It needs to be in the same format as you used in the first example. From MSDN:
When opening a volume or floppy drive, the lpFileName string should be the following form: \\.\X:. Do not use a trailing backslash, which indicates the root directory of a drive.
Hint: Always use GetLastError()
after API functions fail to get the reason for the failure.
Update: MSDN Link
Upvotes: 4