In my C++ application i am trying to do create file for custom backlight
driver, which returns handle as 0xfffffff while GetLastError() returns 0 as
success.
Please find the code snippet :-
HANDLE hBklight = CreateFile(_T("BLK1:"), GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ |
FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
wsprintf(buf, L"JGNS3Device::SetBrightness hBklight=%p", hBklight);
VS_TRACE(buf);
if(hBklight == INVALID_HANDLE_VALUE)
{
wsprintf(buf, L"JGNS3Device::SetBrightness GetLastError=%ld", GetLastError());
VS_TRACE(buf);
return;
}
The same code works in my another project design which is a generic win32
application.
This is a custom application written over WinCE 6 and not a Win32
application where CreateFile returns INVALID_HANDLE.
The handle is not opened any where else in the application and there is no
sharing violation too.
I am not sure what could be the possible reasons of this error.
It would be of great help if you could guide me to the reasons for
CreateFile getting failed my application.
Thanks and regard,
Misbah
0xffffffff is INVALID_HANDLE_VALUE, as your code also uses.
> HANDLE hBklight = CreateFile(_T("BLK1:"), GENERIC_READ | GENERIC_WRITE,
> FILE_SHARE_READ |
> FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
I'm not 100% sure whether sharing read/write access is okay for devices,
read the docs. Also, is that "BLK" or "BKL"?
> wsprintf(buf, L"JGNS3Device::SetBrightness hBklight=%p", hBklight);
>
> VS_TRACE(buf);
Either of these may have overwritten the value of GetLastError()...
> if(hBklight == INVALID_HANDLE_VALUE)
> {
> wsprintf(buf, L"JGNS3Device::SetBrightness GetLastError=%ld",
> GetLastError());
...so this is meaningless. Also, I don't think %ld matches GetLastError()'s
return type.
Rule of thumb:
HANDLE h = CreateFile(...);
if(h == INVALID_HANDLE_VALUE) {
// immediately save value
int const e = GetLastError();
// use 'e' or do other stuff here
...
throw win32_error(e, "CreateFile", "open backlight driver");
}
> The same code works in my another project design which is a generic win32
> application.
>
> This is a custom application written over WinCE 6 and not a Win32
> application where CreateFile returns INVALID_HANDLE.
I don't understand these statements, does "win32" mean desktop
(non-embedded) MS Windows? And why should CreateFile() return anything
different?
Uli
--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932