From: mooni on
Hi all,

I got handle of my USB disk using following instruction.
handle = CreateFile(dev_name, 0, 0, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);

I am getting this handle successfully. The problem I was facing previously
was that my ISR was not getting invoked on attaching USB disk. What I did, I
checked the registry values and found out that I was passing wrong GUID in
notification filter. Just to tell you I was passing {0xc5e4c602, 0xa413,
0x4caa, {0x9b, 0xbd, 0x2e, 0x35, 0x4c, 0x1b, 0xdd, 0xfc}} which was not
working.

I then had to look in registery that when I plug in my device it is
registered at some other Interface GUID which is {0xa5dcbf10, 0x6530, 0x11d2,
{0x90, 0x1f, 0x00, 0xc0, 0x4f, 0xb9, 0x51, 0xed}}.

Now when I set this value in notification filter and then pass this
notification filter to RegisterDeviceNotification I get ISR invoked
successfully.

But now the problem is that when I call DeviceIoControl method as shown
below it returns error. I've checked the error code and it is returning error
number 87 which means invalid parameter. code snippet is below

unsigned int usb_get_last_status(libusb_usb_dev_handle *dev)
{
usb_context_t ctx;
unsigned int ret = 0;
DWORD err = 0;
unsigned int status = 0xfffffffe;

memset(&ctx, 0, sizeof(usb_context_t));
ctx.dev = dev;
ctx.control_code = LIBUSB_IOCTL_USBD_STATUS_READ;
ctx.ol.Offset = 0;
ctx.ol.OffsetHigh = 0;
ctx.ol.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

if(ctx.ol.hEvent)
{
ResetEvent(ctx.ol.hEvent);
if(!DeviceIoControl(ctx.dev->impl_info,
ctx.control_code,&ctx.req,sizeof(ctx.req),
&status,
sizeof(status),
&ret,
&ctx.ol))
{
err = GetLastError();
}

...........................
Can anyone tell me the reason of it and any solution to the problem?

From: chris.aseltine on
On Apr 7, 2:07 am, mooni <mo...(a)discussions.microsoft.com> wrote:

> I got handle of my USB disk using following instruction.
> handle = CreateFile(dev_name, 0, 0, NULL, OPEN_EXISTING,
> FILE_ATTRIBUTE_NORMAL, NULL);
>
> I am getting this handle successfully. The problem I was facing previously
> was that my ISR was not getting invoked on attaching USB disk. What I did, I
> checked the registry values and found out that I was passing wrong GUID in
> notification filter. Just to tell you I was passing {0xc5e4c602, 0xa413,
> 0x4caa, {0x9b, 0xbd, 0x2e, 0x35, 0x4c, 0x1b, 0xdd, 0xfc}} which was not
> working.

I have no idea what that GUID is. Is it some sort of libusb device
interface? If so, maybe that explains why your IOCTL is failing --
libusb isn't really loaded, because you're not getting the device
interface arrival.

> I then had to look in registery that when I plug in my device it is
> registered at some other Interface GUID which is {0xa5dcbf10, 0x6530, 0x11d2,
> {0x90, 0x1f, 0x00, 0xc0, 0x4f, 0xb9, 0x51, 0xed}}.

This is registered by the USB stack for all USB devices. It's not
native to USB disk drives.

> But now the problem is that when I call DeviceIoControl method as shown
> below it returns error. I've checked the error code and it is returning error
> number 87 which means invalid parameter. code snippet is below
>
> ctx.control_code = LIBUSB_IOCTL_USBD_STATUS_READ;

I'm not familiar with how libusb works, but something tells me you
might not be able to send a libusb IOCTL to a USB drive which already
has a driver loaded for it (usbstor).
From: mooni on
Chris,

Thanks for your reply. Please see my comments

"chris.aseltine(a)gmail.com" wrote:

> On Apr 7, 2:07 am, mooni <mo...(a)discussions.microsoft.com> wrote:
>
> > I got handle of my USB disk using following instruction.
> > handle = CreateFile(dev_name, 0, 0, NULL, OPEN_EXISTING,
> > FILE_ATTRIBUTE_NORMAL, NULL);
> >
> > I am getting this handle successfully. The problem I was facing previously
> > was that my ISR was not getting invoked on attaching USB disk. What I did, I
> > checked the registry values and found out that I was passing wrong GUID in
> > notification filter. Just to tell you I was passing {0xc5e4c602, 0xa413,
> > 0x4caa, {0x9b, 0xbd, 0x2e, 0x35, 0x4c, 0x1b, 0xdd, 0xfc}} which was not
> > working.
>
> I have no idea what that GUID is. Is it some sort of libusb device
> interface? If so, maybe that explains why your IOCTL is failing --
> libusb isn't really loaded, because you're not getting the device
> interface arrival.
>

No actually I am successfully getting device interface arrival. Actually on
arrival I call this createFile and after this I called DeviceIoControl. I've
written a test code, just to make things look abit simple. Please see below

#include <windows.h>
#include <SetupAPI.h>
#include<Dbt.h>
#include "usb.h" // come with libusb
#include "driver_api.h" // come with libusb

#define LIBUSB_DEVICE_NAME "\\\\.\\libusb0-"



const char g_szClassName[] = "myWindowClass";
static HDEVNOTIFY hdevnotify;
//static GUID GUID_CLASS_VHCTRL = {0xc5e4c602, 0xa413, 0x4caa, {0x9b, 0xbd,
0x2e, 0x35, 0x4c, 0x1b, 0xdd, 0xfc}};
static GUID GUID_CLASS_VHCTRL = {0xa5dcbf10, 0x6530, 0x11d2, {0x90, 0x1f,
0x00, 0xc0, 0x4f, 0xb9, 0x51, 0xed}};


void checkit(WPARAM);
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_LBUTTONDOWN:
{
char szFileName[MAX_PATH];
HINSTANCE hInstance = GetModuleHandle(NULL);

printf("Hello");
GetModuleFileName(hInstance, szFileName, MAX_PATH);
MessageBox(hwnd, szFileName, "This program is:", MB_OK |
MB_ICONINFORMATION);
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DEVICECHANGE:
MessageBox(hwnd,"Hello","Welcome",MB_OK);
printf("Hello I am Taimoor");
checkit(wParam);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}


void main()
{
HDEVINFO temp;
initDev1();
while(1);
}

DWORD WINAPI ThreadFunc (void* param)
{
MSG Msg;
WNDCLASSEX wc;
HWND hwnd;
DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
DWORD ret;

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = NULL;//hInstance;
wc.hIcon = NULL;//LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = NULL;//LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = NULL;//LoadIcon(NULL, IDI_APPLICATION);

if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}

hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, NULL/*hInstance*/, NULL);

if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
if(hwnd)
{
ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
NotificationFilter.dbcc_classguid = GUID_CLASS_VHCTRL;


hdevnotify = RegisterDeviceNotification(
hwnd,
&NotificationFilter,
DEVICE_NOTIFY_WINDOW_HANDLE);

}

if(!hdevnotify)
{
ret = GetLastError();
}

while(GetMessage(&Msg, hwnd, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
printf("I am out of loop");
}

int initDev1()
{
DWORD dwError;
DWORD threadId;
HANDLE hThread;

hThread = CreateThread(NULL, 0, ThreadFunc, NULL,
0, &threadId);
return 0;
}

void checkit(WPARAM wp)
{
HANDLE hand;
char dev_name[512];
int i;
DWORD derr;

libusb_request req;
OVERLAPPED ol;
unsigned int ret = 0;
unsigned int status = 0xfffffffe;

HDEVINFO dev_info;

if(wp == DBT_DEVICEARRIVAL)
printf("Device arrived");
else
printf("Device Detached");

for(i = 1; i < 256; i++)
{
_snprintf(dev_name, sizeof(dev_name) - 1,"%s%04d",
LIBUSB_DEVICE_NAME, i);

hand = CreateFile(dev_name, 0, 0, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);

if(hand != INVALID_HANDLE_VALUE)
printf("Got the handle");
else
derr = GetLastError();

ol.Offset = 0;
ol.OffsetHigh = 0;
ol.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

if(!DeviceIoControl(hand,LIBUSB_IOCTL_USBD_STATUS_READ,&req,
sizeof(req),&status,sizeof(status),&ret, &ol))
printf("Error in DeviceIoControl");
derr = GetLastError();

}
}


From: HRcrestron on
I am looking for a WDK software develper for a FT perm position in northern
NJ. Would you be interested? Please contact me a bblum(a)crestron.com

"mooni" wrote:

> Hi all,
>
> I got handle of my USB disk using following instruction.
> handle = CreateFile(dev_name, 0, 0, NULL, OPEN_EXISTING,
> FILE_ATTRIBUTE_NORMAL, NULL);
>
> I am getting this handle successfully. The problem I was facing previously
> was that my ISR was not getting invoked on attaching USB disk. What I did, I
> checked the registry values and found out that I was passing wrong GUID in
> notification filter. Just to tell you I was passing {0xc5e4c602, 0xa413,
> 0x4caa, {0x9b, 0xbd, 0x2e, 0x35, 0x4c, 0x1b, 0xdd, 0xfc}} which was not
> working.
>
> I then had to look in registery that when I plug in my device it is
> registered at some other Interface GUID which is {0xa5dcbf10, 0x6530, 0x11d2,
> {0x90, 0x1f, 0x00, 0xc0, 0x4f, 0xb9, 0x51, 0xed}}.
>
> Now when I set this value in notification filter and then pass this
> notification filter to RegisterDeviceNotification I get ISR invoked
> successfully.
>
> But now the problem is that when I call DeviceIoControl method as shown
> below it returns error. I've checked the error code and it is returning error
> number 87 which means invalid parameter. code snippet is below
>
> unsigned int usb_get_last_status(libusb_usb_dev_handle *dev)
> {
> usb_context_t ctx;
> unsigned int ret = 0;
> DWORD err = 0;
> unsigned int status = 0xfffffffe;
>
> memset(&ctx, 0, sizeof(usb_context_t));
> ctx.dev = dev;
> ctx.control_code = LIBUSB_IOCTL_USBD_STATUS_READ;
> ctx.ol.Offset = 0;
> ctx.ol.OffsetHigh = 0;
> ctx.ol.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
>
> if(ctx.ol.hEvent)
> {
> ResetEvent(ctx.ol.hEvent);
> if(!DeviceIoControl(ctx.dev->impl_info,
> ctx.control_code,&ctx.req,sizeof(ctx.req),
> &status,
> sizeof(status),
> &ret,
> &ctx.ol))
> {
> err = GetLastError();
> }
>
> ..........................
> Can anyone tell me the reason of it and any solution to the problem?
>
From: HRcrestron on
I am looking for a WDK software develper for a ft perm position in northern
NJ. Would you be interested. Please contact me at bblum(a)crestron.com

"chris.aseltine(a)gmail.com" wrote:

> On Apr 7, 2:07 am, mooni <mo...(a)discussions.microsoft.com> wrote:
>
> > I got handle of my USB disk using following instruction.
> > handle = CreateFile(dev_name, 0, 0, NULL, OPEN_EXISTING,
> > FILE_ATTRIBUTE_NORMAL, NULL);
> >
> > I am getting this handle successfully. The problem I was facing previously
> > was that my ISR was not getting invoked on attaching USB disk. What I did, I
> > checked the registry values and found out that I was passing wrong GUID in
> > notification filter. Just to tell you I was passing {0xc5e4c602, 0xa413,
> > 0x4caa, {0x9b, 0xbd, 0x2e, 0x35, 0x4c, 0x1b, 0xdd, 0xfc}} which was not
> > working.
>
> I have no idea what that GUID is. Is it some sort of libusb device
> interface? If so, maybe that explains why your IOCTL is failing --
> libusb isn't really loaded, because you're not getting the device
> interface arrival.
>
> > I then had to look in registery that when I plug in my device it is
> > registered at some other Interface GUID which is {0xa5dcbf10, 0x6530, 0x11d2,
> > {0x90, 0x1f, 0x00, 0xc0, 0x4f, 0xb9, 0x51, 0xed}}.
>
> This is registered by the USB stack for all USB devices. It's not
> native to USB disk drives.
>
> > But now the problem is that when I call DeviceIoControl method as shown
> > below it returns error. I've checked the error code and it is returning error
> > number 87 which means invalid parameter. code snippet is below
> >
> > ctx.control_code = LIBUSB_IOCTL_USBD_STATUS_READ;
>
> I'm not familiar with how libusb works, but something tells me you
> might not be able to send a libusb IOCTL to a USB drive which already
> has a driver loaded for it (usbstor).
>