mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 03:37:59 +01:00
chore: format PowerToys custom actions (#2104)
* chore: format PowerToys custom actions * add curly braces
This commit is contained in:
@@ -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).
|
||||
// Based on the Task Scheduler Logon Trigger Example:
|
||||
// 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;
|
||||
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,
|
||||
// so GetUserNameEx can't be used to get the current user details.
|
||||
// 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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
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);
|
||||
|
||||
// Connect to the task service.
|
||||
hr = pService->Connect(_variant_t(), _variant_t(),
|
||||
_variant_t(), _variant_t());
|
||||
hr = pService->Connect(_variant_t(), _variant_t(), _variant_t(), _variant_t());
|
||||
ExitOnFailure(hr, "ITaskService::Connect failed: %x", hr);
|
||||
|
||||
// ------------------------------------------------------
|
||||
// Get the PowerToys task folder. Creates it if it doesn't exist.
|
||||
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.
|
||||
ITaskFolder* pRootFolder = NULL;
|
||||
hr = pService->GetFolder(_bstr_t(L"\\"), &pRootFolder);
|
||||
ExitOnFailure(hr, "Cannot get Root Folder pointer: %x", hr);
|
||||
hr = pRootFolder->CreateFolder(_bstr_t(L"\\PowerToys"), _variant_t(L""), &pTaskFolder);
|
||||
if (FAILED(hr)) {
|
||||
if (FAILED(hr))
|
||||
{
|
||||
pRootFolder->Release();
|
||||
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);
|
||||
|
||||
hr = pLogonTrigger->put_Id(_bstr_t(L"Trigger1"));
|
||||
if (FAILED(hr)) {
|
||||
if (FAILED(hr))
|
||||
{
|
||||
WcaLogError(hr, "Cannot put the trigger ID: %x", hr);
|
||||
}
|
||||
|
||||
// Timing issues may make explorer not be started when the task runs.
|
||||
// Add a little delay to mitigate this.
|
||||
hr = pLogonTrigger->put_Delay(_bstr_t(L"PT03S"));
|
||||
if (FAILED(hr)) {
|
||||
if (FAILED(hr))
|
||||
{
|
||||
WcaLogError(hr, "Cannot put the trigger delay: %x", hr);
|
||||
}
|
||||
|
||||
@@ -206,17 +212,20 @@ UINT __stdcall CreateScheduledTaskCA(MSIHANDLE hInstall) {
|
||||
|
||||
// Set up principal information:
|
||||
hr = pPrincipal->put_Id(_bstr_t(L"Principal1"));
|
||||
if (FAILED(hr)) {
|
||||
if (FAILED(hr))
|
||||
{
|
||||
WcaLogError(hr, "Cannot put the principal ID: %x", hr);
|
||||
}
|
||||
|
||||
hr = pPrincipal->put_UserId(_bstr_t(username_domain));
|
||||
if (FAILED(hr)) {
|
||||
if (FAILED(hr))
|
||||
{
|
||||
WcaLogError(hr, "Cannot put principal user Id: %x", hr);
|
||||
}
|
||||
|
||||
hr = pPrincipal->put_LogonType(TASK_LOGON_INTERACTIVE_TOKEN);
|
||||
if (FAILED(hr)) {
|
||||
if (FAILED(hr))
|
||||
{
|
||||
WcaLogError(hr, "Cannot put principal logon type: %x", hr);
|
||||
}
|
||||
|
||||
@@ -245,15 +254,37 @@ UINT __stdcall CreateScheduledTaskCA(MSIHANDLE hInstall) {
|
||||
|
||||
LExit:
|
||||
ReleaseStr(wszExecutablePath);
|
||||
if (pService) pService->Release();
|
||||
if (pTaskFolder) pTaskFolder->Release();
|
||||
if (pTask) pTask->Release();
|
||||
if (pRegInfo) pRegInfo->Release();
|
||||
if (pSettings) pSettings->Release();
|
||||
if (pTriggerCollection) pTriggerCollection->Release();
|
||||
if (pRegisteredTask) pRegisteredTask->Release();
|
||||
if (pService)
|
||||
{
|
||||
pService->Release();
|
||||
}
|
||||
if (pTaskFolder)
|
||||
{
|
||||
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);
|
||||
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);
|
||||
@@ -266,7 +297,8 @@ LExit:
|
||||
// Removes all Scheduled Tasks in the PowerToys folder and deletes the folder afterwards.
|
||||
// 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---/
|
||||
UINT __stdcall RemoveScheduledTasksCA(MSIHANDLE hInstall) {
|
||||
UINT __stdcall RemoveScheduledTasksCA(MSIHANDLE hInstall)
|
||||
{
|
||||
HRESULT hr = S_OK;
|
||||
UINT er = ERROR_SUCCESS;
|
||||
|
||||
@@ -291,14 +323,14 @@ UINT __stdcall RemoveScheduledTasksCA(MSIHANDLE hInstall) {
|
||||
ExitOnFailure(hr, "Failed to create an instance of ITaskService: %x", hr);
|
||||
|
||||
// Connect to the task service.
|
||||
hr = pService->Connect(_variant_t(), _variant_t(),
|
||||
_variant_t(), _variant_t());
|
||||
hr = pService->Connect(_variant_t(), _variant_t(), _variant_t(), _variant_t());
|
||||
ExitOnFailure(hr, "ITaskService::Connect failed: %x", hr);
|
||||
|
||||
// ------------------------------------------------------
|
||||
// Get the PowerToys task folder.
|
||||
hr = pService->GetFolder(_bstr_t(L"\\PowerToys"), &pTaskFolder);
|
||||
if (FAILED(hr)) {
|
||||
if (FAILED(hr))
|
||||
{
|
||||
// Folder doesn't exist. No need to delete anything.
|
||||
WcaLog(LOGMSG_STANDARD, "The PowerToys scheduled task folder wasn't found. Nothing to delete.");
|
||||
hr = S_OK;
|
||||
@@ -312,25 +344,33 @@ UINT __stdcall RemoveScheduledTasksCA(MSIHANDLE hInstall) {
|
||||
|
||||
LONG numTasks = 0;
|
||||
hr = pTaskCollection->get_Count(&numTasks);
|
||||
for (LONG i = 0; i < numTasks; i++) {
|
||||
for (LONG i = 0; i < numTasks; i++)
|
||||
{
|
||||
// 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.
|
||||
IRegisteredTask* pRegisteredTask = NULL;
|
||||
hr = pTaskCollection->get_Item(_variant_t(i + 1), &pRegisteredTask);
|
||||
if (SUCCEEDED(hr)) {
|
||||
if (SUCCEEDED(hr))
|
||||
{
|
||||
BSTR taskName = NULL;
|
||||
hr = pRegisteredTask->get_Name(&taskName);
|
||||
if (SUCCEEDED(hr)) {
|
||||
if (SUCCEEDED(hr))
|
||||
{
|
||||
hr = pTaskFolder->DeleteTask(taskName, NULL);
|
||||
if (FAILED(hr)) {
|
||||
if (FAILED(hr))
|
||||
{
|
||||
WcaLogError(hr, "Cannot delete the '%S' task: %x", taskName, hr);
|
||||
}
|
||||
SysFreeString(taskName);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
WcaLogError(hr, "Cannot get the registered task name: %x", hr);
|
||||
}
|
||||
pRegisteredTask->Release();
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
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.");
|
||||
|
||||
LExit:
|
||||
if (pService) pService->Release();
|
||||
if (pTaskFolder) pTaskFolder->Release();
|
||||
if (pTaskCollection) pTaskCollection->Release();
|
||||
if (pService)
|
||||
{
|
||||
pService->Release();
|
||||
}
|
||||
if (pTaskFolder)
|
||||
{
|
||||
pTaskFolder->Release();
|
||||
}
|
||||
if (pTaskCollection)
|
||||
{
|
||||
pTaskCollection->Release();
|
||||
}
|
||||
|
||||
if (!SUCCEEDED(hr)) {
|
||||
if (!SUCCEEDED(hr))
|
||||
{
|
||||
PMSIHANDLE hRecord = MsiCreateRecord(0);
|
||||
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);
|
||||
@@ -361,7 +411,8 @@ LExit:
|
||||
return WcaFinalize(er);
|
||||
}
|
||||
|
||||
UINT __stdcall TelemetryLogInstallSuccessCA(MSIHANDLE hInstall) {
|
||||
UINT __stdcall TelemetryLogInstallSuccessCA(MSIHANDLE hInstall)
|
||||
{
|
||||
HRESULT hr = S_OK;
|
||||
UINT er = ERROR_SUCCESS;
|
||||
|
||||
@@ -380,7 +431,8 @@ LExit:
|
||||
return WcaFinalize(er);
|
||||
}
|
||||
|
||||
UINT __stdcall TelemetryLogInstallCancelCA(MSIHANDLE hInstall) {
|
||||
UINT __stdcall TelemetryLogInstallCancelCA(MSIHANDLE hInstall)
|
||||
{
|
||||
HRESULT hr = S_OK;
|
||||
UINT er = ERROR_SUCCESS;
|
||||
|
||||
@@ -399,7 +451,8 @@ LExit:
|
||||
return WcaFinalize(er);
|
||||
}
|
||||
|
||||
UINT __stdcall TelemetryLogInstallFailCA(MSIHANDLE hInstall) {
|
||||
UINT __stdcall TelemetryLogInstallFailCA(MSIHANDLE hInstall)
|
||||
{
|
||||
HRESULT hr = S_OK;
|
||||
UINT er = ERROR_SUCCESS;
|
||||
|
||||
@@ -418,7 +471,8 @@ LExit:
|
||||
return WcaFinalize(er);
|
||||
}
|
||||
|
||||
UINT __stdcall TelemetryLogUninstallSuccessCA(MSIHANDLE hInstall) {
|
||||
UINT __stdcall TelemetryLogUninstallSuccessCA(MSIHANDLE hInstall)
|
||||
{
|
||||
HRESULT hr = S_OK;
|
||||
UINT er = ERROR_SUCCESS;
|
||||
|
||||
@@ -437,7 +491,8 @@ LExit:
|
||||
return WcaFinalize(er);
|
||||
}
|
||||
|
||||
UINT __stdcall TelemetryLogUninstallCancelCA(MSIHANDLE hInstall) {
|
||||
UINT __stdcall TelemetryLogUninstallCancelCA(MSIHANDLE hInstall)
|
||||
{
|
||||
HRESULT hr = S_OK;
|
||||
UINT er = ERROR_SUCCESS;
|
||||
|
||||
@@ -456,7 +511,8 @@ LExit:
|
||||
return WcaFinalize(er);
|
||||
}
|
||||
|
||||
UINT __stdcall TelemetryLogUninstallFailCA(MSIHANDLE hInstall) {
|
||||
UINT __stdcall TelemetryLogUninstallFailCA(MSIHANDLE hInstall)
|
||||
{
|
||||
HRESULT hr = S_OK;
|
||||
UINT er = ERROR_SUCCESS;
|
||||
|
||||
@@ -475,7 +531,8 @@ LExit:
|
||||
return WcaFinalize(er);
|
||||
}
|
||||
|
||||
UINT __stdcall TelemetryLogRepairCancelCA(MSIHANDLE hInstall) {
|
||||
UINT __stdcall TelemetryLogRepairCancelCA(MSIHANDLE hInstall)
|
||||
{
|
||||
HRESULT hr = S_OK;
|
||||
UINT er = ERROR_SUCCESS;
|
||||
|
||||
@@ -494,7 +551,8 @@ LExit:
|
||||
return WcaFinalize(er);
|
||||
}
|
||||
|
||||
UINT __stdcall TelemetryLogRepairFailCA(MSIHANDLE hInstall) {
|
||||
UINT __stdcall TelemetryLogRepairFailCA(MSIHANDLE hInstall)
|
||||
{
|
||||
HRESULT hr = S_OK;
|
||||
UINT er = ERROR_SUCCESS;
|
||||
|
||||
@@ -514,8 +572,10 @@ LExit:
|
||||
}
|
||||
|
||||
// DllMain - Initialize and cleanup WiX custom action utils.
|
||||
extern "C" BOOL WINAPI DllMain(__in HINSTANCE hInst, __in ULONG ulReason, __in LPVOID) {
|
||||
switch (ulReason) {
|
||||
extern "C" BOOL WINAPI DllMain(__in HINSTANCE hInst, __in ULONG ulReason, __in LPVOID)
|
||||
{
|
||||
switch (ulReason)
|
||||
{
|
||||
case DLL_PROCESS_ATTACH:
|
||||
WcaGlobalInitialize(hInst);
|
||||
TraceLoggingRegister(g_hProvider);
|
||||
|
||||
Reference in New Issue
Block a user