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;
@@ -39,13 +40,13 @@ UINT __stdcall CreateScheduledTaskCA(MSIHANDLE hInstall) {
std::wstring wstrTaskName; std::wstring wstrTaskName;
ITaskService *pService = NULL; ITaskService* pService = NULL;
ITaskFolder *pTaskFolder = NULL; ITaskFolder* pTaskFolder = NULL;
ITaskDefinition *pTask = NULL; ITaskDefinition* pTask = NULL;
IRegistrationInfo *pRegInfo = NULL; IRegistrationInfo* pRegInfo = NULL;
ITaskSettings *pSettings = NULL; ITaskSettings* pSettings = NULL;
ITriggerCollection *pTriggerCollection = NULL; ITriggerCollection* pTriggerCollection = NULL;
IRegisteredTask *pRegisteredTask = NULL; IRegisteredTask* pRegisteredTask = NULL;
hr = WcaInitialize(hInstall, "CreateScheduledTaskCA"); hr = WcaInitialize(hInstall, "CreateScheduledTaskCA");
ExitOnFailure(hr, "Failed to initialize"); ExitOnFailure(hr, "Failed to initialize");
@@ -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);
} }
@@ -144,25 +148,27 @@ UINT __stdcall CreateScheduledTaskCA(MSIHANDLE hInstall) {
ExitOnFailure(hr, "Cannot get trigger collection: %x", hr); ExitOnFailure(hr, "Cannot get trigger collection: %x", hr);
// Add the logon trigger to the task. // Add the logon trigger to the task.
ITrigger *pTrigger = NULL; ITrigger* pTrigger = NULL;
hr = pTriggerCollection->Create(TASK_TRIGGER_LOGON, &pTrigger); hr = pTriggerCollection->Create(TASK_TRIGGER_LOGON, &pTrigger);
ExitOnFailure(hr, "Cannot create the trigger: %x", hr); ExitOnFailure(hr, "Cannot create the trigger: %x", hr);
ILogonTrigger *pLogonTrigger = NULL; ILogonTrigger* pLogonTrigger = NULL;
hr = pTrigger->QueryInterface( hr = pTrigger->QueryInterface(
IID_ILogonTrigger, (void**)&pLogonTrigger); IID_ILogonTrigger, (void**)&pLogonTrigger);
pTrigger->Release(); pTrigger->Release();
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);
} }
@@ -174,19 +180,19 @@ UINT __stdcall CreateScheduledTaskCA(MSIHANDLE hInstall) {
// ------------------------------------------------------ // ------------------------------------------------------
// Add an Action to the task. This task will execute the path passed to this custom action. // Add an Action to the task. This task will execute the path passed to this custom action.
IActionCollection *pActionCollection = NULL; IActionCollection* pActionCollection = NULL;
// Get the task action collection pointer. // Get the task action collection pointer.
hr = pTask->get_Actions(&pActionCollection); hr = pTask->get_Actions(&pActionCollection);
ExitOnFailure(hr, "Cannot get Task collection pointer: %x", hr); ExitOnFailure(hr, "Cannot get Task collection pointer: %x", hr);
// Create the action, specifying that it is an executable action. // Create the action, specifying that it is an executable action.
IAction *pAction = NULL; IAction* pAction = NULL;
hr = pActionCollection->Create(TASK_ACTION_EXEC, &pAction); hr = pActionCollection->Create(TASK_ACTION_EXEC, &pAction);
pActionCollection->Release(); pActionCollection->Release();
ExitOnFailure(hr, "Cannot create the action: %x", hr); ExitOnFailure(hr, "Cannot create the action: %x", hr);
IExecAction *pExecAction = NULL; IExecAction* pExecAction = NULL;
// QI for the executable task pointer. // QI for the executable task pointer.
hr = pAction->QueryInterface( hr = pAction->QueryInterface(
IID_IExecAction, (void**)&pExecAction); IID_IExecAction, (void**)&pExecAction);
@@ -200,23 +206,26 @@ UINT __stdcall CreateScheduledTaskCA(MSIHANDLE hInstall) {
// ------------------------------------------------------ // ------------------------------------------------------
// Create the principal for the task // Create the principal for the task
IPrincipal *pPrincipal = NULL; IPrincipal* pPrincipal = NULL;
hr = pTask->get_Principal(&pPrincipal); hr = pTask->get_Principal(&pPrincipal);
ExitOnFailure(hr, "Cannot get principal pointer: %x", hr); ExitOnFailure(hr, "Cannot get principal pointer: %x", hr);
// 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,12 +297,13 @@ 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;
ITaskService *pService = NULL; ITaskService* pService = NULL;
ITaskFolder *pTaskFolder = NULL; ITaskFolder* pTaskFolder = NULL;
IRegisteredTaskCollection* pTaskCollection = NULL; IRegisteredTaskCollection* pTaskCollection = NULL;
hr = WcaInitialize(hInstall, "RemoveScheduledTasksCA"); hr = WcaInitialize(hInstall, "RemoveScheduledTasksCA");
@@ -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,32 +344,40 @@ 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);
} }
} }
// ------------------------------------------------------ // ------------------------------------------------------
// Get the pointer to the root task folder and delete the PowerToys subfolder. // Get the pointer to the root task folder and delete 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->DeleteFolder(_bstr_t(L"PowerToys"), NULL); hr = pRootFolder->DeleteFolder(_bstr_t(L"PowerToys"), NULL);
@@ -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);