/ src / ActionRunner / actionRunner.cpp
actionRunner.cpp
  1  // Copyright (c) Microsoft Corporation
  2  // The Microsoft Corporation licenses this file to you under the MIT license.
  3  // See the LICENSE file in the project root for more information.
  4  
  5  #define WIN32_LEAN_AND_MEAN
  6  #include "Generated Files/resource.h"
  7  
  8  #include <Windows.h>
  9  #include <shellapi.h>
 10  
 11  #include <filesystem>
 12  #include <string_view>
 13  
 14  #include <common/utils/elevation.h>
 15  #include <common/utils/process_path.h>
 16  #include <common/utils/resources.h>
 17  #include <common/utils/timeutil.h>
 18  
 19  #include <common/SettingsAPI/settings_helpers.h>
 20  
 21  #include <common/logger/logger.h>
 22  
 23  #include <winrt/Windows.ApplicationModel.h>
 24  #include <winrt/Windows.Storage.h>
 25  
 26  #include "../runner/tray_icon.h"
 27  #include "../runner/ActionRunnerUtils.h"
 28  
 29  using namespace cmdArg;
 30  
 31  int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
 32  {
 33      int nArgs = 0;
 34      LPWSTR* args = CommandLineToArgvW(GetCommandLineW(), &nArgs);
 35      if (!args || nArgs < 2)
 36      {
 37          return 1;
 38      }
 39  
 40      std::wstring_view action{ args[1] };
 41  
 42      std::filesystem::path logFilePath(PTSettingsHelper::get_root_save_folder_location());
 43      logFilePath.append(LogSettings::actionRunnerLogPath);
 44      Logger::init(LogSettings::actionRunnerLoggerName, logFilePath.wstring(), PTSettingsHelper::get_log_settings_file_location());
 45  
 46      if (action == RUN_NONELEVATED)
 47      {
 48          int nextArg = 2;
 49  
 50          std::wstring_view target;
 51          std::wstring_view pidFile;
 52          std::wstring params;
 53  
 54          while (nextArg < nArgs)
 55          {
 56              if (std::wstring_view(args[nextArg]) == L"-target" && nextArg + 1 < nArgs)
 57              {
 58                  target = args[nextArg + 1];
 59                  nextArg += 2;
 60              }
 61              else if (std::wstring_view(args[nextArg]) == L"-pidFile" && nextArg + 1 < nArgs)
 62              {
 63                  pidFile = args[nextArg + 1];
 64                  nextArg += 2;
 65              }
 66              else
 67              {
 68                  params += args[nextArg];
 69                  params += L' ';
 70                  nextArg++;
 71              }
 72          }
 73  
 74          HANDLE hMapFile = NULL;
 75          PDWORD pidBuffer = NULL;
 76  
 77          if (!pidFile.empty())
 78          {
 79              hMapFile = OpenFileMappingW(FILE_MAP_WRITE, FALSE, pidFile.data());
 80              if (hMapFile)
 81              {
 82                  pidBuffer = static_cast<PDWORD>(MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(DWORD)));
 83                  if (pidBuffer)
 84                  {
 85                      *pidBuffer = 0;
 86                  }
 87              }
 88          }
 89  
 90          run_same_elevation(target.data(), params, pidBuffer);
 91  
 92          if (!pidFile.empty())
 93          {
 94              if (pidBuffer)
 95              {
 96                  FlushViewOfFile(pidBuffer, sizeof(DWORD));
 97                  UnmapViewOfFile(pidBuffer);
 98              }
 99  
100              if (hMapFile)
101              {
102                  FlushFileBuffers(hMapFile);
103                  CloseHandle(hMapFile);
104              }
105          }
106      }
107  
108      return 0;
109  }