views:

81

answers:

3

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

+4  A: 

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

humbagumba
thanks...no table provided
barbgal
humbagumba
when i modified like HANDLE hDevice = CreateFile ( aDrive[0], 0,FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);i get error C2664: 'CreateFileW' : cannot convert parameter 1 from 'WCHAR' to 'LPCWSTR'
barbgal
That's because when you pass aDrive[0], you're only passing one character, not a string.
humbagumba
can you pls tel me how to convert it
barbgal
You need to put the lpFileName paramter in the correct form. Please look at my answer above or at the MSDN page. You're missing the "\\.\" in front of the drive letter.
humbagumba
Try this ....HANDLE hDevice = CreateFile (QString("\\\\.\\%1:").arg(aDrive[0]), 0,FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
humbagumba
qDebug ()<<QString::fromWCharArray ( aDrive[0] ); returns error..ii cannot convert this to qstring ...any idea
barbgal
@barbgal: I'll quote put humbagumba's earlier answer, since it still applies: "That's because when you pass aDrive[0], you're only passing one character, not a string"
MSalters
+1  A: 

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);
the_void
+1  A: 

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.

TheUndeadFish