{"id":470,"date":"2009-06-19T09:33:00","date_gmt":"2009-06-19T01:33:00","guid":{"rendered":""},"modified":"2013-11-17T17:26:05","modified_gmt":"2013-11-17T09:26:05","slug":"%e8%b0%83%e7%94%a8cmd%e5%91%bd%e4%bb%a4%e8%a1%8c%e5%b9%b6%e5%8f%96%e5%be%97%e8%bf%94%e5%9b%9e%e7%bb%93%e6%9e%9c","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/470.html","title":{"rendered":"\u8c03\u7528CMD\u547d\u4ee4\u884c\u5e76\u53d6\u5f97\u8fd4\u56de\u7ed3\u679c"},"content":{"rendered":"<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\nprocedure CheckResult(b: Boolean);\r\nbegin\r\n   if not b then\r\n   Raise Exception.Create(SysErrorMessage(GetLastError));\r\nend;\r\n\r\nfunction RunDOS(const CommandLine: String): String;\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,\r\n   nil,\r\n   nil,\r\n   StartInfo,\r\n   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>procedure CheckResult(b: Boolean); begin if not b then  [&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-470","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/470","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=470"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/470\/revisions"}],"predecessor-version":[{"id":4743,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/470\/revisions\/4743"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}