{"id":72,"date":"2009-10-16T21:50:40","date_gmt":"2009-10-16T13:50:40","guid":{"rendered":"http:\/\/tmp.xujiwei.com\/blog\/?p=72"},"modified":"2010-01-28T22:33:18","modified_gmt":"2010-01-28T14:33:18","slug":"using-shellexecute-in-realbasic","status":"publish","type":"post","link":"https:\/\/xujiwei.com\/blog\/2009\/10\/using-shellexecute-in-realbasic\/","title":{"rendered":"REALbasic \u4e2d\u4f7f\u7528 ShellExecute \u6267\u884c\u547d\u4ee4"},"content":{"rendered":"<p>\u5728\u00a0REALbasic\u00a0\u4e2d\uff0c\u5982\u679c\u9700\u8981\u6267\u884c\u00a0cmd\u00a0\u547d\u4ee4\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u00a0Shell\u00a0\u7c7b\uff0c\u4f46\u662f\u8fd9\u6837\u7684\u8bdd\uff0c\u7f16\u8bd1\u6210\u00a0Windows\u00a0\u7a0b\u5e8f\u65f6\u4f1a\u989d\u5916\u9700\u8981\u4e00\u4e2a\u00a0Shell.dll\u00a0\u7684\u52a8\u6001\u94fe\u63a5\u5e93\uff0c\u8fd9\u5bf9\u4e8e\u6211\u8fd9\u6837\u7684\u00a01exe\u00a0\u7231\u597d\u8005\u662f\u4e0d\u80fd\u5fcd\u53d7\u7684\u3002\u4f46\u662f\u5bf9\u4e8e\u00a0Mac\u00a0OS\u00a0X\u00a0\u548c\u00a0Linux\u00a0\u7684\u751f\u6210\u76ee\u6807\u6765\u8bf4\uff0c\u662f\u4e0d\u5b58\u5728\u8fd9\u4e2a\u95ee\u9898\u7684\uff0c\u56e0\u4e3a\u00a0Mac\u00a0OS\u00a0X\u00a0\u7684\u5e94\u7528\u7a0b\u5e8f\u672c\u8eab\u5c31\u662f\u4e00\u4e2a\u6587\u4ef6\u5939\uff0c\u800c\u00a0Linux\u00a0\u7684\u76ee\u6807\u4e0d\u4f1a\u751f\u6210\u989d\u5916\u7684\u94fe\u63a5\u5e93\u3002\u56e0\u6b64\uff0c\u9700\u8981\u9488\u5bf9\u00a0Windows\u00a0\u8fdb\u884c\u7279\u6b8a\u5904\u7406\u3002\u4e8e\u662f\u5728\u7f51\u4e0a\u641c\u7d22\u89e3\u51b3\u65b9\u6848\uff0c\u627e\u5230\u4e86\u00a0VB\u00a0\u4e2d\u6267\u884c\u7a0b\u5e8f\u7684\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1.\u00a0\u4f7f\u7528\u00a0CreateProcess<\/strong><\/p>\n<p>\u901a\u8fc7\u00a0CreateProcess\u00a0\u4ee5\u53ca\u4f7f\u7528\u7ba1\u9053\uff0c\u53ef\u4ee5\u6267\u884c\u5916\u90e8\u7a0b\u5e8f\u5e76\u83b7\u53d6\u8f93\u51fa\uff0c\u4f46\u662f\u8fd9\u4e2a\u65b9\u6cd5\u8fc7\u4e8e\u70e6\u7410\uff0c\u5e76\u4e14\u6211\u4e5f\u4e0d\u9700\u8981\u5916\u90e8\u8fdb\u7a0b\u6267\u884c\u5b8c\u6bd5\u540e\u7684\u8f93\u51fa\u7ed3\u679c\uff0c\u56e0\u6b64\u4e0d\u91c7\u7528\u3002<\/p>\n<p><strong>2.\u00a0\u4f7f\u7528\u00a0Shell\u00a0\u65b9\u6cd5<\/strong><\/p>\n<p>VB\u00a0\u91cc\u6709\u4e00\u4e2a\u00a0Shell\u00a0\u65b9\u6cd5\uff0c\u4f46\u662f\u5728\u00a0RB\u00a0\u4e2d\u5e76\u6ca1\u6709\uff0c\u6240\u4ee5\u6b64\u8def\u4e0d\u901a\u3002<\/p>\n<p><strong>3.\u00a0\u4f7f\u7528\u00a0ShellExecute<\/strong><\/p>\n<p>\u8fd9\u4e2a\u65b9\u6cd5\u540c\u6837\u662f\u4e00\u4e2a\u7cfb\u7edf\u00a0API\uff0c\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u00a0RB\u00a0\u7684\u00a0declare\u00a0\u58f0\u660e\u5e76\u8c03\u7528\u5b83\uff0c\u5728\u6d4b\u8bd5\u4e4b\u540e\uff0c\u4f7f\u7528\u00a0declare\u00a0\u6765\u4f7f\u7528\u7cfb\u7edf\u00a0API\u00a0\u4e0d\u4f1a\u751f\u6210\u989d\u5916\u7684\u00a0dll\uff0c\u6b63\u662f\u6211\u9700\u8981\u7684\u3002<\/p>\n<p>\u9996\u5148\u5728\u00a0RB\u00a0\u7684\u67d0\u4e2a\u6a21\u5757\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b9\u6cd5\u00a0ShellExecute\uff0c\u7528\u6765\u5c01\u88c5\u5bf9\u7cfb\u7edf\u00a0API\u00a0\u7684\u8bf7\u6c42\uff1a<\/p>\n<blockquote><p>function\u00a0ShellExecute(hWnd\u00a0as\u00a0Integer,\u00a0lpOperation\u00a0as\u00a0String, _<br \/>\nlpFile\u00a0as\u00a0String,\u00a0lpParameters\u00a0as\u00a0String,\u00a0lpDirectory\u00a0as\u00a0String,\u00a0nShowCmd\u00a0as\u00a0Integer)<\/p><\/blockquote>\n<p>\u6ce8\u610f\uff0c\u5728\u00a0RB\u00a0\u4e2d\u6dfb\u52a0\u65b9\u6cd5\u65f6\uff0c\u53c2\u6570\u5217\u8868\u4e2d\u7684\u00a0_\u00a0\u9700\u8981\u53bb\u6389\uff0c\u8fd9\u91cc\u662f\u4e3a\u4e86\u6392\u7248\u7684\u9700\u8981\u800c\u52a0\u4e0a\u7684\u3002<\/p>\n<p>\u8fd9\u4e2a\u00a0API\u00a0\u662f\u5b9a\u4e49\u5728\u00a0shell32.dll\u00a0\u4e2d\u7684\uff0c\u5728\u00a0ShellExecute\u00a0\u65b9\u6cd5\u4e2d\u5148\u9700\u8981\u58f0\u660e\uff1a<\/p>\n<blockquote><p>soft\u00a0declare\u00a0function\u00a0ShellExecuteA\u00a0Lib\u00a0\"shell32.dll\"\u00a0_<br \/>\n(ByVal\u00a0hWnd\u00a0As\u00a0Integer,\u00a0ByVal\u00a0lpOperation\u00a0As\u00a0CString,\u00a0_<br \/>\nByVal\u00a0lpFile\u00a0As\u00a0CString,\u00a0ByVal\u00a0lpParameters\u00a0As\u00a0CString,\u00a0_<br \/>\nByVal\u00a0lpDirectory\u00a0As\u00a0CString,\u00a0ByVal\u00a0nShowCmd\u00a0As\u00a0Integer)\u00a0As\u00a0Integer<\/p>\n<p>soft\u00a0declare\u00a0function\u00a0ShellExecuteW\u00a0Lib\u00a0\"shell32.dll\"\u00a0_<br \/>\n(ByVal\u00a0hWnd\u00a0As\u00a0Integer,\u00a0ByVal\u00a0lpOperation\u00a0As\u00a0CString,\u00a0_<br \/>\nByVal\u00a0lpFile\u00a0As\u00a0CString,\u00a0ByVal\u00a0lpParameters\u00a0As\u00a0CString,\u00a0_<br \/>\nByVal\u00a0lpDirectory\u00a0As\u00a0CString,\u00a0ByVal\u00a0nShowCmd\u00a0As\u00a0Integer)\u00a0As\u00a0Integer<\/p><\/blockquote>\n<p>\u7136\u540e\u6765\u8c03\u7528\u8fd9\u4e2a\u00a0API\uff1a<\/p>\n<pre lang=\"vb\">  dim ret as Integer\r\n    Try\r\n      ret = ShellExecuteW(hWnd, ConvertEncoding(lpOperation, Encodings.UTF16), _\r\n      ConvertEncoding(lpFile, Encodings.UTF16), _\r\n      ConvertEncoding(lpParameters, Encodings.UTF16), _\r\n      ConvertEncoding(lpDirectory, Encodings.UTF16), nShowCmd)\r\n    Catch\r\n      ret = ShellExecuteA(hWnd, lpOperation, lpFile, lpParameters, lpDirectory, nShowCmd)\r\n    End\r\n    return ret<\/pre>\n<p>\u628a ShellExecuteW \u653e\u5728 Try ... Catch \u4e2d\u662f\u4e3a\u4e86\u517c\u5bb9\u7684\u9700\u8981\uff0c\u56e0\u4e3a\u50cf Windows 98 \u8fd9\u6837\u6bd4\u8f83\u8001\u7684\u7cfb\u7edf\u4e2d\uff0c\u7cfb\u7edf\u5185\u90e8\u7f16\u7801\u8fd8\u4e0d\u662f Unicode\uff0c\u8fd8\u6ca1\u6709 ShellExecuteW \u8fd9\u6837\u7ed3\u5c3e\u662f W \u7684 API\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u56e0\u4e3a RB \u5185\u90e8\u662f\u4f7f\u7528\u7684 UTF-8 \u7f16\u7801\uff0c\u800c\u7cfb\u7edf API ShellExecuteW \u4f7f\u7528\u7684\u7f16\u7801\u662f Unicode\uff0c\u6240\u4ee5\u53c2\u6570\u5728\u4f20\u9012\u524d\u9700\u8981\u8fdb\u884c\u8f6c\u7801\uff0c\u5c06\u5b57\u7b26\u4e32\u53c2\u6570\u503c\u8f6c\u6362\u4e3a Unicode \u7f16\u7801\uff0c\u5426\u5219\u5728\u6267\u884c\u4e00\u4e2a\u660e\u660e\u6b63\u786e\u7684\u547d\u4ee4\u65f6\uff0c\u4f1a\u51fa\u73b0\u201c\u627e\u4e0d\u5230\u6587\u4ef6\u201d\u7684\u9519\u8bef\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u4f7f\u7528 ShellExecute \u65b9\u6cd5\u6765\u6267\u884c\u5916\u90e8\u7a0b\u5e8f\u4e86\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre lang=\"vb\">  #if TargetWin32\r\n    dim ret as Integer\r\n    ret = Win32Helper.ShellExecute(hWnd, \"open\", \"cmd.exe\", _\r\n    \"\/c shutdown -t 10\", \"\", 0)\r\n  #endif<\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u5c31\u8c03\u7528 cmd.exe \u6267\u884c\u4e86\u547d\u4ee4 shutdown -s -t 10\uff0c\u4e5f\u5c31\u662f 10 \u79d2\u540e\u5173\u673a\u3002<\/p>\n<p>\u5176\u5b9e\u8fd9\u4e2a\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u7528\u6765\u6253\u5f00\u6587\u6863\u6216\u8005\u7f51\u5740\uff0c\u5177\u4f53\u7684\u7528\u6cd5\u53ef\u4ee5\u53bb MSDN \u627e\u4e00\u627e\u3002<\/p>\n<p>\u5f53\u7136\uff0c\u5982\u679c\u4e0d\u5728\u4e4e\u751f\u6210\u7684 Windows \u76ee\u6807\u8fd8\u6709\u989d\u5916\u7684 dll \u7684\u8bdd\uff0c\u5b8c\u5168\u53ef\u4ee5\u4f7f\u7528 RB \u81ea\u5e26\u7684 Shell \u7c7b\uff0c\u529f\u80fd\u4e5f\u5f3a\u4e0a\u4e0d\u5c11\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u00a0REALbasic\u00a0\u4e2d\uff0c\u5982\u679c\u9700\u8981\u6267\u884c\u00a0cmd\u00a0\u547d\u4ee4\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u00a0Shell &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/xujiwei.com\/blog\/2009\/10\/using-shellexecute-in-realbasic\/\">\u7ee7\u7eed\u9605\u8bfb &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[55,62,56],"_links":{"self":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/posts\/72"}],"collection":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/comments?post=72"}],"version-history":[{"count":0,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/posts\/72\/revisions"}],"wp:attachment":[{"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/media?parent=72"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/categories?post=72"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xujiwei.com\/blog\/wp-json\/wp\/v2\/tags?post=72"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}