{"record":{"id":"22c2e8de590d3871","repo":"OdysseusYuan/LKY_OfficeTools","slug":"serv-name-22c2e8","errorCode":null,"errorMessage":"创建服务 {serv_name} 时，返回值为空！","messagePattern":"创建服务 (.+?) 时，返回值为空！","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"LKY_OfficeTools/Common/Com_ServiceOS.cs","lineNumber":305,"sourceCode":"            internal static bool Create(string serv_name, string serv_runpath, string serv_displayname, string serv_description = null)\n            {\n                try\n                {\n                    if (Query.IsCreated(serv_name))\n                    {\n                        //已经创建服务，直接返回真\n                        return true;\n                    }\n                    else\n                    {\n                        //未安装，开始安装\n                        string cmd_install = $\"sc create \\\"{serv_name}\\\" binPath=\\\"{serv_runpath}\\\" start=auto DisplayName=\\\"{serv_displayname}\\\"\";\n                        var install_result = Com_ExeOS.Run.Cmd(cmd_install);\n\n                        //非空判断，若返回值为空，则为假\n                        if (string.IsNullOrEmpty(install_result))\n                        {\n                            throw new Exception($\"创建服务 {serv_name} 时，返回值为空！\");\n                        }\n\n                        //判断是否安装成功\n                        if (install_result.Contains(\"成功\") || install_result.ToLower().Contains(\"success\"))\n                        {\n                            //描述信息不为空，配置描述信息\n                            if (!string.IsNullOrEmpty(serv_description))\n                            {\n                                Modify.Description(serv_name, serv_description);\n                            }\n\n                            //无论修改描述信息是否成功，均返回成功\n                            return true;\n                        }\n                        else\n                        {\n                            return false;\n                        }","sourceCodeStart":287,"sourceCodeEnd":323,"githubUrl":"https://github.com/OdysseusYuan/LKY_OfficeTools/blob/6f9a1bd471918f3310e6be4a6d7f42d0d6e467cf/LKY_OfficeTools/Common/Com_ServiceOS.cs#L287-L323","documentation":"Thrown in Com_ServiceOS.Config.Create when the stdout captured from `sc create ...` via Com_ExeOS.Run.Cmd is null or empty (Com_ServiceOS.cs:303-305). Run.Cmd only returns null inside its own catch (cmd.exe launch or stdio-redirect failure), so an empty result indicates the command never produced output rather than an sc-level failure (access-denied still yields non-empty text). Create catches the throw, logs it, and returns false (Com_ServiceOS.cs:326-330).","triggerScenarios":"Run.Cmd throws because cmd.exe cannot be started, standard-output redirection fails, or the process is killed before emitting output. Extremely unlikely from a plain sc access-denied, which still prints an error string.","commonSituations":"cmd.exe missing/off-path on a stripped image; an aggressive security product blocking child cmd.exe; stdio handle exhaustion; antivirus interfering with redirected console IO.","solutions":["Run as administrator (Create requires privileges anyway for sc create).","Verify cmd.exe is reachable on PATH and not blocked by policy.","Reproduce by running the exact `sc create ...` string from a real console.","Inspect the application log (Lib_AppLog) for the underlying exception written by Run.Cmd's catch."],"exampleFix":"// before\nbool ok = Com_ServiceOS.Config.Create(name, path, display);\n\n// after\nif (string.IsNullOrEmpty(Com_ExeOS.Run.Cmd(\"ver\"))) { /* cmd unavailable; fix environment */ return false; }\nbool ok = Com_ServiceOS.Config.Create(name, path, display);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(Com_ExeOS.Run.Cmd(\"ver\"))) { /* cmd unavailable; fix env */ return false; }\nbool ok = Com_ServiceOS.Config.Create(name, path, display, desc);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Create swallows the throw and returns false; check the bool return and the log.","Run elevated; sc create requires administrator privileges.","Ensure cmd.exe is reachable on PATH and not blocked by policy.","Reproduce the exact `sc create ...` string in a console when debugging."],"tags":["windows-service","scm","cmd","service-install"],"backgroundTag":null,"analyzedSha":"6f9a1bd471918f3310e6be4a6d7f42d0d6e467cf","analyzedAt":"2026-08-13T18:00:34.810Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}