Next: OpenThread
From: Abdo Haji-Ali on 27 Sep 2005 17:07 You can use the WM_DEVICECHANGE message, of course to use it you must have a window... Abdo Haji-Ali Programmer In|Framez "Mike M." <nospam(a)someplace.com> wrote in message news:OvDFYP3wFHA.1256(a)TK2MSFTNGP09.phx.gbl... > Windows XP Pro and Visual Studio 6.0 I need to write a small service that > detects when a USB drive is plugged in so I can run another program from > the > removable drive. I have been testing with the Autoplay settings using > TweakUI and such but it isn't as reliable as I would like. And I don't > like > the way it functions in some circumstances. Is there a way to register to > receive the detection event or periodically scan for a device? > > TIA > >
From: Hieu Le Trung on 27 Sep 2005 21:48 So the SHNotifyChangeRegister function will not work on a Service? "William DePalo [MVP VC++]" <willd.no.spam(a)mvps.org> wrote in message news:O8kwKh3wFHA.664(a)tk2msftngp13.phx.gbl... > "Mike M." <nospam(a)someplace.com> wrote in message > news:OvDFYP3wFHA.1256(a)TK2MSFTNGP09.phx.gbl... >> Windows XP Pro and Visual Studio 6.0 I need to write a small service >> that >> detects when a USB drive is plugged in so I can run another program from >> the >> removable drive. I have been testing with the Autoplay settings using >> TweakUI and such but it isn't as reliable as I would like. And I don't >> like >> the way it functions in some circumstances. Is there a way to register >> to >> receive the detection event or periodically scan for a device? > > Yes, there is a way. > > The shell provides a less-than-barely documented function named > SHNotifyChangeRegister() to register a request to be notified of device or > media insertions, removals etc. > > As it is a shell function, there may be gotchas when trying to use the > function in a service which - if you follow reasonable security policy - > should not run on the interactive desktop. > > You may want to post again in the kernel group > > microsoft.public.win32.programmer.kernel > > for more information or to see if there are other options. > > On your way over you may find this sample useful: > > http://www.codeproject.com/shell/shchangenotifyregister.asp > > Sadly I couldn't find one the MSDN site. > > Regards, > Will > >
From: William DePalo [MVP VC++] on 27 Sep 2005 22:08 "Hieu Le Trung" <hieult(a)cybersoft-vn.com> wrote in message news:OdZQ668wFHA.3720(a)TK2MSFTNGP14.phx.gbl... > So the SHNotifyChangeRegister function will not work on a Service? I don't know never having tried it. I offered the caution because it is a shell (Explorer) function and sometimes seemimgly simple things fail to work as you'd expect in a service: http://www.microsoft.com/msj/0398/service2.aspx Regards, Will
From: Mike M. on 28 Sep 2005 08:29 I am currently trying to get the RegisterDeviceNotification() to work. On these machines we start our application(service) instead of the shell (explorer) so most features that are part of the shell don't work. That is one of the issues I ran into with the Autoplay logic. I had to let explorer run to get it to work. "William DePalo [MVP VC++]" <willd.no.spam(a)mvps.org> wrote in message news:uEo5EG9wFHA.1168(a)TK2MSFTNGP15.phx.gbl... > "Hieu Le Trung" <hieult(a)cybersoft-vn.com> wrote in message > news:OdZQ668wFHA.3720(a)TK2MSFTNGP14.phx.gbl... > > So the SHNotifyChangeRegister function will not work on a Service? > > I don't know never having tried it. > > I offered the caution because it is a shell (Explorer) function and > sometimes seemimgly simple things fail to work as you'd expect in a service: > > http://www.microsoft.com/msj/0398/service2.aspx > > Regards, > Will > >
From: Mike M. on 28 Sep 2005 11:26
o.k. Well, I got that all to work nicely using RegisterDeviceNotification(). Using HandlerEx instead of Handler like below: The only thing remaing is to get the drive letter for the device. The name I retrieve as shown by the code below is some long device name including the GUID but NOT the drive letter. Anyone know how to get the drive letter? // Extended service handler DWORD CServiceModule::ServiceHandlerEx( DWORD fdwControl, DWORD dwEventType, LPVOID lpEventData, PVOID lpContext ) { // Local variables. static DWORD CachedState; DEV_BROADCAST_HDR *hdr; DEV_BROADCAST_DEVICEINTERFACE *devhdr; int nameLen; CString drive; // Do not replicate current state. // Controls codes greater than 127 are programmer // defined service control codes. if(( CachedState == fdwControl) && ( fdwControl < 128)) return NO_ERROR; // Switch on service control message. switch(fdwControl) { case SERVICE_CONTROL_STOP: SetServiceStatus(SERVICE_STOP_PENDING); PostThreadMessage(dwThreadID, WM_QUIT, 0, 0); break; case SERVICE_CONTROL_DEVICEEVENT: //SERVICE_CONTROL_POWEREVENT: { switch((int) dwEventType) { case DBT_DEVICEARRIVAL: hdr = (DEV_BROADCAST_HDR *)lpEventData; // DBT_DEVTYP_DEVICEINTERFACE // handle event if(hdr->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE) { devhdr = (DEV_BROADCAST_DEVICEINTERFACE *)lpEventData; nameLen = devhdr->dbcc_size - sizeof(DEV_BROADCAST_DEVICEINTERFACE); // Services always get unicode strings drive = (wchar_t *)&devhdr->dbcc_name; } return NO_ERROR; case DBT_DEVICEREMOVECOMPLETE: // handle event return NO_ERROR; } } // end switch SERVICE_CONTROL_DEVICEEVENT } // end switch fdwcontrol return NO_ERROR; } "Mike M." <nospam(a)someplace.com> wrote in message news:erRAieCxFHA.3112(a)TK2MSFTNGP10.phx.gbl... > I am currently trying to get the RegisterDeviceNotification() to work. On > these machines we start our application(service) instead of the shell > (explorer) so most features that are part of the shell don't work. That is > one of the issues I ran into with the Autoplay logic. I had to let explorer > run to get it to work. > > "William DePalo [MVP VC++]" <willd.no.spam(a)mvps.org> wrote in message > news:uEo5EG9wFHA.1168(a)TK2MSFTNGP15.phx.gbl... > > "Hieu Le Trung" <hieult(a)cybersoft-vn.com> wrote in message > > news:OdZQ668wFHA.3720(a)TK2MSFTNGP14.phx.gbl... > > > So the SHNotifyChangeRegister function will not work on a Service? > > > > I don't know never having tried it. > > > > I offered the caution because it is a shell (Explorer) function and > > sometimes seemimgly simple things fail to work as you'd expect in a > service: > > > > http://www.microsoft.com/msj/0398/service2.aspx > > > > Regards, > > Will > > > > > > |