@@ -7415,39 +7415,50 @@ static size_t _webui_get_child_process_id(_webui_window_t* win) {
7415
7415
} else {
7416
7416
// Web Browser Mode
7417
7417
// Filter process by WebUI's web server URL in process CLI
7418
- char cmd [1024 ] = {0 };
7419
- FILE * fp ;
7420
7418
#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 ;
7429
7445
#else
7446
+ char cmd [1024 ] = {0 };
7430
7447
snprintf (cmd , sizeof (cmd ), "pgrep -f \"%s\"" , win -> url );
7448
+ FILE * fp ;
7431
7449
fp = popen (cmd , "r" );
7432
- #endif
7433
7450
if (!fp ) return 0 ;
7434
7451
char line [4096 ] = {0 };
7435
7452
while (fgets (line , sizeof (line ), fp )) {
7436
7453
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
7443
7454
pid = atoi (line );
7444
- #endif
7445
7455
if (pid > 0 ) {
7446
7456
pclose (fp );
7447
7457
return (size_t )pid ;
7448
7458
}
7449
7459
}
7450
7460
pclose (fp );
7461
+ #endif
7451
7462
}
7452
7463
}
7453
7464
0 commit comments