Skip to content

Commit ee83144

Browse files
committed
Win32 - Fix issue where terminal window appears
1 parent 42a9af3 commit ee83144

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

src/webui.c

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7415,39 +7415,50 @@ static size_t _webui_get_child_process_id(_webui_window_t* win) {
74157415
} else {
74167416
// Web Browser Mode
74177417
// Filter process by WebUI's web server URL in process CLI
7418-
char cmd[1024] = {0};
7419-
FILE* fp;
74207418
#if defined(_WIN32)
7421-
#define popen _popen
7422-
#define pclose _pclose
7423-
snprintf(
7424-
cmd, sizeof(cmd),
7425-
"wmic process get ProcessId,CommandLine /FORMAT:CSV | findstr /c:\"%s\"",
7426-
win->url
7427-
);
7428-
fp = popen(cmd, "r");
7419+
char* out = NULL;
7420+
char cmd[1024] = {0};
7421+
WEBUI_SN_PRINTF_STATIC(cmd, sizeof(cmd),
7422+
"cmd.exe /c wmic process where \"CommandLine like '%c%s%c'\" get ProcessId,CommandLine /format:csv 2>&1",
7423+
'%', win->url, '%');
7424+
_webui_system_win32_out(cmd, &out, false);
7425+
size_t process_id = 0;
7426+
if (out) {
7427+
char* line_start = out;
7428+
char* found_url_ptr = NULL;
7429+
while ((line_start = strstr(line_start, "\n")) != NULL) {
7430+
line_start++; // Move past the newline
7431+
found_url_ptr = strstr(line_start, win->url);
7432+
if (found_url_ptr) {
7433+
char* comma_ptr = strchr(found_url_ptr, ',');
7434+
if (comma_ptr) {
7435+
comma_ptr++;
7436+
while (*comma_ptr == ' ' || *comma_ptr == '\t') comma_ptr++;
7437+
process_id = (size_t)atoi(comma_ptr);
7438+
}
7439+
break;
7440+
}
7441+
}
7442+
free(out);
7443+
}
7444+
return process_id;
74297445
#else
7446+
char cmd[1024] = {0};
74307447
snprintf(cmd, sizeof(cmd), "pgrep -f \"%s\"", win->url);
7448+
FILE* fp;
74317449
fp = popen(cmd, "r");
7432-
#endif
74337450
if (!fp) return 0;
74347451
char line[4096] = {0};
74357452
while (fgets(line, sizeof(line), fp)) {
74367453
int pid = 0;
7437-
#if defined(_WIN32)
7438-
char* last_comma = strrchr(line, ',');
7439-
if (last_comma) {
7440-
pid = atoi(last_comma + 1);
7441-
}
7442-
#else
74437454
pid = atoi(line);
7444-
#endif
74457455
if (pid > 0) {
74467456
pclose(fp);
74477457
return (size_t)pid;
74487458
}
74497459
}
74507460
pclose(fp);
7461+
#endif
74517462
}
74527463
}
74537464

0 commit comments

Comments
 (0)