{"id":625,"date":"2018-01-03T23:00:20","date_gmt":"2018-01-03T15:00:20","guid":{"rendered":"http:\/\/blog.sorayuki.net\/?p=625"},"modified":"2018-01-03T23:00:20","modified_gmt":"2018-01-03T15:00:20","slug":"windows%e4%b8%8b%e6%8e%a7%e5%88%b6%e5%ad%90%e8%bf%9b%e7%a8%8b%e7%9a%84%e5%8f%a5%e6%9f%84%e7%bb%a7%e6%89%bf","status":"publish","type":"post","link":"https:\/\/blog.sorayuki.net\/?p=625","title":{"rendered":"Windows\u4e0b\u63a7\u5236\u5b50\u8fdb\u7a0b\u7684\u53e5\u67c4\u7ee7\u627f"},"content":{"rendered":"<p>\u76f8\u4fe1\u7528 Windows API \u5199\u8fc7\u91cd\u5b9a\u5411\u5b50\u8fdb\u7a0b\u7684\u6807\u51c6\u8f93\u5165\u8f93\u51fa\u7684\u4eba\u90fd\u5f04\u8fc7\u8fd9\u4e1c\u897f\u3002\u5927\u6982\u8fc7\u7a0b\u662f\u4e0d\u96be\uff0c\u4e0d\u8fc7\u5bf9\u521d\u5b66\u8005\u5751\u5f88\u591a\u3002\u51e0\u5e74\u524d\u6211\u5199\u4e86\u4e2a\u6587\u5927\u6982\u8bf4\u8fd9\u4e2a\u7684 <a title=\"https:\/\/blog.sorayuki.net\/?p=85\" href=\"https:\/\/blog.sorayuki.net\/?p=85\">https:\/\/blog.sorayuki.net\/?p=85<\/a> \uff0c\u73b0\u5728\u8981\u8bf4\u7684\u4e5f\u662f\u8fd9\u4e2a\u8bdd\u9898\uff0c\u4e0d\u8fc7\u662f\u53e6\u5916\u7684\u4e8b\u3002<\/p>\n<p>\u4e4b\u524d\u7684\u7ecf\u9a8c\u662f\u8fd9\u6837\u7684\uff0c\u6bd4\u5982\u6211\u6709\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u8fd9\u4e2a\u8fdb\u7a0b\u8981\u5f00\u4e2a\u5b50\u8fdb\u7a0b\u3001\u5e76\u4e14\u53d1\u6570\u636e\u7ed9\u5b50\u8fdb\u7a0b\u3002\u90a3\u4e48\u505a\u6cd5\u662f\u521b\u5efa\u4e00\u5bf9\u533f\u540d\u7ba1\u9053\uff0c\u628a\u7ba1\u9053\u8bfb\u7684\u90a3\u4e00\u7aef\u62ff\u53bb\u7ed9\u5b50\u8fdb\u7a0b\uff0c\u5199\u7684\u8fd9\u4e00\u7aef\u81ea\u5df1\u7559\u7740\u3002\u5bb9\u6613\u51fa\u9519\u7684\u5730\u65b9\u5728\u8fd9\u4e2a\u201c\u81ea\u5df1\u62ff\u7740\u201d\u548c\u201c\u62ff\u53bb\u7ed9\u5b50\u8fdb\u7a0b\u201d\u3002\u62ff\u53bb\u7ed9\u5b50\u8fdb\u7a0b\u53ef\u4ee5\u7406\u89e3\u4e3a\uff0c\u8fd9\u4e2a\u53e5\u67c4\u8981\u8bbe\u7f6e\u6210\u53ef\u7ee7\u627f\u7684\uff0c\u5b50\u8fdb\u7a0b\u5f00\u542f\u4ee5\u540e\u5c31\u5e26\u7740\u8fd9\u4e2a\u53e5\u67c4\u4e86\uff0c\u90a3\u4e48\u81ea\u5df1\u8fdb\u7a0b\u62ff\u7740\u7684\u5c31\u53ef\u4ee5\u5173\u6389\uff08CloseHandle\uff09\u4e86\uff1b\u81ea\u5df1\u62ff\u7740\uff0c\u90a3\u4e48\u5b57\u9762\u610f\u601d\u5c31\u662f\u4e0d\u7ee7\u627f\u7ed9\u5b50\u8fdb\u7a0b\u4e86\u3002API\u51fd\u6570CreatePipe\u6709\u4e2a SECURITY_ATTRIBUTES \u53c2\u6570\uff0c\u8fd9\u4e2a\u7ed3\u6784\u4f53\u91cc\u662f\u53ef\u4ee5\u8bbe\u7f6e\u521b\u5efa\u7684\u7ba1\u9053\u53e5\u67c4\u80fd\u4e0d\u80fd\u7ee7\u627f\u7684\uff0c\u8981\u4e48\u4e24\u4e2a\u90fd\u80fd\u7ee7\u627f\u8981\u4e48\u4e24\u4e2a\u90fd\u4e0d\u80fd\u7ee7\u627f\u3002\u4f46\u662f\u73b0\u5728\u8981\u7684\u662f\u5199\u7684\u90a3\u4e2a\u4e0d\u7ee7\u627f\u3001\u8bfb\u7684\u90a3\u4e2a\u7ee7\u627f\u3002\u505a\u6cd5\u4e0d\u6b62\u4e00\u79cd\uff0c\u4e0d\u8fc7\u601d\u8def\u662f\u8fd9\u4e48\u4e24\u4e2a\uff0c\u4e00\u4e2a\u662f\u521b\u5efa\u7684\u65f6\u5019\u4e24\u4e2a\u90fd\u4e0d\u80fd\u7ee7\u627f\uff0c\u7136\u540e\u628a\u5176\u4e2d\u4e00\u4e2a\u6539\u6210\u53ef\u4ee5\uff1b\u53e6\u4e00\u4e2a\u662f\u521b\u5efa\u7684\u65f6\u5019\u4e24\u4e2a\u90fd\u80fd\u7ee7\u627f\uff0c\u7136\u540e\u628a\u5176\u4e2d\u4e00\u4e2a\u6539\u6210\u4e0d\u884c\u3002\u6539\u7ee7\u627f\u5c5e\u6027\u53ef\u4ee5\u7528 SetHandleInformation \u4e5f\u53ef\u4ee5\u7528 DuplicateHandle \u590d\u5236\u4e00\u4e2a\u80fd\/\u4e0d\u80fd\u7ee7\u627f\u7684\u65b0\u53e5\u67c4\u51fa\u6765\u3001\u968f\u540e\u628a\u539f\u6765\u7684\u5173\u6389\u3002\u6211\u73b0\u5728\u662f\u7528 SetHandleInformation \u7684\u65b9\u6cd5\uff0cDuplicateHandle \u7684\u65b9\u6cd5\u5df2\u7ecf\u5fd8\u8bb0\u662f\u5728\u54ea\u91cc\u770b\u7684\u4e86\uff0c\u73b0\u5728\u4e5f\u4e0d\u77e5\u9053\u600e\u4e48\u6bd4\u8f83\u597d\u574f\u548c\u5fc5\u8981\u6027\u3002<\/p>\n<p>\u590d\u6742\u4e00\u70b9\u7684\u573a\u666f\uff0c\u662f\u6211\u8981\u8d77\u4e24\u4e2a\u8fdb\u7a0b\uff0c\u7b2c\u4e00\u4e2a\u8fdb\u7a0b\u7684\u8f93\u51fa\u7ed9\u7b2c\u4e8c\u4e2a\u8fdb\u7a0b\u7684\u8f93\u5165\u3002\u9ebb\u70e6\u4e00\u70b9\u7684\u529e\u6cd5\u53ef\u4ee5\u4e24\u4e2a\u90fd\u548c\u81ea\u5df1\u901a\u4fe1\u3001\u81ea\u5df1\u5728\u4e2d\u95f4\u505a\u8f6c\u53d1\uff1b\u7b80\u5355\u7684\u529e\u6cd5\u53ef\u4ee5\u76f4\u63a5\u521b\u5efa\u4e00\u5bf9\u7ba1\u9053\uff0c\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u5206\u4e00\u4e2a\u3001\u81ea\u5df1\u4e0d\u7559\u3002\u73b0\u5728\u8fd9\u91cc\u8981\u8bf4\u7684\u662f\u540e\u8005\uff0c\u590d\u6742\u6027\u4e00\u4e2a\u8868\u73b0\u5728\u7ba1\u9053\u662f\u81ea\u5df1\u521b\u5efa\u7684\uff0c\u4f46\u662f\u81ea\u5df1\u5728\u8d77\u4e86\u5b50\u8fdb\u7a0b\u4e4b\u540e\u4e00\u4e2a\u90fd\u4e0d\u7559\uff1b\u53e6\u4e00\u4e2a\u8868\u73b0\u5728\u8981\u521a\u597d\u4e00\u4e2a\u8fdb\u7a0b\u7ee7\u627f\u4e00\u4e2a\uff0c\u5982\u679c\u6709\u4e00\u4e2a\u8fdb\u7a0b\u7ee7\u627f\u4e86\u4e24\u4e2a\uff0c\u6bd4\u5982\u5199\u7684\u90a3\u4e00\u7aef\u4e00\u4e0d\u5c0f\u5fc3\u4e24\u4e2a\u8fdb\u7a0b\u90fd\u7ee7\u627f\u4e86\uff0c\u90a3\u4e48\u5f88\u4e0d\u5e78\u8fd9\u4e00\u5bf9\u8fdb\u7a0b\u6c38\u8fdc\u8dd1\u4e0d\u5b8c\u4e86\uff1a\u5199\u5165\u6570\u636e\u7684\u90a3\u4e2a\u5b50\u8fdb\u7a0b\u5199\u5b8c\u4e86\u5173\u6389\u4e86\u8fd9\u4e2a\u5199\u5165\u7aef\uff0c\u4f46\u662f\u7531\u4e8e\u8bfb\u53d6\u6570\u636e\u7684\u90a3\u4e2a\u5b50\u8fdb\u7a0b\u4e5f\u6301\u6709\u8fd9\u4e2a\u5199\u7aef\uff0c\u81ea\u5df1\u6709\u81ea\u5df1\u90fd\u4e0d\u77e5\u9053\uff0c\u4e5f\u5c31\u662f\u8bf4\u8fd9\u4e2a\u5199\u7aef\u6ca1\u6cd5\u5168\u90e8\u88ab\u5173\u95ed\uff0c\u90a3\u4e48\u8bfb\u7684\u90a3\u4e00\u7aef\u5c31\u6c38\u8fdc\u5728\u7b49\u5f85\u4e86\u3002<\/p>\n<p>\u867d\u7136\u4e0a\u9762\u90a3\u4e2a\u4f8b\u5b50\u53ea\u8981\u641e\u6e05\u695a\u5173\u7cfb\u3001\u5c0f\u5fc3\u64cd\u4f5c\u7684\u8bdd\uff0c\u4e5f\u6ca1\u6709\u90a3\u4e48\u5927\u95ee\u9898\u3002\u4e0d\u8fc7\u9ebb\u70e6\u7684\u4e8b\u60c5\u5728\uff0c\u5047\u8bbe\u6211\u73b0\u5728\u662f\u4e00\u4e2a\u6709\u597d\u51e0\u4e2a\u7ebf\u7a0b\u5728\u540c\u65f6\u6267\u884c\u7684\u7a0b\u5e8f\uff0c\u5728\u5176\u4e2d\u4e00\u4e2a\u7ebf\u7a0bA\u91cc\uff0c\u8981\u8c03\u7528 CreateProcess \u7684\u65f6\u5019\uff0c\u5b83\u4e0d\u77e5\u9053\u5176\u4ed6\u7ebf\u7a0b\u6bd4\u5982B\u6709\u6ca1\u6709\u521b\u5efa\u4ec0\u4e48\u4f1a\u88ab\u7ee7\u627f\u7684\u53e5\u67c4\uff0c\u5047\u8bbe\u8fd9\u4e2a\u65f6\u5019\u7ebf\u7a0bB\u4e5f\u5728\u8c03\u7528 CreateProcess\uff0c\u90a3\u4e48\u7ebf\u7a0bB\u521b\u5efa\u7684\u8fd9\u4e2a\u5b50\u8fdb\u7a0b\u4f1a\u628a\u7ebf\u7a0bA\u521b\u5efa\u7684\u90a3\u4e9b\u53e5\u67c4\u4e5f\u4e00\u8d77\u7ee7\u627f\u8d70\u3002\u7136\u540e\u95ee\u9898\u5462\u5c31\u4f1a\u51fa\u5728\u521a\u624d\u8bf4\u7684\u90a3\u4e2a\u573a\u666f\u4e0a\uff0c\u6bd4\u5982\u4e00\u5bf9\u7ba1\u9053\uff0c\u8bfb\u53d6\u7aef\u6c38\u8fdc\u963b\u585e\u56e0\u4e3a\u5199\u5165\u7aef\u6ca1\u6709\u5168\u90e8\u88ab\u5173\u95ed\u3002\u4e5f\u8bb8\u4f1a\u89c9\u5f97\u8fd9\u6837\u7684\u573a\u666f\u53ea\u8981\u5728\u8981\u521b\u5efa\u5b50\u8fdb\u7a0b\u7684\u65f6\u5019\u9501\u4e2a\u6574\u4e2a\u8fdb\u7a0b\u8303\u56f4\u7684\u9501\u5c31\u597d\u4e86\uff0c\u4f46\u5982\u679c\u8fd9\u91cc\u9762\u8c03\u7528\u4e86\u4ec0\u4e48\u7b2c\u4e09\u65b9\u5e93\uff0c\u6216\u8005\u662f\u5de5\u7a0b\u4e0a\u597d\u591a\u4eba\u540c\u65f6\u5f00\u53d1\u4e00\u4e2a\u9879\u76ee\uff0c\u6709\u4e9b\u4e8b\u60c5\u5c31\u4e0d\u4e00\u5b9a\u6709\u90a3\u4e48\u53ef\u63a7\u4e86\u3002\u800c\u4e14\u8fd9\u79cdBug\u8fd8\u53ef\u80fd\u5728\u6781\u5076\u7136\u7684\u60c5\u51b5\u4e0b\u624d\u4f1a\u9047\u5230\u90a3\u4e48\u4e00\u4e24\u6b21\uff0c\u8c03\u8bd5\u7684\u65f6\u5019\u96be\u4ee5\u91cd\u73b0\u5341\u5206\u75db\u82e6\u2026\u2026\u554a\u522b\u8bf4\u4e86\uff0c\u5bb3\u6015\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u90a3\u4e48\u5728 CreateProcess \u7684\u65f6\u5019\u6709\u6ca1\u6709\u4ec0\u4e48\u66f4\u53ef\u9760\u7684\u65b9\u6cd5\uff0c\u6bd4\u5982\u4e0d\u5149\u662f\u7ed9\u6bcf\u4e2a\u53e5\u67c4\u8bbe\u7f6e\u4ec0\u4e48\u6807\u5fd7\u80fd\u4e0d\u80fd\u7ee7\u627f\uff0c\u8fd8\u80fd\u5728\u8c03\u7528 CreateProcess \u7684\u65f6\u5019\u901a\u8fc7\u4ec0\u4e48\u53c2\u6570\u6765\u663e\u5f0f\u6307\u5b9a\u6211\u8981\u8fd9\u4e2a\u3001\u8fd9\u4e2a\u3001\u8fd9\u4e2a\u548c\u90a3\u4e2a\u53e5\u67c4\uff1f\u5176\u5b9e\u662f\u6709\u7684\uff0c\u6211\u4ee5\u524d\u6ca1\u7528\u8fc7\uff0c\u6700\u8fd1\u770b\u4e86\u4e00\u4e2a\u535a\u5ba2\u4e4b\u540e\u624d\u77e5\u9053\u6709\u8fd9\u6837\u7684\u73a9\u6cd5\u3002\u535a\u5ba2\u5728\u8fd9\u91cc\uff1a<\/p>\n<p><a title=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20111216-00\/?p=8873\/\" href=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20111216-00\/?p=8873\/\">https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20111216-00\/?p=8873\/<\/a><\/p>\n<p>\u5176\u4e2d\u5f88\u5173\u952e\u7684\u4e00\u4e2a\u793a\u4f8b\u51fd\u6570\u7684\u4ee3\u7801\u662f<\/p>\n<pre>BOOL CreateProcessWithExplicitHandles(\n  __in_opt     LPCTSTR lpApplicationName,\n  __inout_opt  LPTSTR lpCommandLine,\n  __in_opt     LPSECURITY_ATTRIBUTES lpProcessAttributes,\n  __in_opt     LPSECURITY_ATTRIBUTES lpThreadAttributes,\n  __in         BOOL bInheritHandles,\n  __in         DWORD dwCreationFlags,\n  __in_opt     LPVOID lpEnvironment,\n  __in_opt     LPCTSTR lpCurrentDirectory,\n  __in         LPSTARTUPINFO lpStartupInfo,\n  __out        LPPROCESS_INFORMATION lpProcessInformation,\n    \/\/ here is the new stuff\n  __in         DWORD cHandlesToInherit,\n  __in_ecount(cHandlesToInherit) HANDLE *rgHandlesToInherit)\n{\n BOOL fSuccess;\n BOOL fInitialized = FALSE;\n SIZE_T size = 0;\n LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList = NULL;\n\n fSuccess = cHandlesToInherit &lt; 0xFFFFFFFF \/ sizeof(HANDLE) &amp;&amp;\n            lpStartupInfo-&gt;cb == sizeof(*lpStartupInfo);\n if (!fSuccess) {\n  SetLastError(ERROR_INVALID_PARAMETER);\n }\n if (fSuccess) {\n  fSuccess = InitializeProcThreadAttributeList(NULL, 1, 0, &amp;size) ||\n             GetLastError() == ERROR_INSUFFICIENT_BUFFER;\n }\n if (fSuccess) {\n  lpAttributeList = reinterpret_cast&lt;LPPROC_THREAD_ATTRIBUTE_LIST&gt;\n                                (HeapAlloc(GetProcessHeap(), 0, size));\n  fSuccess = lpAttributeList != NULL;\n }\n if (fSuccess) {\n  fSuccess = InitializeProcThreadAttributeList(lpAttributeList,\n                    1, 0, &amp;size);\n }\n if (fSuccess) {\n  fInitialized = TRUE;\n  fSuccess = UpdateProcThreadAttribute(lpAttributeList,\n                    0, PROC_THREAD_ATTRIBUTE_HANDLE_LIST,\n                    rgHandlesToInherit,\n                    cHandlesToInherit * sizeof(HANDLE), NULL, NULL);\n }\n if (fSuccess) {\n  STARTUPINFOEX info;\n  <a href=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2005\/06\/28\/433341.aspx\">ZeroMemory<\/a>(&amp;info, sizeof(info));\n  info.StartupInfo = *lpStartupInfo;\n  info.StartupInfo.cb = sizeof(info);\n  info.lpAttributeList = lpAttributeList;\n  fSuccess = CreateProcess(lpApplicationName,\n                           lpCommandLine,\n                           lpProcessAttributes,\n                           lpThreadAttributes,\n                           bInheritHandles,\n                           dwCreationFlags | EXTENDED_STARTUPINFO_PRESENT,\n                           lpEnvironment,\n                           lpCurrentDirectory,\n                           &amp;info.StartupInfo,\n                           lpProcessInformation);\n }\n \n if (fInitialized) DeleteProcThreadAttributeList(lpAttributeList);\n if (lpAttributeList) HeapFree(GetProcessHeap(), 0, lpAttributeList);\n return fSuccess;\n}<\/pre>\n<p><\/p>\n<p>\u6211\u81ea\u5df1\u5728\u7528\u8fd9\u4e2a\u65b9\u6cd5\u7684\u65f6\u5019\u5012\u662f\u6ca1\u6709\u76f4\u63a5\u6284\u4ee3\u7801\uff0c\u91cc\u9762\u6709\u4e9b\u5730\u65b9\u88ab\u6211\u6362\u6210\u66f4\u7b80\u5355\u7684\u4ee3\u7801\u3002\u4e0d\u8fc7\u6838\u5fc3\u601d\u60f3\u662f\uff0c\u7528 STARTUPINFOEX \u6765\u4ee3\u66ff STARTUPINFO\uff0c\u8fd9\u6837\u5728\u90a3\u4e2a\u5e26 EX \u540e\u7f00\u7684\u7ed3\u6784\u4f53\u91cc\u6709\u4e2a lpAttributeList \u53ef\u4ee5\u7528\uff0c\u91cc\u9762\u53ef\u4ee5\u585e\u4e00\u4e9b\u6bd4\u5982\u6211\u8981\u7ee7\u627f\u90a3\u4e9b\u53e5\u67c4\u8fd9\u6837\u7684\u4fe1\u606f\u3002\u90a3\u4e48\u5728\u4f60\u7ed9\u7684\u5217\u8868\u4e4b\u5916\u7684\u53e5\u67c4\uff0c\u5c31\u4e0d\u4f1a\u88ab\u5b50\u8fdb\u7a0b\u7ee7\u627f\u3002<\/p>\n<p>\u8fd9\u6837\u56de\u5230\u521a\u624d\u7684\u4f8b\u5b50\uff0c\u6211\u8981\u5f00\u4e24\u4e2a\u8fdb\u7a0b\u6bd4\u5982A\u548cB\uff0c\u628aA\u7684\u6807\u51c6\u8f93\u51fa\u8fde\u5230B\u7684\u6807\u51c6\u8f93\u5165\u4e0a\uff0c\u5c31\u53ef\u4ee5\u521b\u5efa\u4e00\u5bf9\u7ba1\u9053\uff0c\u53cd\u6b63\u90fd\u8bbe\u6210\u7ee7\u627f\u3001\u4e5f\u4e0d\u7528\u4e00\u4e2a\u7ee7\u627f\u4e00\u4e2a\u4e0d\u7ee7\u627f\u4e86\u3002\u7136\u540e\uff0c\u521b\u5efa\u5b50\u8fdb\u7a0b\u7684\u65f6\u5019\u4e00\u4eba\u585e\u4e00\u4e2a\uff0c\u7528\u8fd9\u79cd\u65b9\u5f0f\u6307\u5b9a\u53ea\u7ee7\u627f\u5176\u4e2d\u4e00\u4e2a\u3001\u4e0d\u7ee7\u627f\u53e6\u5916\u4e00\u4e2a\uff0c\u4e24\u4e2a\u8fdb\u7a0b\u90fd\u5f00\u597d\u4e86\u5c31\u628a\u81ea\u5df1\u624b\u4e0a\u7684\u5168\u5173\u4e86\u3002\u5269\u4e0b\u7684\u5c31\u662f\u7b49\u5f85\u5b50\u8fdb\u7a0b\u8fd0\u884c\u5b8c\u6210\u4e86\u3002<\/p>\n<p>\u5728\u5b9e\u9a8c\u8fc7\u7a0b\u4e2d\u6211\u8fd8\u53d1\u73b0\u6709\u8fd9\u6837\u4e00\u4e2a\u5751\uff08\uff1f\uff09\uff0cWindows\u7684\u90a3\u51e0\u4e2a\u6807\u51c6\u8f93\u5165\u8f93\u51fa\u6807\u51c6\u9519\u8bef\u7684\u53e5\u67c4\uff0c\u5c31\u662f\u7528 GetStdHandles \u83b7\u5f97\u7684\u90a3\u51e0\u4e2a\uff0c\u867d\u7136\u53ef\u4ee5\u7528 DuplicateHandle \u6765\u590d\u5236\uff0c\u4f46\u662f\u8fd9\u590d\u5236\u51fa\u6765\u7684\u5374\u4e0d\u80fd\u7ee7\u627f\u3002\u5982\u679c\u5c1d\u8bd5\u5728\u7ee7\u627f\u53e5\u67c4\u5217\u8868\u91cc\u585e\u4e0a\u8fd9\u6837\u590d\u5236\u51fa\u6765\u7684 Stdin\u3001Stdout \u6216\u8005 Stderr \u53e5\u67c4\uff0cCreateProcess \u4f1a\u8c03\u7528\u5931\u8d25\uff0c\u7136\u540e\u7ed9\u4f60\u4e00\u4e2a 1450\uff08ERROR_NO_SYSTEM_RESOURCES\uff09 \u9519\u8bef\u3002\u8fd8\u6709\u5c31\u662f\uff0c\u867d\u7136\u6307\u5b9a\u4e86\u8981\u7ee7\u627f\u7684\u53e5\u67c4\u7684\u5217\u8868\uff0cCreateProcess \u7684\u90a3\u4e2a bInheritHandles \u51fd\u6570\u8fd8\u662f\u8981\u8bbe\u7f6e\u6210 TRUE \u7684\uff0c\u4e0d\u7136\u4f60\u5217\u8868\u91cc\u6307\u5b9a\u7684\u90a3\u4e9b\u53e5\u67c4\u5b83\u7ee7\u627f\u4e0d\u4e0b\u53bb\u3002\u4ee5\u53ca\u5728\u8981\u7ee7\u627f\u7684\u53e5\u67c4\u5217\u8868\u91cc\u6307\u5b9a\u7684\u90a3\u4e9b\u53e5\u67c4\uff0c\u4e5f\u5fc5\u987b\u662f\u53ef\u7ee7\u627f\u7684\uff0c\u5982\u679c\u6709\u4e0d\u53ef\u7ee7\u627f\u7684\u5728\u91cc\u9762\uff0cCreateProcess \u4f1a\u62a5 87\uff08ERROR_INVALID_PARAMETER\uff09 \u9519\u8bef\u3002<\/p>\n<p>\u6211\u8fd9\u8fb9\u5c31\u62ff\u4e00\u4e2a\u7a0b\u5e8f\u5c31\u4ee5\u7528lame\u91cd\u7f16\u7801MP3\u6587\u4ef6\u4e3a\u4f8b\u7684sample\u5427\u3002\u4ee3\u7801\u4e0d\u4e00\u5b9a\u5c31\u6ca1\u95ee\u9898\uff0c\u53ea\u6f14\u793a\u4e00\u4e0b\u7528\u6cd5<\/p>\n<pre>#include &lt;windows.h&gt;\n#include &lt;exception&gt;\n#include &lt;string&gt;\n#include &lt;vector&gt;\n#include &lt;functional&gt;\n\n#include &lt;atlbase.h&gt;\n\nclass OnExit\n{\n    std::function&lt;void()&gt; onExit_;\npublic:\n    template&lt;class T&gt; OnExit(const T&amp; lambda) : onExit_(lambda) {}\n    ~OnExit() { onExit_(); }\n};\n\nclass ReEncoderErr : public std::exception \n{\n    std::string msg_;\npublic:\n    ReEncoderErr(const char* msg) : msg_(msg) {}\n    const char* what() const { return msg_.c_str(); }\n};\n\nclass ReEncoder\n{\n    CHandle hProcDec_, hProcEnc_;\n    CHandle hPipeRead_, hPipeWrite_;\n\n    void ClosePipePair()\n    {\n        hPipeRead_.Close();\n        hPipeWrite_.Close();\n    }\n\n    void CreatePipePair()\n    {\n        ClosePipePair();\n\n        HANDLE hPipeRead, hPipeWrite;\n        SECURITY_ATTRIBUTES sa{};\n        sa.bInheritHandle = TRUE;\n        if (!CreatePipe(&amp;hPipeRead, &amp;hPipeWrite, &amp;sa, 0))\n            throw ReEncoderErr(\"Fail to create pipe\");\n\n        hPipeRead_.Attach(hPipeRead);\n        hPipeWrite_.Attach(hPipeWrite);\n    }\n\n    void RunLame(bool isEncoder)\n    {\n        wchar_t decCmdLine[] = L\"lame.exe --decode input.mp3 -\";\n        wchar_t encCmdLine[] = L\"lame.exe --preset medium - output.mp3\";\n        wchar_t* cmdLine;\n\n        STARTUPINFOEXW si{};\n        PROCESS_INFORMATION pi{};\n\n        std::vector&lt;HANDLE&gt; handlesToInherit;\n\n        si.StartupInfo.cb = sizeof(si);\n        si.StartupInfo.dwFlags = STARTF_USESTDHANDLES;\n\n        HANDLE hStdErr = GetStdHandle(STD_ERROR_HANDLE);\n\n        si.StartupInfo.hStdError = hStdErr;\n\n        if (isEncoder)\n        {\n            si.StartupInfo.hStdInput = hPipeRead_;\n            handlesToInherit.push_back(hPipeRead_);\n            cmdLine = encCmdLine;\n        }\n        else\n        {\n            si.StartupInfo.hStdOutput = hPipeWrite_;\n            handlesToInherit.push_back(hPipeWrite_);\n            cmdLine = decCmdLine;\n        }\n\n        SIZE_T nProcThreadAttrSize = 0;\n        InitializeProcThreadAttributeList(0, 1, 0, &amp;nProcThreadAttrSize);\n        if (nProcThreadAttrSize == 0)\n            throw ReEncoderErr(\"Fail to get size of procthreadattr\");\n\n        std::vector&lt;char&gt; buf(nProcThreadAttrSize);\n        si.lpAttributeList = reinterpret_cast&lt;LPPROC_THREAD_ATTRIBUTE_LIST&gt;(buf.data());\n        if (InitializeProcThreadAttributeList(si.lpAttributeList, 1, 0, &amp;nProcThreadAttrSize) == FALSE)\n            throw ReEncoderErr(\"Fail to init procthreadattr\");\n\n        OnExit clean([&amp;]() { DeleteProcThreadAttributeList(si.lpAttributeList); });\n\n        if (UpdateProcThreadAttribute(si.lpAttributeList, 0, PROC_THREAD_ATTRIBUTE_HANDLE_LIST, handlesToInherit.data(), handlesToInherit.size() * sizeof(HANDLE), NULL, NULL) == FALSE)\n            throw ReEncoderErr(\"Fail to update procthreadattr\");\n        \n        if (!CreateProcessW(NULL, cmdLine, NULL, NULL, TRUE, EXTENDED_STARTUPINFO_PRESENT, NULL, NULL, &amp;si.StartupInfo, &amp;pi))\n            throw ReEncoderErr(\"Fail to create process\");\n\n        CloseHandle(pi.hThread);\n\n        if (isEncoder)\n            hProcEnc_.Attach(pi.hProcess);\n        else\n            hProcDec_.Attach(pi.hProcess);\n    }\n\npublic:\n    void Run()\n    {\n        CreatePipePair();\n        RunLame(false);\n        RunLame(true);\n        ClosePipePair();\n\n        HANDLE handles[2] = { hProcEnc_.m_h, hProcDec_.m_h };\n        WaitForMultipleObjects(2, handles, TRUE, INFINITE);\n    }\n};\n\n#include &lt;iostream&gt;\n\nint main(int argc, char* argv[])\n{\n    try \n    {\n        ReEncoder reenc;\n        reenc.Run();\n        return 0;\n    }\n    catch (const std::exception&amp; exception)\n    {\n        std::cerr &lt;&lt; exception.what() &lt;&lt; std::endl;\n        return 1;\n    }\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u76f8\u4fe1\u7528 Windows API \u5199\u8fc7\u91cd\u5b9a\u5411\u5b50\u8fdb\u7a0b\u7684\u6807\u51c6\u8f93\u5165\u8f93\u51fa\u7684\u4eba\u90fd\u5f04\u8fc7\u8fd9\u4e1c\u897f\u3002\u5927\u6982\u8fc7\u7a0b\u662f\u4e0d\u96be\uff0c\u4e0d\u8fc7\u5bf9\u521d\u5b66\u8005\u5751 &hellip; <a href=\"https:\/\/blog.sorayuki.net\/?p=625\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">Windows\u4e0b\u63a7\u5236\u5b50\u8fdb\u7a0b\u7684\u53e5\u67c4\u7ee7\u627f<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-625","post","type-post","status-publish","format-standard","hentry","category-windows"],"_links":{"self":[{"href":"https:\/\/blog.sorayuki.net\/index.php?rest_route=\/wp\/v2\/posts\/625","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.sorayuki.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.sorayuki.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.sorayuki.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.sorayuki.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=625"}],"version-history":[{"count":0,"href":"https:\/\/blog.sorayuki.net\/index.php?rest_route=\/wp\/v2\/posts\/625\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.sorayuki.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.sorayuki.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.sorayuki.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}