|
32 | 32 | #if __has_include("windows.h") |
33 | 33 | #define WIN32_LEAN_AND_MEAN |
34 | 34 | #include <windows.h> |
| 35 | +#include <shellapi.h> |
35 | 36 | #define HAVE_WINDOWS_H |
36 | 37 | #endif |
37 | 38 |
|
38 | 39 | namespace lnav::console { |
39 | | - |
40 | | -bool only_process_attached_to_win32_console() { |
| 40 | +bool |
| 41 | +only_process_attached_to_win32_console() |
| 42 | +{ |
41 | 43 | #if defined(HAVE_WINDOWS_H) |
42 | 44 | DWORD procIDs[2]; |
43 | | - DWORD count = GetConsoleProcessList((LPDWORD)procIDs, 2); |
| 45 | + DWORD count = GetConsoleProcessList((LPDWORD) procIDs, 2); |
44 | 46 | return count == 1; |
45 | 47 | #else |
46 | 48 | return false; |
47 | 49 | #endif |
48 | 50 | } |
49 | 51 |
|
| 52 | +void |
| 53 | +get_command_line_args(int* argc, char*** argv) |
| 54 | +{ |
| 55 | +#if defined(HAVE_WINDOWS_H) |
| 56 | + // Get the command line arguments as wchar_t strings |
| 57 | + wchar_t** wargv = CommandLineToArgvW(GetCommandLineW(), argc); |
| 58 | + if (!wargv) { |
| 59 | + *argc = 0; |
| 60 | + *argv = NULL; |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + // Count the number of bytes necessary to store the UTF-8 versions of those strings |
| 65 | + int n = 0; |
| 66 | + for (int i = 0; i < *argc; i++) |
| 67 | + n += WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, NULL, 0, NULL, NULL) |
| 68 | + + 1; |
| 69 | + |
| 70 | + // Allocate the argv[] array + all the UTF-8 strings |
| 71 | + *argv = (char**) malloc((*argc + 1) * sizeof(char*) + n); |
| 72 | + if (!*argv) { |
| 73 | + *argc = 0; |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + // Convert all wargv[] --> argv[] |
| 78 | + char* arg = (char*) &((*argv)[*argc + 1]); |
| 79 | + for (int i = 0; i < *argc; i++) { |
| 80 | + (*argv)[i] = arg; |
| 81 | + arg += WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, arg, n, NULL, NULL) |
| 82 | + + 1; |
| 83 | + } |
| 84 | + (*argv)[*argc] = NULL; |
| 85 | +#endif |
| 86 | +} |
| 87 | + |
50 | 88 | } |
0 commit comments