"DBT_DEVICEARRIVAL" message in Win2000

Hello,everyone,I had encounted a problem,that I can’t get message
“DBT_DEVICEARRIVAL” in Win2000,but it run OK in Win98,can anyone help me to
solve it? Thanks!
Follow is my source code:

#include “dbt’h”

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM
lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

switch (message)
{
case WM_DEVICECHANGE:
{
switch(wParam)
case DBT_DEVICEARRIVAL:
{
MessageBox(NULL,“Device Arrival”,“Hello”,MB_OK);
}
break;
}
break;
}

Following does for a service! I think should be same for an Win app.
Hope this helps.

Anand

DWORD ServiceHandlerEx( DWORD fdwControl,
DWORD dwEventType,
LPVOID lpEventData,
PVOID lpContext )
{

// Local variables.
static DWORD CachedState;

// 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_DEVICEEVENT: //SERVICE_CONTROL_POWEREVENT:
{
OutputDebugString(TEXT(“Entered Device Event\n”));
switch((int) dwEventType)
{
case DBT_DEVICEARRIVAL:
// handle event
OutputDebugString(TEXT(“Device Arrival\n”));

return NO_ERROR;

case DBT_DEVICEREMOVECOMPLETE:
// handle event
OutputDebugString(TEXT(“Device Removal\n”));
return NO_ERROR;
}
}
}

Thanks Peter!

Futoshi

-----Original Message-----
From: Peter Wieland [mailto:xxxxx@windows.microsoft.com]
Sent: Saturday, September 14, 2002 12:48 AM
To: NT Developers Interest List
Subject: [ntdev] Re: [BugCheck 0xC2]

bugcheck 0xc2 occurs for many “bad” uses of pool. There is no specific
check for NULL but using a NULL pointer would probably turn into an
attempt to free unallocated memory and thus cause this bugcheck.

you should consider not handing NULL into ExFreePool.

-p

-----Original Message-----
From: xxxxx@citrix.co.jp [mailto:xxxxx@citrix.co.jp]
Sent: Thursday, September 12, 2002 7:44 PM
To: NT Developers Interest List
Subject: [ntdev] Re: [BugCheck 0xC2]

Thanks Roddy,

I would like to know whether we can say that we lead to BUGCHECK 0xC2
or not whenever kernel mode driver passed NULL POINTER to nt!ExFreePool.

Thanks a lot.

Futoshi

-----Original Message-----
From: Roddy, Mark [mailto:xxxxx@stratus.com]
Sent: Thursday, September 12, 2002 9:46 PM
To: NT Developers Interest List
Subject: [ntdev] Re: [BugCheck 0xC2]

Yes ExFreePool will bugcheck on null pointer.

-----Original Message-----
From: xxxxx@citrix.co.jp [mailto:xxxxx@citrix.co.jp]
Sent: Wednesday, September 11, 2002 10:38 PM
To: NT Developers Interest List
Subject: [ntdev] Re: [BugCheck 0xC2]

Hi, Mark

Thank you for your reply, Actually I have looked into windbg
document in regarding BUGCHECK 0xC2. This BSOD was caused
that our kernel mode driver passed NULL POINTER to
ExFreePool. maybe I though it did not take care about NULL
POINTER. As I have not developped kernel mode driver, I would
like to know whether BUGCHEC 0xC2 occurs or not whenever
kernel mode driver passed NULL POINTER to ExFreePool.

Thanks in advance,
Futoshi

-----Original Message-----
From: Mark Roddy [mailto:xxxxx@hollistech.com]
Sent: Wednesday, September 11, 2002 9:39 PM
To: NT Developers Interest List
Subject: [ntdev] Re: [BugCheck 0xC2]

See “Bug Check 0xC2: BAD_POOL_CALLER” in the section “Bug Check Code
Reference” in the latest windbg documentation for complete
information
about this bugcheck code.

It is certainly a fatal error to call ExFreePool with any invalid
address.

Will you always get this bugcheck? I give up, probably.

===========================
Mark Roddy
Consultant, Microsoft DDK MVP
Hollis Technology Solutions
xxxxx@hollistech.com
www.hollistech.com
603-321-1032

-----Original Message-----
From: xxxxx@citrix.co.jp
To: “NT Developers Interest List”
> Date: Wed, 11 Sep 2002 18:03:33 +0900
> Subject: [ntdev] [BugCheck 0xC2]
>
> > Hi, all
> >
> > I have an quick question.
> > If kernel mode driver try to free null poiner using
> nt!ExFreePool(),
> > Can we say that we can get BSOD with BugCheck 0xC2 absolutely?
> >
> > I had an experience so that get BSOD with BugCheck 0xC2 when kernel
> > mode driver try to do so.
> >
> > Thanks in advance,
> > Futoshi
> >
> >
> > —
> > You are currently subscribed to ntdev as: xxxxx@hollistech.com To
> > unsubscribe send a blank email to %%email.unsub%%
>
>
>
> —
> You are currently subscribed to ntdev as:
> xxxxx@citrix.co.jp To unsubscribe send a blank email
> to %%email.unsub%%
>
>
> —
> You are currently subscribed to ntdev as:
> xxxxx@stratus.com To unsubscribe send a blank email to
> %%email.unsub%%
>


You are currently subscribed to ntdev as: xxxxx@citrix.co.jp
To unsubscribe send a blank email to %%email.unsub%%


You are currently subscribed to ntdev as: xxxxx@microsoft.com
To unsubscribe send a blank email to %%email.unsub%%


You are currently subscribed to ntdev as: xxxxx@citrix.co.jp
To unsubscribe send a blank email to %%email.unsub%%

Hi Anand,

I am writing a service that needs to know about cd arrival and removal.
Unfortunately my service does not get the DBT_DEVICEARRIVAL,
DBT_DEVICEREMOVECOMPLETE events.
My code looks very similar to your snippet below.
I am running on wink server sp2.

Do you need to explicitly register for device notification by using
RegisterDevicenotification? I want to avoid doing that.
Can you please tell me how you have setup your SERVICE_STATUS structure?

Thanks.
Parag.

You have to register for device notification or you will not receive
device notification. There are samples in the ddk that do this correctly.

===========================
Mark Roddy
Consultant, Microsoft DDK MVP
Hollis Technology Solutions
xxxxx@hollistech.com
www.hollistech.com
603-321-1032

-----Original Message-----
From: xxxxx@connectix.com
To: “NT Developers Interest List”
Date: Thu, 19 Sep 2002 15:23:43 -0400
Subject: [ntdev] Re: “DBT_DEVICEARRIVAL” message in Win2000

> Hi Anand,
>
> I am writing a service that needs to know about cd arrival and removal.
> Unfortunately my service does not get the DBT_DEVICEARRIVAL,
> DBT_DEVICEREMOVECOMPLETE events.
> My code looks very similar to your snippet below.
> I am running on wink server sp2.
>
> Do you need to explicitly register for device notification by using
> RegisterDevicenotification? I want to avoid doing that.
> Can you please tell me how you have setup your SERVICE_STATUS
> structure?
>
> Thanks.
> Parag.
>
> —
> You are currently subscribed to ntdev as: xxxxx@hollistech.com
> To unsubscribe send a blank email to %%email.unsub%%

Mark,
I used RegisterDevicenotification, but I still do not see any arrival/removal events.
Parag.

Here is how I set things up.

Service properties.

mCurrentServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
mCurrentServiceStatus.dwCurrentState = SERVICE_START_PENDING;
mCurrentServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_HARDWAREPROFILECHANGE ; mCurrentServiceStatus.dwWin32ExitCode = 0;
mCurrentServiceStatus.dwServiceSpecificExitCode = 0;
mCurrentServiceStatus.dwCheckPoint = 0;
mCurrentServiceStatus.dwWaitHint = 0;


RegisterDeviceNotification

DEV_BROADCAST_DEVICEINTERFACE broadCastDeviceInterface = {0};

broadCastDeviceInterface.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
broadCastDeviceInterface.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
broadCastDeviceInterface.dbcc_classguid = GUID_DEVCLASS_VOLUME; //GUID_DEVCLASS_CDROM;

RegisterDeviceNotification(mStatusHandle, &broadCastDeviceInterface, DEVICE_NOTIFY_SERVICE_HANDLE);


ServiceHandlerEx

DWORD WINAPI
Win32ServicesApp::ServiceControlEx(
DWORD inControl,
DWORD inEventType,
LPVOID inEventData,
LPVOID inContext)
{
#pragma unused (inContext)

switch (inControl)
{
case SERVICE_CONTROL_DEVICEEVENT:
{
// This is where I expect to catch arrival/removal events.
}
default:
debug_unexpected();
}
-----Original Message-----
From: Mark Roddy [mailto:xxxxx@hollistech.com]
Sent: Thu 9/19/2002 1:48 PM
To: NT Developers Interest List
Cc:
Subject: [ntdev] Re: “DBT_DEVICEARRIVAL” message in Win2000

You have to register for device notification or you will not receive
device notification. There are samples in the ddk that do this correctly.

===========================
Mark Roddy
Consultant, Microsoft DDK MVP
Hollis Technology Solutions
xxxxx@hollistech.com
www.hollistech.com
603-321-1032

-----Original Message-----
From: xxxxx@connectix.com
To: “NT Developers Interest List”
Date: Thu, 19 Sep 2002 15:23:43 -0400
Subject: [ntdev] Re: “DBT_DEVICEARRIVAL” message in Win2000

> Hi Anand,
>
> I am writing a service that needs to know about cd arrival and removal.
> Unfortunately my service does not get the DBT_DEVICEARRIVAL,
> DBT_DEVICEREMOVECOMPLETE events.
> My code looks very similar to your snippet below.
> I am running on wink server sp2.
>
> Do you need to explicitly register for device notification by using
> RegisterDevicenotification? I want to avoid doing that.
> Can you please tell me how you have setup your SERVICE_STATUS
> structure?
>
> Thanks.
> Parag.
>
> —
> You are currently subscribed to ntdev as: xxxxx@hollistech.com
> To unsubscribe send a blank email to %%email.unsub%%


You are currently subscribed to ntdev as: xxxxx@connectix.com
To unsubscribe send a blank email to %%email.unsub%%

> Parag Chakraborty wrote:

I used RegisterDevicenotification, but I still do not see any
arrival/removal events.

I’m assuming you’re calling RegisterDeviceNotification in your
ServiceMain function. It would also be a good idea to enumerate existing
interfaces at the same time, to pick up cases where devices start before
your service does. The AUTOLAUNCH sample in my WDM book shows exactly
how to do this.

In addition, you’ll only get notifications about device interfaces.
There was a lot of confusion prior to XP about which GUIDs were for
device interfaces and which were for other things, such as setup
classes. Beginning in the XP DDK, Microsoft started using the convention
that device interface GUIDs were declared with names beginning
GUID_DEVINTERFACE. The GUID you’re using – GUID_DEVCLASS_VOLUME – if
for a setup class rather than a device interface. There is a set of
interface guids for storage devices with names like DiskClassGuid,
VolumeClassGuid, etc., that *are* device interfaces. They’re defined in
NTDDSTOR.H. the INTERFACEENUM sample in my WDM book contains lists of
all the interface GUIDs I was able to find.


Walter Oney, Consulting and Training
Check out new US seminar schedule at http://www.oneysoft.com

See walter’s reply.

===========================
Mark Roddy
Consultant, Microsoft DDK MVP
Hollis Technology Solutions
xxxxx@hollistech.com
www.hollistech.com
603-321-1032

-----Original Message-----
From: “Parag Chakraborty”
To: “NT Developers Interest List”
Date: Fri, 20 Sep 2002 15:10:22 -0700
Subject: [ntdev] Re: “DBT_DEVICEARRIVAL” message in Win2000

> Mark,
> I used RegisterDevicenotification, but I still do not see any
> arrival/removal events.
> Parag.
>
> Here is how I set things up.
> --------------------------------------
> Service properties.
> --------------------------------------
>
> mCurrentServiceStatus.dwServiceType =
> SERVICE_WIN32_OWN_PROCESS;
> mCurrentServiceStatus.dwCurrentState = SERVICE_START_PENDING;
> mCurrentServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP |
> SERVICE_ACCEPT_HARDWAREPROFILECHANGE ;
> mCurrentServiceStatus.dwWin32ExitCode = 0;
> mCurrentServiceStatus.dwServiceSpecificExitCode = 0;
> mCurrentServiceStatus.dwCheckPoint = 0;
> mCurrentServiceStatus.dwWaitHint = 0;
>
> --------------------------------------
> RegisterDeviceNotification
> --------------------------------------
>
> DEV_BROADCAST_DEVICEINTERFACE broadCastDeviceInterface = {0};
>
> broadCastDeviceInterface.dbcc_size =
> sizeof(DEV_BROADCAST_DEVICEINTERFACE);
> broadCastDeviceInterface.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
>
> broadCastDeviceInterface.dbcc_classguid = GUID_DEVCLASS_VOLUME;
> //GUID_DEVCLASS_CDROM;
>
> RegisterDeviceNotification(mStatusHandle, &broadCastDeviceInterface,
> DEVICE_NOTIFY_SERVICE_HANDLE);
>
> --------------------------------------
> ServiceHandlerEx
> --------------------------------------
>
> DWORD WINAPI
> Win32ServicesApp::ServiceControlEx(
> DWORD inControl,
> DWORD inEventType,
> LPVOID inEventData,
> LPVOID inContext)
> {
> #pragma unused (inContext)
>
> switch (inControl)
> {
> case SERVICE_CONTROL_DEVICEEVENT:
> {
> // This is where I expect to catch arrival/removal events.
> }
> default:
> debug_unexpected();
> }
> -----Original Message-----
> From: Mark Roddy [mailto:xxxxx@hollistech.com]
> Sent: Thu 9/19/2002 1:48 PM
> To: NT Developers Interest List
> Cc:
> Subject: [ntdev] Re: “DBT_DEVICEARRIVAL” message in Win2000
>
>
> You have to register for device notification or you will not receive
> device notification. There are samples in the ddk that do this
> correctly.
>
> ===========================
> Mark Roddy
> Consultant, Microsoft DDK MVP
> Hollis Technology Solutions
> xxxxx@hollistech.com
> www.hollistech.com
> 603-321-1032
>
>
> -----Original Message-----
> From: xxxxx@connectix.com
> To: “NT Developers Interest List”
> Date: Thu, 19 Sep 2002 15:23:43 -0400
> Subject: [ntdev] Re: “DBT_DEVICEARRIVAL” message in Win2000
>
> > Hi Anand,
> >
> > I am writing a service that needs to know about cd arrival and
> removal.
> > Unfortunately my service does not get the DBT_DEVICEARRIVAL,
> > DBT_DEVICEREMOVECOMPLETE events.
> > My code looks very similar to your snippet below.
> > I am running on wink server sp2.
> >
> > Do you need to explicitly register for device notification by using
> > RegisterDevicenotification? I want to avoid doing that.
> > Can you please tell me how you have setup your SERVICE_STATUS
> > structure?
> >
> > Thanks.
> > Parag.
> >
> > —
> > You are currently subscribed to ntdev as: xxxxx@hollistech.com
> > To unsubscribe send a blank email to %%email.unsub%%
>
>
>
> —
> You are currently subscribed to ntdev as: xxxxx@connectix.com
> To unsubscribe send a blank email to %%email.unsub%%
>
>
>
> —
> You are currently subscribed to ntdev as: xxxxx@hollistech.com
> To unsubscribe send a blank email to %%email.unsub%%
>

Walter,

I am calling RegisterDeviceNotification from my ServiceMain function.
Thanks for correcting me about the device interface GUIDs.
Unfortunately, none of the following guids
{DiskClassGuid,MediumChangerClassGuid,VolumeClassGuid,CdRomClassGuid}
help me in getting DBT_DEVICEARRIVAL/ DBT_DEVICEREMOVECOMPLETE events for
cd media insertion/removal in my service handler.
I read your Autolaunch example. In the auto launch example, you register
your own device interface for notification. Since I am not writing a
driver for the cdrom device, I cannot do the same.
Your enumeration example, explains how to enumerate all devices for a
particular interface.

Correct me if I am wrong.
What I need to do is enumerate all interfaces registered to Device\Cdrom0.
Then try to register for notification on each of these interfaces one at a
time and find out which one fires the DBT_DEVICEARRIVAL/
DBT_DEVICEREMOVECOMPLETE event.

Is there a way to enumerate all the interfaces that are registered to a
particular device?

I greatly appreciate your help with this issue.

Thanks,
Parag.

> Parag Chakraborty wrote:
> I used RegisterDevicenotification, but I still do not see any
> arrival/removal events.

I’m assuming you’re calling RegisterDeviceNotification in your
ServiceMain function. It would also be a good idea to enumerate existing
interfaces at the same time, to pick up cases where devices start before
your service does. The AUTOLAUNCH sample in my WDM book shows exactly
how to do this.

In addition, you’ll only get notifications about device interfaces.
There was a lot of confusion prior to XP about which GUIDs were for
device interfaces and which were for other things, such as setup
classes. Beginning in the XP DDK, Microsoft started using the convention
that device interface GUIDs were declared with names beginning
GUID_DEVINTERFACE. The GUID you’re using – GUID_DEVCLASS_VOLUME – if
for a setup class rather than a device interface. There is a set of
interface guids for storage devices with names like DiskClassGuid,
VolumeClassGuid, etc., that *are* device interfaces. They’re defined in
NTDDSTOR.H. the INTERFACEENUM sample in my WDM book contains lists of
all the interface GUIDs I was able to find.


Walter Oney, Consulting and Training
Check out new US seminar schedule at http://www.oneysoft.com