|
5 | 5 | #include <stdint.h> |
6 | 6 | #include <fstream> |
7 | 7 | #include <string> |
| 8 | +#include <vector> |
8 | 9 |
|
9 | 10 | #ifdef _MSC_VER |
10 | 11 | // clang-format off |
11 | 12 | # include <process.h> |
12 | 13 | # include <windows.h> |
13 | 14 | # include <psapi.h> |
14 | 15 | # include <shellapi.h> |
| 16 | +# pragma comment(lib, "shell32.lib") |
15 | 17 | # define getpid _getpid |
16 | 18 | // clang-format on |
17 | 19 | #else |
@@ -125,28 +127,24 @@ TEST(ProcessDetectorUtilsTest, CommandTest) |
125 | 127 | int32_t pid = getpid(); |
126 | 128 | std::string command; |
127 | 129 | #ifdef _MSC_VER |
128 | | - // On Windows, GetCommandLineW only works for the CURRENT process, |
129 | | - // so we ignore `pid` and just return the current process's command line. |
130 | | - LPCWSTR wcmd = GetCommandLineW(); |
131 | | - if (!wcmd) |
| 130 | + int argc = 0; |
| 131 | + LPWSTR *argvW = CommandLineToArgvW(GetCommandLineW(), &argc); |
| 132 | + |
| 133 | + if (argvW && argc > 0) |
132 | 134 | { |
133 | | - command = std::string(); |
| 135 | + int size_needed = WideCharToMultiByte(CP_UTF8, 0, argvW[0], -1, NULL, 0, NULL, NULL); |
| 136 | + if (size_needed > 0) |
| 137 | + { |
| 138 | + std::string arg(size_needed - 1, 0); |
| 139 | + WideCharToMultiByte(CP_UTF8, 0, argvW[0], -1, &arg[0], size_needed, NULL, NULL); |
| 140 | + command = arg; |
| 141 | + } |
| 142 | + |
| 143 | + LocalFree(argvW); |
134 | 144 | } |
135 | 145 | else |
136 | 146 | { |
137 | | - |
138 | | - // Convert UTF-16 to UTF-8 |
139 | | - int size_needed = WideCharToMultiByte(CP_UTF8, 0, wcmd, -1, NULL, 0, NULL, NULL); |
140 | | - if (size_needed <= 0) |
141 | | - { |
142 | | - command = std::string(); |
143 | | - } |
144 | | - else |
145 | | - { |
146 | | - std::string utf8_command(size_needed - 1, 0); // exclude null terminator |
147 | | - WideCharToMultiByte(CP_UTF8, 0, wcmd, -1, &utf8_command[0], size_needed, NULL, NULL); |
148 | | - command = utf8_command; |
149 | | - } |
| 147 | + command = std::string(); |
150 | 148 | } |
151 | 149 | #else |
152 | 150 | std::string command_line_path = |
|
0 commit comments