chore: format PowerToys custom actions (#2104)

* chore: format PowerToys custom actions

* add curly braces
This commit is contained in:
Andrey Nekrasov
2020-04-13 16:00:51 +03:00
committed by GitHub
parent 77e4984468
commit 6bb0f18d53

View File

@@ -30,7 +30,8 @@ const DWORD USERNAME_LEN = UNLEN + 1; // User Name + '\0'
// The path of the executable to run should be passed as the CustomActionData (Value). // The path of the executable to run should be passed as the CustomActionData (Value).
// Based on the Task Scheduler Logon Trigger Example: // Based on the Task Scheduler Logon Trigger Example:
// https://docs.microsoft.com/en-us/windows/win32/taskschd/logon-trigger-example--c---/ // https://docs.microsoft.com/en-us/windows/win32/taskschd/logon-trigger-example--c---/
UINT __stdcall CreateScheduledTaskCA(MSIHANDLE hInstall) { UINT __stdcall CreateScheduledTaskCA(MSIHANDLE hInstall)
{
HRESULT hr = S_OK; HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS; UINT er = ERROR_SUCCESS;
@@ -58,10 +59,12 @@ UINT __stdcall CreateScheduledTaskCA(MSIHANDLE hInstall) {
// This action needs to run as the system to get elevated privileges from the installation, // This action needs to run as the system to get elevated privileges from the installation,
// so GetUserNameEx can't be used to get the current user details. // so GetUserNameEx can't be used to get the current user details.
// The USERNAME and USERDOMAIN environment variables are used instead. // The USERNAME and USERDOMAIN environment variables are used instead.
if (!GetEnvironmentVariable(L"USERNAME", username, USERNAME_LEN)) { if (!GetEnvironmentVariable(L"USERNAME", username, USERNAME_LEN))
{
ExitWithLastError(hr, "Getting username failed: %x", hr); ExitWithLastError(hr, "Getting username failed: %x", hr);
} }
if (!GetEnvironmentVariable(L"USERDOMAIN", username_domain, USERNAME_DOMAIN_LEN)) { if (!GetEnvironmentVariable(L"USERDOMAIN", username_domain, USERNAME_DOMAIN_LEN))
{
ExitWithLastError(hr, "Getting the user's domain failed: %x", hr); ExitWithLastError(hr, "Getting the user's domain failed: %x", hr);
} }
wcscat_s(username_domain, L"\\"); wcscat_s(username_domain, L"\\");
@@ -90,20 +93,21 @@ UINT __stdcall CreateScheduledTaskCA(MSIHANDLE hInstall) {
ExitOnFailure(hr, "Failed to create an instance of ITaskService: %x", hr); ExitOnFailure(hr, "Failed to create an instance of ITaskService: %x", hr);
// Connect to the task service. // Connect to the task service.
hr = pService->Connect(_variant_t(), _variant_t(), hr = pService->Connect(_variant_t(), _variant_t(), _variant_t(), _variant_t());
_variant_t(), _variant_t());
ExitOnFailure(hr, "ITaskService::Connect failed: %x", hr); ExitOnFailure(hr, "ITaskService::Connect failed: %x", hr);
// ------------------------------------------------------ // ------------------------------------------------------
// Get the PowerToys task folder. Creates it if it doesn't exist. // Get the PowerToys task folder. Creates it if it doesn't exist.
hr = pService->GetFolder(_bstr_t(L"\\PowerToys"), &pTaskFolder); hr = pService->GetFolder(_bstr_t(L"\\PowerToys"), &pTaskFolder);
if (FAILED(hr)) { if (FAILED(hr))
{
// Folder doesn't exist. Get the Root folder and create the PowerToys subfolder. // Folder doesn't exist. Get the Root folder and create the PowerToys subfolder.
ITaskFolder* pRootFolder = NULL; ITaskFolder* pRootFolder = NULL;
hr = pService->GetFolder(_bstr_t(L"\\"), &pRootFolder); hr = pService->GetFolder(_bstr_t(L"\\"), &pRootFolder);
ExitOnFailure(hr, "Cannot get Root Folder pointer: %x", hr); ExitOnFailure(hr, "Cannot get Root Folder pointer: %x", hr);
hr = pRootFolder->CreateFolder(_bstr_t(L"\\PowerToys"), _variant_t(L""), &pTaskFolder); hr = pRootFolder->CreateFolder(_bstr_t(L"\\PowerToys"), _variant_t(L""), &pTaskFolder);
if (FAILED(hr)) { if (FAILED(hr))
{
pRootFolder->Release(); pRootFolder->Release();
ExitOnFailure(hr, "Cannot create PowerToys task folder: %x", hr); ExitOnFailure(hr, "Cannot create PowerToys task folder: %x", hr);
} }
@@ -155,14 +159,16 @@ UINT __stdcall CreateScheduledTaskCA(MSIHANDLE hInstall) {
ExitOnFailure(hr, "QueryInterface call failed for ILogonTrigger: %x", hr); ExitOnFailure(hr, "QueryInterface call failed for ILogonTrigger: %x", hr);
hr = pLogonTrigger->put_Id(_bstr_t(L"Trigger1")); hr = pLogonTrigger->put_Id(_bstr_t(L"Trigger1"));
if (FAILED(hr)) { if (FAILED(hr))
{
WcaLogError(hr, "Cannot put the trigger ID: %x", hr); WcaLogError(hr, "Cannot put the trigger ID: %x", hr);
} }
// Timing issues may make explorer not be started when the task runs. // Timing issues may make explorer not be started when the task runs.
// Add a little delay to mitigate this. // Add a little delay to mitigate this.
hr = pLogonTrigger->put_Delay(_bstr_t(L"PT03S")); hr = pLogonTrigger->put_Delay(_bstr_t(L"PT03S"));
if (FAILED(hr)) { if (FAILED(hr))
{
WcaLogError(hr, "Cannot put the trigger delay: %x", hr); WcaLogError(hr, "Cannot put the trigger delay: %x", hr);
} }
@@ -206,17 +212,20 @@ UINT __stdcall CreateScheduledTaskCA(MSIHANDLE hInstall) {
// Set up principal information: // Set up principal information:
hr = pPrincipal->put_Id(_bstr_t(L"Principal1")); hr = pPrincipal->put_Id(_bstr_t(L"Principal1"));
if (FAILED(hr)) { if (FAILED(hr))
{
WcaLogError(hr, "Cannot put the principal ID: %x", hr); WcaLogError(hr, "Cannot put the principal ID: %x", hr);
} }
hr = pPrincipal->put_UserId(_bstr_t(username_domain)); hr = pPrincipal->put_UserId(_bstr_t(username_domain));
if (FAILED(hr)) { if (FAILED(hr))
{
WcaLogError(hr, "Cannot put principal user Id: %x", hr); WcaLogError(hr, "Cannot put principal user Id: %x", hr);
} }
hr = pPrincipal->put_LogonType(TASK_LOGON_INTERACTIVE_TOKEN); hr = pPrincipal->put_LogonType(TASK_LOGON_INTERACTIVE_TOKEN);
if (FAILED(hr)) { if (FAILED(hr))
{
WcaLogError(hr, "Cannot put principal logon type: %x", hr); WcaLogError(hr, "Cannot put principal logon type: %x", hr);
} }
@@ -245,15 +254,37 @@ UINT __stdcall CreateScheduledTaskCA(MSIHANDLE hInstall) {
LExit: LExit:
ReleaseStr(wszExecutablePath); ReleaseStr(wszExecutablePath);
if (pService) pService->Release(); if (pService)
if (pTaskFolder) pTaskFolder->Release(); {
if (pTask) pTask->Release(); pService->Release();
if (pRegInfo) pRegInfo->Release(); }
if (pSettings) pSettings->Release(); if (pTaskFolder)
if (pTriggerCollection) pTriggerCollection->Release(); {
if (pRegisteredTask) pRegisteredTask->Release(); pTaskFolder->Release();
}
if (pTask)
{
pTask->Release();
}
if (pRegInfo)
{
pRegInfo->Release();
}
if (pSettings)
{
pSettings->Release();
}
if (pTriggerCollection)
{
pTriggerCollection->Release();
}
if (pRegisteredTask)
{
pRegisteredTask->Release();
}
if (!SUCCEEDED(hr)) { if (!SUCCEEDED(hr))
{
PMSIHANDLE hRecord = MsiCreateRecord(0); PMSIHANDLE hRecord = MsiCreateRecord(0);
MsiRecordSetString(hRecord, 0, TEXT("Failed to create a scheduled task to start PowerToys at user login. You can re-try to create the scheduled task using the PowerToys settings.")); MsiRecordSetString(hRecord, 0, TEXT("Failed to create a scheduled task to start PowerToys at user login. You can re-try to create the scheduled task using the PowerToys settings."));
MsiProcessMessage(hInstall, INSTALLMESSAGE(INSTALLMESSAGE_WARNING + MB_OK), hRecord); MsiProcessMessage(hInstall, INSTALLMESSAGE(INSTALLMESSAGE_WARNING + MB_OK), hRecord);
@@ -266,7 +297,8 @@ LExit:
// Removes all Scheduled Tasks in the PowerToys folder and deletes the folder afterwards. // Removes all Scheduled Tasks in the PowerToys folder and deletes the folder afterwards.
// Based on the Task Scheduler Displaying Task Names and State example: // Based on the Task Scheduler Displaying Task Names and State example:
// https://docs.microsoft.com/en-us/windows/desktop/TaskSchd/displaying-task-names-and-state--c---/ // https://docs.microsoft.com/en-us/windows/desktop/TaskSchd/displaying-task-names-and-state--c---/
UINT __stdcall RemoveScheduledTasksCA(MSIHANDLE hInstall) { UINT __stdcall RemoveScheduledTasksCA(MSIHANDLE hInstall)
{
HRESULT hr = S_OK; HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS; UINT er = ERROR_SUCCESS;
@@ -291,14 +323,14 @@ UINT __stdcall RemoveScheduledTasksCA(MSIHANDLE hInstall) {
ExitOnFailure(hr, "Failed to create an instance of ITaskService: %x", hr); ExitOnFailure(hr, "Failed to create an instance of ITaskService: %x", hr);
// Connect to the task service. // Connect to the task service.
hr = pService->Connect(_variant_t(), _variant_t(), hr = pService->Connect(_variant_t(), _variant_t(), _variant_t(), _variant_t());
_variant_t(), _variant_t());
ExitOnFailure(hr, "ITaskService::Connect failed: %x", hr); ExitOnFailure(hr, "ITaskService::Connect failed: %x", hr);
// ------------------------------------------------------ // ------------------------------------------------------
// Get the PowerToys task folder. // Get the PowerToys task folder.
hr = pService->GetFolder(_bstr_t(L"\\PowerToys"), &pTaskFolder); hr = pService->GetFolder(_bstr_t(L"\\PowerToys"), &pTaskFolder);
if (FAILED(hr)) { if (FAILED(hr))
{
// Folder doesn't exist. No need to delete anything. // Folder doesn't exist. No need to delete anything.
WcaLog(LOGMSG_STANDARD, "The PowerToys scheduled task folder wasn't found. Nothing to delete."); WcaLog(LOGMSG_STANDARD, "The PowerToys scheduled task folder wasn't found. Nothing to delete.");
hr = S_OK; hr = S_OK;
@@ -312,25 +344,33 @@ UINT __stdcall RemoveScheduledTasksCA(MSIHANDLE hInstall) {
LONG numTasks = 0; LONG numTasks = 0;
hr = pTaskCollection->get_Count(&numTasks); hr = pTaskCollection->get_Count(&numTasks);
for (LONG i = 0; i < numTasks; i++) { for (LONG i = 0; i < numTasks; i++)
{
// Delete all the tasks found. // Delete all the tasks found.
// If some tasks can't be deleted, the folder won't be deleted later and the user will still be notified. // If some tasks can't be deleted, the folder won't be deleted later and the user will still be notified.
IRegisteredTask* pRegisteredTask = NULL; IRegisteredTask* pRegisteredTask = NULL;
hr = pTaskCollection->get_Item(_variant_t(i + 1), &pRegisteredTask); hr = pTaskCollection->get_Item(_variant_t(i + 1), &pRegisteredTask);
if (SUCCEEDED(hr)) { if (SUCCEEDED(hr))
{
BSTR taskName = NULL; BSTR taskName = NULL;
hr = pRegisteredTask->get_Name(&taskName); hr = pRegisteredTask->get_Name(&taskName);
if (SUCCEEDED(hr)) { if (SUCCEEDED(hr))
{
hr = pTaskFolder->DeleteTask(taskName, NULL); hr = pTaskFolder->DeleteTask(taskName, NULL);
if (FAILED(hr)) { if (FAILED(hr))
{
WcaLogError(hr, "Cannot delete the '%S' task: %x", taskName, hr); WcaLogError(hr, "Cannot delete the '%S' task: %x", taskName, hr);
} }
SysFreeString(taskName); SysFreeString(taskName);
} else { }
else
{
WcaLogError(hr, "Cannot get the registered task name: %x", hr); WcaLogError(hr, "Cannot get the registered task name: %x", hr);
} }
pRegisteredTask->Release(); pRegisteredTask->Release();
} else { }
else
{
WcaLogError(hr, "Cannot get the registered task item at index=%d: %x", i + 1, hr); WcaLogError(hr, "Cannot get the registered task item at index=%d: %x", i + 1, hr);
} }
} }
@@ -347,11 +387,21 @@ UINT __stdcall RemoveScheduledTasksCA(MSIHANDLE hInstall) {
WcaLog(LOGMSG_STANDARD, "Deleted the PowerToys Task Scheduler folder."); WcaLog(LOGMSG_STANDARD, "Deleted the PowerToys Task Scheduler folder.");
LExit: LExit:
if (pService) pService->Release(); if (pService)
if (pTaskFolder) pTaskFolder->Release(); {
if (pTaskCollection) pTaskCollection->Release(); pService->Release();
}
if (pTaskFolder)
{
pTaskFolder->Release();
}
if (pTaskCollection)
{
pTaskCollection->Release();
}
if (!SUCCEEDED(hr)) { if (!SUCCEEDED(hr))
{
PMSIHANDLE hRecord = MsiCreateRecord(0); PMSIHANDLE hRecord = MsiCreateRecord(0);
MsiRecordSetString(hRecord, 0, TEXT("Failed to remove the PowerToys folder from the scheduled task. These can be removed manually later.")); MsiRecordSetString(hRecord, 0, TEXT("Failed to remove the PowerToys folder from the scheduled task. These can be removed manually later."));
MsiProcessMessage(hInstall, INSTALLMESSAGE(INSTALLMESSAGE_WARNING + MB_OK), hRecord); MsiProcessMessage(hInstall, INSTALLMESSAGE(INSTALLMESSAGE_WARNING + MB_OK), hRecord);
@@ -361,7 +411,8 @@ LExit:
return WcaFinalize(er); return WcaFinalize(er);
} }
UINT __stdcall TelemetryLogInstallSuccessCA(MSIHANDLE hInstall) { UINT __stdcall TelemetryLogInstallSuccessCA(MSIHANDLE hInstall)
{
HRESULT hr = S_OK; HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS; UINT er = ERROR_SUCCESS;
@@ -380,7 +431,8 @@ LExit:
return WcaFinalize(er); return WcaFinalize(er);
} }
UINT __stdcall TelemetryLogInstallCancelCA(MSIHANDLE hInstall) { UINT __stdcall TelemetryLogInstallCancelCA(MSIHANDLE hInstall)
{
HRESULT hr = S_OK; HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS; UINT er = ERROR_SUCCESS;
@@ -399,7 +451,8 @@ LExit:
return WcaFinalize(er); return WcaFinalize(er);
} }
UINT __stdcall TelemetryLogInstallFailCA(MSIHANDLE hInstall) { UINT __stdcall TelemetryLogInstallFailCA(MSIHANDLE hInstall)
{
HRESULT hr = S_OK; HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS; UINT er = ERROR_SUCCESS;
@@ -418,7 +471,8 @@ LExit:
return WcaFinalize(er); return WcaFinalize(er);
} }
UINT __stdcall TelemetryLogUninstallSuccessCA(MSIHANDLE hInstall) { UINT __stdcall TelemetryLogUninstallSuccessCA(MSIHANDLE hInstall)
{
HRESULT hr = S_OK; HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS; UINT er = ERROR_SUCCESS;
@@ -437,7 +491,8 @@ LExit:
return WcaFinalize(er); return WcaFinalize(er);
} }
UINT __stdcall TelemetryLogUninstallCancelCA(MSIHANDLE hInstall) { UINT __stdcall TelemetryLogUninstallCancelCA(MSIHANDLE hInstall)
{
HRESULT hr = S_OK; HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS; UINT er = ERROR_SUCCESS;
@@ -456,7 +511,8 @@ LExit:
return WcaFinalize(er); return WcaFinalize(er);
} }
UINT __stdcall TelemetryLogUninstallFailCA(MSIHANDLE hInstall) { UINT __stdcall TelemetryLogUninstallFailCA(MSIHANDLE hInstall)
{
HRESULT hr = S_OK; HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS; UINT er = ERROR_SUCCESS;
@@ -475,7 +531,8 @@ LExit:
return WcaFinalize(er); return WcaFinalize(er);
} }
UINT __stdcall TelemetryLogRepairCancelCA(MSIHANDLE hInstall) { UINT __stdcall TelemetryLogRepairCancelCA(MSIHANDLE hInstall)
{
HRESULT hr = S_OK; HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS; UINT er = ERROR_SUCCESS;
@@ -494,7 +551,8 @@ LExit:
return WcaFinalize(er); return WcaFinalize(er);
} }
UINT __stdcall TelemetryLogRepairFailCA(MSIHANDLE hInstall) { UINT __stdcall TelemetryLogRepairFailCA(MSIHANDLE hInstall)
{
HRESULT hr = S_OK; HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS; UINT er = ERROR_SUCCESS;
@@ -514,8 +572,10 @@ LExit:
} }
// DllMain - Initialize and cleanup WiX custom action utils. // DllMain - Initialize and cleanup WiX custom action utils.
extern "C" BOOL WINAPI DllMain(__in HINSTANCE hInst, __in ULONG ulReason, __in LPVOID) { extern "C" BOOL WINAPI DllMain(__in HINSTANCE hInst, __in ULONG ulReason, __in LPVOID)
switch (ulReason) { {
switch (ulReason)
{
case DLL_PROCESS_ATTACH: case DLL_PROCESS_ATTACH:
WcaGlobalInitialize(hInst); WcaGlobalInitialize(hInst);
TraceLoggingRegister(g_hProvider); TraceLoggingRegister(g_hProvider);