@@ -25,38 +25,31 @@ pub fn is_winserver() -> bool {
2525 !unsafe { VerifyVersionInfoA ( & mut osvi, VER_PRODUCT_TYPE , cond_mask) . is_ok ( ) }
2626}
2727
28- #[ allow( dead_code) ]
29- fn alloc_console_if_needed ( ) {
30- unsafe {
31- if AttachConsole ( ATTACH_PARENT_PROCESS ) . is_err ( ) {
32- AllocConsole ( ) . expect ( "[Error]: Failed to allocate console" ) ;
33- }
34- }
35- }
36-
37- #[ allow( dead_code) ]
38- fn free_console_if_needed ( ) {
39- unsafe {
40- FreeConsole ( ) . ok ( ) ;
41- }
42- }
43-
4428pub fn run ( ) {
45- let args = args:: Args :: parse ( ) ;
46- #[ cfg( not( debug_assertions) ) ]
47- if !args. auto {
48- alloc_console_if_needed ( ) ;
49- }
50- let mut ctx = Ctx :: default_with_name ( & args. name ) ;
51- if args. disable {
52- ctx. state = 0 ; // OFF
53- }
29+ // 首先设置一下环境
5430 let current_path = std:: env:: current_exe ( ) . unwrap ( ) ;
5531 let current_dir = current_path. parent ( ) . unwrap ( ) ;
5632 unsafe { std:: env:: set_var ( "DEFENDER_RS_PATH" , current_dir. to_str ( ) . unwrap ( ) ) } ;
57- let ctx_path = current_dir. join ( "ctx.bin" ) ;
58- ctx. serialize ( & ctx_path) ;
59- println ! ( "[Info]: Write context" ) ;
33+ // 解析命令行参数
34+ let args = args:: Args :: parse ( ) ;
35+
36+ if !args. auto {
37+ // 只在非 auto 模式下分配控制台
38+ unsafe {
39+ if AttachConsole ( ATTACH_PARENT_PROCESS ) . is_err ( ) {
40+ AllocConsole ( ) . expect ( "[Error]: Failed to allocate console" ) ;
41+ }
42+ }
43+
44+ // 只在非 auto 模式下写入配置
45+ let mut ctx = Ctx :: default_with_name ( & args. name ) ;
46+ if args. disable {
47+ ctx. state = 0 ; // OFF
48+ }
49+ let ctx_path = current_dir. join ( "ctx.bin" ) ;
50+ ctx. serialize ( & ctx_path) ;
51+ println ! ( "[Info]: Write context" ) ;
52+ }
6053
6154 // 环境准备, 确保 wscsvc 服务已启动
6255 if let Err ( e) = wsc:: ensure_wsc ( ) {
@@ -121,8 +114,9 @@ pub fn run() {
121114 CoUninitialize ( ) ;
122115 }
123116
124- #[ cfg( not( debug_assertions) ) ]
125117 if !args. auto {
126- free_console_if_needed ( ) ;
118+ unsafe {
119+ FreeConsole ( ) . ok ( ) ;
120+ }
127121 }
128122}
0 commit comments