DeviceIoControl failing with ERROR_INVALID_PARAMETER

Hi All,

I created an IOCTL in my user app as well as in the driver with the
following code:
#define IOCTL_KEYBOARD_TRANSFER_DATA CTL_CODE(FILE_DEVICE_KEYBOARD, 0x800 ,
METHOD_BUFFERED , FILE_READ_DATA )

Now from my user app : I call the following code:

char output[100];
DWORD dwSize = 0;

memset(output, 0, sizeof(output));

HANDLE hFile = CreateFile(L"\.\kbDevice", GENERIC_ALL,
FILE_SHARE_WRITE|FILE_SHARE_READ, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); //using symbolic link as
CreateFile fails with DeviceInterfaces.

dwError = GetLastError();
if(hFile == INVALID_HANDLE_VALUE)
{
dwError = GetLastError();
}
DeviceIoControl(hFile, IOCTL_KEYBOARD_TRANSFER_DATA, NULL,
0,(void*)output, sizeof(output), &dwSize, NULL);
dwError = GetLastError());

I get dwError = 87 which is Invalid_Parameter.

I am not even getting call into my drivers Dipatch routine…

What could be the problem…

Appreciate your valuable insights

Abdul

You cannot just call GetLastError after each API call. You have to look at the api return value and check for failure statistics there first. Basically, upon success there is no guarantee that the api /clears/ the previous GetLastError value which means calling GLE in the success case can easily return a stale value.

d

dent from a phpne with no keynoard


From: Abdul Qader
Sent: September 15, 2010 5:45 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] DeviceIoControl failing with ERROR_INVALID_PARAMETER

Hi All,

I created an IOCTL in my user app as well as in the driver with the following code:
#define IOCTL_KEYBOARD_TRANSFER_DATA CTL_CODE(FILE_DEVICE_KEYBOARD, 0x800 , METHOD_BUFFERED , FILE_READ_DATA )

Now from my user app : I call the following code:

char output[100];
DWORD dwSize = 0;

memset(output, 0, sizeof(output));

HANDLE hFile = CreateFile(L"\.\kbDevice", GENERIC_ALL, FILE_SHARE_WRITE|FILE_SHARE_READ, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); //using symbolic link as CreateFile fails with DeviceInterfaces.

dwError = GetLastError();
if(hFile == INVALID_HANDLE_VALUE)
{
dwError = GetLastError();
}
DeviceIoControl(hFile, IOCTL_KEYBOARD_TRANSFER_DATA, NULL, 0,(void*)output, sizeof(output), &dwSize, NULL);
dwError = GetLastError());

I get dwError = 87 which is Invalid_Parameter.

I am not even getting call into my drivers Dipatch routine…

What could be the problem…

Appreciate your valuable insights

Abdul

— NTDEV is sponsored by OSR For our schedule of WDF, WDM, debugging and other seminars visit: http://www.osr.com/seminars To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

Abdul Qader wrote:

I created an IOCTL in my user app as well as in the driver with the
following code:
#define IOCTL_KEYBOARD_TRANSFER_DATA CTL_CODE(FILE_DEVICE_KEYBOARD,
0x800 , METHOD_BUFFERED , FILE_READ_DATA )

Now from my user app : I call the following code:

char output[100];
DWORD dwSize = 0;

memset(output, 0, sizeof(output));

HANDLE hFile = CreateFile(L"\.\kbDevice", GENERIC_ALL,
FILE_SHARE_WRITE|FILE_SHARE_READ, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); //using symbolic link as
CreateFile fails with DeviceInterfaces.

dwError = GetLastError();
if(hFile == INVALID_HANDLE_VALUE)
{
dwError = GetLastError();
}
DeviceIoControl(hFile, IOCTL_KEYBOARD_TRANSFER_DATA, NULL,
0,(void*)output, sizeof(output), &dwSize, NULL);
dwError = GetLastError());

I get dwError = 87 which is Invalid_Parameter.

I am not even getting call into my drivers Dipatch routine…

What could be the problem…

The likely problem is that your CreateFile is failing, so you get an
invalid handle. Even though you call GetLastError, you don’t ever check
the value, nor do you stop your processing. You just continue on
blindly. I’m not sure what you think those GetLastError calls are doing.

Your file name is incorrect. You need another backslash pair:
L"\\.\kbDevice".

Is this a HID driver? HID drivers are opened exclusively by the HID
stack. You can’t open your own handle.

Although this is really a style point, it always makes me a little
uneasy to see:
char buffer[100];
…( … buffer, sizeof(buffer) … );
because sooner or later, you’re going to want to make buffer a heap
variable, so you’ll change it to:
char * buffer = malloc(100);
and now you have a bug, because sizeof returns the wrong thing. It is
much better, in my opinion, to say:

#define BUFFER_SIZE 100
char buffer[BUFFER_SIZE];
…( … buffer, BUFFER_SIZE, … );

Or, even better:
std::vector buffer( 100 );
…( … &buffer[0], buffer.size(), … );


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

Tim,Doron…

thanks for your reply.

On Wed, Sep 15, 2010 at 10:47 PM, Tim Roberts wrote:

> Abdul Qader wrote:
> >
> >
> > I created an IOCTL in my user app as well as in the driver with the
> > following code:
> > #define IOCTL_KEYBOARD_TRANSFER_DATA CTL_CODE(FILE_DEVICE_KEYBOARD,
> > 0x800 , METHOD_BUFFERED , FILE_READ_DATA )
> >
> > Now from my user app : I call the following code:
> >
> > char output[100];
> > DWORD dwSize = 0;
> >
> > memset(output, 0, sizeof(output));
> >
> > HANDLE hFile = CreateFile(L"\.\kbDevice", GENERIC_ALL,
> > FILE_SHARE_WRITE|FILE_SHARE_READ, NULL,
> > CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); //using symbolic link as
> > CreateFile fails with DeviceInterfaces.
> >
> > dwError = GetLastError();
> > if(hFile == INVALID_HANDLE_VALUE)
> > {
> > dwError = GetLastError();
> > }
> > DeviceIoControl(hFile, IOCTL_KEYBOARD_TRANSFER_DATA, NULL,
> > 0,(void*)output, sizeof(output), &dwSize, NULL);
> > dwError = GetLastError());
> >
> > I get dwError = 87 which is Invalid_Parameter.
> >
> > I am not even getting call into my drivers Dipatch routine…
> >
> > What could be the problem…
>
> The likely problem is that your CreateFile is failing, so you get an
> invalid handle. Even though you call GetLastError, you don’t ever check
> the value, nor do you stop your processing. You just continue on
> blindly. I’m not sure what you think those GetLastError calls are doing.
>
> Your file name is incorrect. You need another backslash pair:
> L"\\.\kbDevice".
>
> Is this a HID driver? HID drivers are opened exclusively by the HID
> stack. You can’t open your own handle.
>
> Although this is really a style point, it always makes me a little
> uneasy to see:
> char buffer[100];
> …( … buffer, sizeof(buffer) … );
> because sooner or later, you’re going to want to make buffer a heap
> variable, so you’ll change it to:
> char * buffer = malloc(100);
> and now you have a bug, because sizeof returns the wrong thing. It is
> much better, in my opinion, to say:
>
> #define BUFFER_SIZE 100
> char buffer[BUFFER_SIZE];
> …( … buffer, BUFFER_SIZE, … );
>
> Or, even better:
> std::vector buffer( 100 );
> …( … &buffer[0], buffer.size(), … );
>
> –
> Tim Roberts, xxxxx@probo.com
> Providenza & Boekelheide, Inc.
>
>
> —
> NTDEV is sponsored by OSR
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer
>