16,472
社区成员
发帖
与我相关
我的任务
分享
DWORD dwFilter = REG_NOTIFY_CHANGE_NAME |
REG_NOTIFY_CHANGE_ATTRIBUTES |
REG_NOTIFY_CHANGE_LAST_SET |
REG_NOTIFY_CHANGE_SECURITY;
HANDLE hEvent = NULL;
HKEY hKey = NULL;
LONG lErrorCode = 0;
TCHAR SUBKEY[] = TEXT("SYSTEM\\CurrentControlSet\\Control\\Print\\Printers");
// Open a key.
lErrorCode = RegOpenKeyEx(HKEY_LOCAL_MACHINE, SUBKEY, 0, KEY_NOTIFY, &hKey);
if (lErrorCode != ERROR_SUCCESS)
{
#ifdef VPrinterLog
LOGBEGIN _tsprintf(szLog,TEXT("Error in RegOpenKeyEx (%d)."), lErrorCode); LOGEND
#endif
return;
}
// Create an event.
hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (hEvent == NULL)
{
#ifdef VPrinterLog
LOGBEGIN _tsprintf(szLog,TEXT("Error in CreateEvent (%d)."), GetLastError()); LOGEND
#endif
goto CleanUp;
}
while (1)
{
// Watch the registry key for a change of value.
lErrorCode = RegNotifyChangeKeyValue(hKey,TRUE,dwFilter,hEvent,TRUE);
if (lErrorCode != ERROR_SUCCESS)
{
#ifdef VPrinterLog
LOGBEGIN _tsprintf(szLog,TEXT("Error in RegNotifyChangeKeyValue (%d)."), lErrorCode); LOGEND
#endif
goto CleanUp;
}
// Wait for an event to occur.
DWORD dwRet = WaitForSingleObject(hEvent, INFINITE); //在WIN7 VISTA上,此处没有触发!
switch(dwRet)
{
case WAIT_FAILED:
{
#ifdef VPrinterLog
LOGBEGIN _tsprintf(szLog,TEXT("Error in WaitForSingleObject (%d)."), GetLastError()); LOGEND
#endif
goto CleanUp;
}
break;
case WAIT_TIMEOUT:
{
#ifdef VPrinterLog
LOGBEGIN _tsprintf(szLog,TEXT("TimeOut in WaitForSingleObject.")); LOGEND
#endif
DeleteInstallFile();
goto CleanUp;
}
break;
case WAIT_OBJECT_0:
{
ResetEvent( hEvent );
#ifdef VPrinterLog
LOGBEGIN _tsprintf(szLog,TEXT("Change has occurred.")); LOGEND
#endif
HKEY hRead = NULL;
TCHAR SUBKEY[] = TEXT("SYSTEM\\CurrentControlSet\\Controls\\dvprinter");
lErrorCode = RegOpenKeyEx(HKEY_LOCAL_MACHINE, SUBKEY, 0, KEY_READ, &hRead);
if (lErrorCode != ERROR_SUCCESS)
{
#ifdef VPrinterLog
LOGBEGIN _tsprintf(szLog,TEXT("Error in RegOpenKeyEx (%d)."), lErrorCode); LOGEND
#endif
continue;
}
DeleteInstallFile();
lErrorCode = RegCloseKey(hRead);
if (lErrorCode != ERROR_SUCCESS)
{
#ifdef VPrinterLog
LOGBEGIN _tsprintf(szLog,TEXT("Error in RegCloseKey (%d)."), GetLastError()); LOGEND
#endif
goto CleanUp;
}
goto CleanUp;
}
break;
default:
break;
}
}
CleanUp:
// Close the key.
if(hKey)
{
lErrorCode = RegCloseKey(hKey);
if (lErrorCode != ERROR_SUCCESS)
{
#ifdef VPrinterLog
LOGBEGIN _tsprintf(szLog,TEXT("Error in RegCloseKey (%d)."), GetLastError()); LOGEND
#endif
return;
}
}
// Close the handle.
if(hEvent)
{
if (!CloseHandle(hEvent))
{
#ifdef VPrinterLog
LOGBEGIN _tsprintf(szLog,TEXT("Error in CloseHandle.")); LOGEND
#endif
return;
}
}