Skip to content

Commit 9f6dfd6

Browse files
unicode and non-unicode
1 parent f47f3eb commit 9f6dfd6

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

resource_detectors/process_detector_utils.cc

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,20 @@ std::vector<std::string> ExtractCommandWithArgs(const std::string &command_line_
8989
std::vector<std::string> GetCommandWithArgs(const int32_t &pid)
9090
{
9191
#ifdef _MSC_VER
92-
int argc = 0;
93-
LPWSTR *argvW = CommandLineToArgvW(GetCommandLineW(), &argc);
92+
int argc = 0;
93+
LPTSTR cmdLine = GetCommandLine();
94+
95+
# ifdef UNICODE
96+
// for UNICODE
97+
LPWSTR *argvW = CommandLineToArgvW(cmdLine, &argc);
9498
if (!argvW)
9599
{
96-
return {}; // returns an empty vector if CommandLineToArgvW fails
100+
return {};
97101
}
98102

99103
std::vector<std::string> args;
100104
for (int i = 0; i < argc; i++)
101105
{
102-
// Convert UTF-16 to UTF-8
103106
int size_needed = WideCharToMultiByte(CP_UTF8, 0, argvW[i], -1, NULL, 0, NULL, NULL);
104107
if (size_needed > 0)
105108
{
@@ -111,7 +114,23 @@ std::vector<std::string> GetCommandWithArgs(const int32_t &pid)
111114

112115
LocalFree(argvW);
113116
return args;
114-
#else
117+
# else
118+
// for Non-UNICODE
119+
LPSTR *argvA = CommandLineToArgvA(cmdLine, &argc);
120+
if (!argvA)
121+
{
122+
return {};
123+
}
124+
125+
std::vector<std::string> args;
126+
for (int i = 0; i < argc; i++)
127+
{
128+
args.push_back(argvA[i]);
129+
}
130+
131+
LocalFree(argvA);
132+
return args;
133+
# endif // endif for UNICODE
115134
std::string command_line_path = FormFilePath(pid, kCmdlineName);
116135
return ExtractCommandWithArgs(command_line_path);
117136
#endif

0 commit comments

Comments
 (0)