Fix calling conventions and do not unload setupapi.dll after registering the notification. Thank you Thomas.

svn path=/trunk/; revision=29206
This commit is contained in:
James Tabor 2007-09-26 01:10:51 +00:00
parent d42fa2d392
commit 9f930ed173

View file

@ -9,14 +9,17 @@
#define CR_INVALID_DATA 0x0000001F #define CR_INVALID_DATA 0x0000001F
#endif #endif
typedef DWORD (*CMP_REGNOTIFY) (HANDLE, LPVOID, DWORD, PULONG); typedef DWORD STDCALL (*CMP_REGNOTIFY) (HANDLE, LPVOID, DWORD, PULONG);
typedef DWORD (*CMP_UNREGNOTIFY) (ULONG ); typedef DWORD STDCALL (*CMP_UNREGNOTIFY) (ULONG );
/* FIXME: Currently IsBadWritePtr is implemented using VirtualQuery which /* FIXME: Currently IsBadWritePtr is implemented using VirtualQuery which
does not seem to work properly for stack address space. */ does not seem to work properly for stack address space. */
/* kill `left-hand operand of comma expression has no effect' warning */ /* kill `left-hand operand of comma expression has no effect' warning */
#define IsBadWritePtr(lp, n) ((DWORD)lp==n?0:0) #define IsBadWritePtr(lp, n) ((DWORD)lp==n?0:0)
static HINSTANCE hSetupApi = NULL;
BOOL STDCALL _InternalLoadString BOOL STDCALL _InternalLoadString
( (
HINSTANCE hInstance, HINSTANCE hInstance,
@ -253,16 +256,16 @@ RegisterDeviceNotificationW(
DWORD ConfigRet = 0; DWORD ConfigRet = 0;
CMP_REGNOTIFY RegNotify = NULL; CMP_REGNOTIFY RegNotify = NULL;
HDEVNOTIFY hDevNotify = NULL; HDEVNOTIFY hDevNotify = NULL;
HINSTANCE hSetupApi = LoadLibraryA("SETUPAPI.DLL"); if ( hSetupApi == NULL ) hSetupApi = LoadLibraryA("SETUPAPI.DLL");
if (hSetupApi == NULL) return NULL; if ( hSetupApi == NULL ) return NULL;
RegNotify = (CMP_REGNOTIFY) GetProcAddress ( hSetupApi, "CMP_RegisterNotification"); RegNotify = (CMP_REGNOTIFY) GetProcAddress ( hSetupApi, "CMP_RegisterNotification");
if (RegNotify == NULL) if (RegNotify == NULL)
{ {
FreeLibrary ( hSetupApi ); FreeLibrary ( hSetupApi );
hSetupApi = NULL;
return NULL; return NULL;
} }
ConfigRet = RegNotify ( hRecipient, NotificationFilter, Flags, (PULONG) &hDevNotify); ConfigRet = RegNotify ( hRecipient, NotificationFilter, Flags, (PULONG) &hDevNotify);
FreeLibrary ( hSetupApi );
if (ConfigRet != CR_SUCCESS) if (ConfigRet != CR_SUCCESS)
{ {
switch (ConfigRet) switch (ConfigRet)
@ -295,16 +298,16 @@ UnregisterDeviceNotification(
{ {
DWORD ConfigRet = 0; DWORD ConfigRet = 0;
CMP_UNREGNOTIFY UnRegNotify = NULL; CMP_UNREGNOTIFY UnRegNotify = NULL;
HINSTANCE hSetupApi = LoadLibraryA("SETUPAPI.DLL"); if ( hSetupApi == NULL ) hSetupApi = LoadLibraryA("SETUPAPI.DLL");
if (hSetupApi == NULL) return FALSE; if ( hSetupApi == NULL ) return FALSE;
UnRegNotify = (CMP_UNREGNOTIFY) GetProcAddress ( hSetupApi, "CMP_UnregisterNotification"); UnRegNotify = (CMP_UNREGNOTIFY) GetProcAddress ( hSetupApi, "CMP_UnregisterNotification");
if (UnRegNotify == NULL) if (UnRegNotify == NULL)
{ {
FreeLibrary ( hSetupApi ); FreeLibrary ( hSetupApi );
hSetupApi = NULL;
return FALSE; return FALSE;
} }
ConfigRet = UnRegNotify ( (ULONG) Handle ); ConfigRet = UnRegNotify ( (ULONG) Handle );
FreeLibrary ( hSetupApi );
if (ConfigRet != CR_SUCCESS) if (ConfigRet != CR_SUCCESS)
{ {
switch (ConfigRet) switch (ConfigRet)