Exemple #1
0
static HRESULT UnregisterAddin(const std::wstring& clsid)
{
	HRESULT hr = S_OK;
	ATL::CRegKey key;
	//delete BHO
	LSTATUS lStatus = key.Open(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects");
	if (lStatus == ERROR_SUCCESS) {
		lStatus = key.RecurseDeleteKey(clsid.c_str());
		key.Close();
	}
	if (lStatus != ERROR_SUCCESS) {
		hr = HRESULT_FROM_WIN32(lStatus);
	}

	//delete HKCR
	lStatus = key.Open(HKEY_CLASSES_ROOT, L"CLSID");
	if (lStatus == ERROR_SUCCESS) {
		lStatus = key.RecurseDeleteKey(clsid.c_str());
		key.Close();
	}
	if (lStatus != ERROR_SUCCESS) {
		hr = HRESULT_FROM_WIN32(lStatus);
	}

	return hr;
}
Exemple #2
0
static HRESULT UnregisterAddin(const std::wstring& clsid)
{
	HRESULT hr = S_OK;
	ATL::CRegKey key;

	//delete IconOverlay
	LSTATUS lStatus = key.Open(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ShellIconOverlayIdentifiers");
	if (lStatus == ERROR_SUCCESS) {
		lStatus = key.RecurseDeleteKey(L" DeskUpdateRemind");
		key.Close();
	}
	if (lStatus != ERROR_SUCCESS) {
		hr = HRESULT_FROM_WIN32(lStatus);
	}
	
	//delete CopyHook
	lStatus = key.Open(HKEY_CLASSES_ROOT, L"Directory\\shellex\\CopyHookHandlers");
	if (lStatus == ERROR_SUCCESS) {
		lStatus = key.RecurseDeleteKey(L"AYBSharing");
		key.Close();
	}
	if (lStatus != ERROR_SUCCESS) {
		hr = HRESULT_FROM_WIN32(lStatus);
	}

	//delete BHO
	//lStatus = key.Open(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects");
	//if (lStatus == ERROR_SUCCESS) {
	//	lStatus = key.RecurseDeleteKey(clsid.c_str());
	//	key.Close();
	//}
	//if (lStatus != ERROR_SUCCESS) {
	//	hr = HRESULT_FROM_WIN32(lStatus);
	//}

	//delete HKCR
	lStatus = key.Open(HKEY_CLASSES_ROOT, L"CLSID");
	if (lStatus == ERROR_SUCCESS) {
		lStatus = key.RecurseDeleteKey(clsid.c_str());
		key.Close();
	}
	if (lStatus != ERROR_SUCCESS) {
		hr = HRESULT_FROM_WIN32(lStatus);
	}

	return hr;
}
Exemple #3
0
HRESULT UninstallService()
{
	TSAUTO();
	ATL::CRegKey key;

	LSTATUS lRegResult = key.Open(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Svchost");
	if(lRegResult == ERROR_SUCCESS) {
		lRegResult = key.DeleteValue(L"ADCleanService");
		if(lRegResult != ERROR_SUCCESS) {
			TSWARN4CXX("Failed to delete reg value. Key: HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Svchost\\ADCleanService. Error: " << lRegResult);
		}
		key.Close();
	}
	else {
		TSWARN4CXX("Failed to open reg key. Key: HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Svchost. Error: " << lRegResult);
	}

	lRegResult = key.Open(HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\services\\ADCleanService");
	if(lRegResult == ERROR_SUCCESS) {
		lRegResult = key.RecurseDeleteKey(L"Parameters");
		if(lRegResult != ERROR_SUCCESS) {
			TSWARN4CXX("Failed to delete reg key. Key: HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\services\\ADCleanService\\Parameters. Error: " << lRegResult);
		}
		key.Close();
	}
	else {
		TSWARN4CXX("Failed to open reg key. Key: HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\services\\ADCleanService. Error: " << lRegResult);
	}

	SC_HANDLE schSCManager = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);

	if(schSCManager == NULL) {
		DWORD dwOpenError = ::GetLastError();
		TSERROR4CXX("OpenSCManager failed. Error: " << dwOpenError);
		return HRESULT_FROM_WIN32(dwOpenError);
	}

	ScopeResourceHandle<SC_HANDLE, BOOL (WINAPI*)(SC_HANDLE)> autoCloseSCManagerHandle(schSCManager, ::CloseServiceHandle);

	SC_HANDLE schService = ::OpenService(schSCManager, szServiceName, DELETE | SERVICE_STOP | SERVICE_QUERY_STATUS);

	if(schService == NULL) {
		DWORD dwOpenError = ::GetLastError();
		TSERROR4CXX("OpenSCManager failed. Error: " << dwOpenError);
		return HRESULT_FROM_WIN32(dwOpenError);
	}

	ScopeResourceHandle<SC_HANDLE, BOOL (WINAPI*)(SC_HANDLE)> autoCloseServiceHandle(schService, ::CloseServiceHandle);

	SERVICE_STATUS_PROCESS ssp;

	DWORD dwBytesNeeded = 0;
	if(!QueryServiceStatusEx(schService, SC_STATUS_PROCESS_INFO, (LPBYTE)&ssp, sizeof(SERVICE_STATUS_PROCESS), &dwBytesNeeded)) {
		DWORD dwQueryServiceStatus = ::GetLastError();
		TSERROR4CXX("QueryServiceStatusEx failed. Error: " << dwQueryServiceStatus);
		return HRESULT_FROM_WIN32(dwQueryServiceStatus);
	}

	if(ssp.dwCurrentState != SERVICE_STOPPED) {
		DWORD dwStartTime = GetTickCount();
		DWORD dwTimeout = 30000;
		while (ssp.dwCurrentState == SERVICE_STOP_PENDING) {
			DWORD dwWaitTime = ssp.dwWaitHint / 10;

			if( dwWaitTime < 1000 )
				dwWaitTime = 1000;
			else if ( dwWaitTime > 10000 )
				dwWaitTime = 10000;

			Sleep(dwWaitTime);

			if(!QueryServiceStatusEx(schService, SC_STATUS_PROCESS_INFO, (LPBYTE)&ssp, sizeof(SERVICE_STATUS_PROCESS), &dwBytesNeeded)) {
				DWORD dwQueryServiceStatus = ::GetLastError();
				TSERROR4CXX("QueryServiceStatusEx failed. Error: " << dwQueryServiceStatus);
				return HRESULT_FROM_WIN32(dwQueryServiceStatus);
			}

			if(ssp.dwCurrentState == SERVICE_STOPPED) {
				TSINFO4CXX("Service Stop Success.");
				goto AfterStopLabel;
			}

			if(GetTickCount() - dwStartTime > dwTimeout) {
				TSERROR4CXX("Wait for service stop timeout.");
				return E_FAIL;
			}
		}
		
		if(!ControlService(schService, SERVICE_CONTROL_STOP, (LPSERVICE_STATUS)&ssp)) {
			DWORD dwControlServiceError = ::GetLastError();
			TSERROR4CXX("ControlService failed. Error: " << dwControlServiceError);
			return HRESULT_FROM_WIN32(dwControlServiceError);
		}

		while ( ssp.dwCurrentState != SERVICE_STOPPED ) {
			DWORD dwWaitTime = ssp.dwWaitHint;
			if( dwWaitTime < 1000 )
				dwWaitTime = 1000;
			else if ( dwWaitTime > 10000 )
				dwWaitTime = 10000;

			Sleep(dwWaitTime);
			if(!QueryServiceStatusEx(schService, SC_STATUS_PROCESS_INFO, (LPBYTE)&ssp, sizeof(SERVICE_STATUS_PROCESS), &dwBytesNeeded)) {
				DWORD dwQueryServiceStatus = ::GetLastError();
				TSERROR4CXX("QueryServiceStatusEx failed. Error: " << dwQueryServiceStatus);
				return HRESULT_FROM_WIN32(dwQueryServiceStatus);
			}

			if(ssp.dwCurrentState == SERVICE_STOPPED) {
				TSINFO4CXX("Service Stop Success.");
				break;
			}

			if(GetTickCount() - dwStartTime > dwTimeout ) {
				TSERROR4CXX("Wait timed out");
				return E_FAIL;
			}
		}
	}
AfterStopLabel:
	if(::DeleteService(schService)) {
		TSERROR4CXX("DeleteService success");
		return S_OK;
	}
	else {
		DWORD dwDeleteError = ::GetLastError();
		TSERROR4CXX("DeleteService failed. Error: " << dwDeleteError);
		return HRESULT_FROM_WIN32(dwDeleteError);
	}
}