main.cpp
 1  #include "pch.h"
 2  #include "CLILogic.h"
 3  #include "FileLocksmithLib/FileLocksmith.h"
 4  #include <iostream>
 5  #include "resource.h"
 6  #include <common/logger/logger.h>
 7  #include <common/utils/logger_helper.h>
 8  
 9  struct RealProcessFinder : IProcessFinder
10  {
11      std::vector<ProcessResult> find(const std::vector<std::wstring>& paths) override
12      {
13          return find_processes_recursive(paths);
14      }
15  };
16  
17  struct RealProcessTerminator : IProcessTerminator
18  {
19      bool terminate(DWORD pid) override
20      {
21          HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
22          if (hProcess)
23          {
24              bool result = TerminateProcess(hProcess, 0);
25              CloseHandle(hProcess);
26              return result;
27          }
28          return false;
29      }
30  };
31  
32  struct RealStringProvider : IStringProvider
33  {
34      std::wstring GetString(UINT id) override
35      {
36          wchar_t buffer[4096];
37          int len = LoadStringW(GetModuleHandle(NULL), id, buffer, ARRAYSIZE(buffer));
38          if (len > 0)
39          {
40              return std::wstring(buffer, len);
41          }
42          return L"";
43      }
44  };
45  
46  #ifndef UNIT_TEST
47  int wmain(int argc, wchar_t* argv[])
48  {
49      winrt::init_apartment();
50      LoggerHelpers::init_logger(L"FileLocksmithCLI", L"", LogSettings::fileLocksmithLoggerName);
51      Logger::info("FileLocksmithCLI started");
52  
53      RealProcessFinder finder;
54      RealProcessTerminator terminator;
55      RealStringProvider strings;
56  
57      auto result = run_command(argc, argv, finder, terminator, strings);
58  
59      if (result.exit_code != 0)
60      {
61          Logger::error("Command failed with exit code {}", result.exit_code);
62      }
63      else
64      {
65          Logger::info("Command succeeded");
66      }
67  
68      std::wcout << result.output;
69      return result.exit_code;
70  }
71  #endif