{"id":1104,"date":"2010-05-24T11:25:00","date_gmt":"2010-05-24T03:25:00","guid":{"rendered":""},"modified":"2013-11-17T16:45:58","modified_gmt":"2013-11-17T08:45:58","slug":"delphi%e8%8e%b7%e5%8f%96%e6%89%a7%e8%a1%8ccmd%e5%90%8e%e8%bf%94%e5%9b%9e%e7%bb%93%e6%9e%9c","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/1104.html","title":{"rendered":"Delphi\u83b7\u53d6\u6267\u884cCMD\u540e\u8fd4\u56de\u7ed3\u679c"},"content":{"rendered":"<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\nfunction RunDOS(const CommandLine: String): String;\r\nprocedure CheckResult(b: Boolean);\r\nbegin\r\n  if not b then\r\n    Raise Exception.Create(SysErrorMessage(GetLastError));\r\nend;\r\nvar\r\n  HRead, HWrite: THandle;\r\n  StartInfo: TStartupInfo;\r\n  ProceInfo: TProcessInformation;\r\n  b: Boolean;\r\n  sa: TSecurityAttributes;\r\n  inS: THandleStream;\r\n  sRet: TStrings;\r\nbegin\r\n  Result := '';\r\n  FillChar(sa, sizeof(sa), 0);\r\n  \/\/ \u8bbe\u7f6e\u5141\u8bb8\u7ee7\u627f\uff0c\u5426\u5219\u5728NT\u548c2000\u4e0b\u65e0\u6cd5\u53d6\u5f97\u8f93\u51fa\u7ed3\u679c\r\n  sa.nLength := sizeof(sa);\r\n  sa.bInheritHandle := True;\r\n  sa.lpSecurityDescriptor := nil;\r\n  b := CreatePipe(HRead, HWrite, @sa, 0);\r\n  CheckResult(b);\r\n\r\n  FillChar(StartInfo, sizeof(StartInfo), 0);\r\n  StartInfo.cb := sizeof(StartInfo);\r\n  StartInfo.wShowWindow := SW_HIDE;\r\n  \/\/ \u4f7f\u7528\u6307\u5b9a\u7684\u53e5\u67c4\u4f5c\u4e3a\u6807\u51c6\u8f93\u5165\u8f93\u51fa\u7684\u6587\u4ef6\u53e5\u67c4,\u4f7f\u7528\u6307\u5b9a\u7684\u663e\u793a\u65b9\u5f0f\r\n  StartInfo.dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;\r\n  StartInfo.hStdError := HWrite;\r\n  StartInfo.hStdInput := GetStdHandle(STD_INPUT_HANDLE); \/\/ HRead;\r\n  StartInfo.hStdOutput := HWrite;\r\n\r\n  b := CreateProcess(nil, \/\/ lpApplicationName: PChar\r\n    PChar(CommandLine), \/\/ lpCommandLine: PChar\r\n    nil, \/\/ lpProcessAttributes: PSecurityAttributes\r\n    nil, \/\/ lpThreadAttributes: PSecurityAttributes\r\n    True, \/\/ bInheritHandles: BOOL\r\n    CREATE_NEW_CONSOLE, nil, nil, StartInfo, ProceInfo);\r\n\r\n  CheckResult(b);\r\n  WaitForSingleObject(ProceInfo.hProcess, INFINITE);\r\n\r\n  inS := THandleStream.Create(HRead);\r\n  if inS.Size &gt; 0 then\r\n  begin\r\n    sRet := TStringList.Create;\r\n    sRet.LoadFromStream(inS);\r\n    Result := sRet.Text;\r\n    sRet.Free;\r\n  end;\r\n  inS.Free;\r\n\r\n  CloseHandle(HRead);\r\n  CloseHandle(HWrite);\r\nend;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>function RunDOS(const CommandLine: String): String; pro [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-1104","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/1104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=1104"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/1104\/revisions"}],"predecessor-version":[{"id":4700,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/1104\/revisions\/4700"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=1104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=1104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=1104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}