Skip to content

Commit 07cd812

Browse files
command test fix
1 parent a915a3d commit 07cd812

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

resource_detectors/process_detector.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <string>
1717
#include <unordered_map>
1818
#include <utility>
19+
#include <vector>
1920

2021
#ifdef _MSC_VER
2122
# include <process.h>

resource_detectors/test/process_detector_test.cc

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
#include <stdint.h>
66
#include <fstream>
77
#include <string>
8+
#include <vector>
89

910
#ifdef _MSC_VER
1011
// clang-format off
1112
# include <process.h>
1213
# include <windows.h>
1314
# include <psapi.h>
1415
# include <shellapi.h>
16+
# pragma comment(lib, "shell32.lib")
1517
# define getpid _getpid
1618
// clang-format on
1719
#else
@@ -125,28 +127,24 @@ TEST(ProcessDetectorUtilsTest, CommandTest)
125127
int32_t pid = getpid();
126128
std::string command;
127129
#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)
132134
{
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);
134144
}
135145
else
136146
{
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();
150148
}
151149
#else
152150
std::string command_line_path =

0 commit comments

Comments
 (0)