{"id":1577,"date":"2024-06-17T08:56:47","date_gmt":"2024-06-17T00:56:47","guid":{"rendered":"https:\/\/lyvba.com\/?p=1577"},"modified":"2024-06-17T08:56:47","modified_gmt":"2024-06-17T00:56:47","slug":"msvc_bstr_t_01","status":"publish","type":"post","link":"https:\/\/lyvba.com\/index.php\/2024\/06\/17\/msvc_bstr_t_01\/","title":{"rendered":"CorelDRAW CPG\u63d2\u4ef6\u5b66\u4e60: Visual C++ \u4e2d\u7528\u4e8e\u7ba1\u7406 COM \u5b57\u7b26\u4e32\u7684 C++ \u5305\u88c5\u7c7b _bstr_t \u4ecb\u7ecd"},"content":{"rendered":"<h2><code>_bstr_t<\/code> \u662f Visual C++ \u4e2d\u7528\u4e8e\u7ba1\u7406 COM \u5b57\u7b26\u4e32\u7684\u4e00\u79cd C++ \u5305\u88c5\u7c7b\u3002\u5b83\u7684\u5168\u79f0\u662f &quot;Basic String Template&quot;\u3002<\/h2>\n<h3><code>_bstr_t<\/code> \u7684\u4e3b\u8981\u7279\u70b9\u5982\u4e0b:<\/h3>\n<ol>\n<li>\n<p><strong>\u81ea\u52a8\u5185\u5b58\u7ba1\u7406<\/strong>: <code>_bstr_t<\/code> \u7c7b\u4f1a\u81ea\u52a8\u7ba1\u7406\u5176\u5185\u90e8\u5b57\u7b26\u4e32\u7f13\u51b2\u533a\u7684\u5185\u5b58\u5206\u914d\u548c\u91ca\u653e,\u907f\u514d\u4e86\u624b\u52a8\u7ba1\u7406\u5185\u5b58\u7684\u9ebb\u70e6\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u65e0\u9700\u624b\u52a8\u8f6c\u6362<\/strong>: \u4f7f\u7528 <code>_bstr_t<\/code> \u53ef\u4ee5\u5728 BSTR \u548c C++ \u5b57\u7b26\u4e32\u4e4b\u95f4\u8fdb\u884c\u81ea\u52a8\u8f6c\u6362,\u65e0\u9700\u624b\u52a8\u8c03\u7528 <code>SysAllocString()<\/code> \u6216 <code>SysFreeString()<\/code> \u7b49 COM \u5b57\u7b26\u4e32\u51fd\u6570\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u652f\u6301\u8d4b\u503c\u64cd\u4f5c<\/strong>: <code>_bstr_t<\/code> \u7c7b\u652f\u6301\u5404\u79cd\u8d4b\u503c\u64cd\u4f5c,\u5305\u62ec\u4ece <code>BSTR<\/code>\u3001<code>char*<\/code>\u3001<code>wchar_t*<\/code> \u7b49\u8f6c\u6362,\u4ee5\u53ca\u62f7\u8d1d\u8d4b\u503c\u7b49\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u652f\u6301\u6d41\u64cd\u4f5c\u7b26<\/strong>: <code>_bstr_t<\/code> \u7c7b\u91cd\u8f7d\u4e86 <code>&lt;&lt;<\/code> \u548c <code>&gt;&gt;<\/code> \u8fd0\u7b97\u7b26,\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728\u8f93\u5165\/\u8f93\u51fa\u6d41\u4e2d\u4f7f\u7528\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5f02\u5e38\u5b89\u5168<\/strong>: <code>_bstr_t<\/code> \u7c7b\u5728\u5904\u7406 COM \u5b57\u7b26\u4e32\u65f6\u4f1a\u629b\u51fa\u5f02\u5e38,\u6709\u52a9\u4e8e\u7f16\u5199\u5f02\u5e38\u5b89\u5168\u7684\u4ee3\u7801\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u603b\u7684\u6765\u8bf4, <code>_bstr_t<\/code> \u7c7b\u7b80\u5316\u4e86 COM \u7f16\u7a0b\u4e2d\u5b57\u7b26\u4e32\u7684\u7ba1\u7406,\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u5b83\u662f Visual C++ \u4e2d\u5e38\u7528\u7684 COM \u7f16\u7a0b\u5de5\u5177\u4e4b\u4e00\u3002<\/p>\n<h2><code>_bstr_t<\/code> \u5b57\u7b26\u4e32\u6478\u677f\u7c7b\u4f7f\u7528\u793a\u4f8b\u4ee3\u7801<\/h2>\n<pre><code class=\"language-cpp\">#include &lt;comdef.h&gt;\n#include &lt;comutil.h&gt;\n\nint main() {\n  _bstr_t bstr = &quot;Hello, World!&quot;;\n  MessageBox(NULL, bstr, &quot;Hello, World!&quot;, MB_OK);\n\n  \/\/ \u58f0\u660e _bstr_t \u53d8\u91cf\n  _bstr_t bstrMyString;\n  _bstr_t strMyString;\n\n  \/\/ \u8d4b\u503c:\n  strMyString = &quot;Hello, world!&quot;;    \/\/ \u4ece C-style \u5b57\u7b26\u4e32\u8d4b\u503c\n  bstrMyString = L&quot;\u4f60\u597d, \u4e16\u754c!&quot;;    \/\/ \u4ece wide \u5b57\u7b26\u4e32\u8d4b\u503c\n  \/\/ bstrMyString = SomeComFunction(); \/\/ \u4ece BSTR \u8d4b\u503c\n\n  \/\/ \u4f7f\u7528 _bstr_t \u5bf9\u8c61:\n  MessageBox(NULL, bstrMyString, &quot;My String&quot;, MB_OK);\n  printf(&quot;%s\\n&quot;, static_cast&lt;const char *&gt;(bstrMyString));\n\n \/\/ fprintf(file, &quot;%S\\n&quot;, static_cast&lt;const wchar_t *&gt;(bstrMyString));\n\n  return 0;\n}<\/code><\/pre>\n<h3>\u91ca\u653e\u8d44\u6e90:<\/h3>\n<ul>\n<li><code>_bstr_t<\/code> \u5bf9\u8c61\u4f1a\u81ea\u52a8\u91ca\u653e\u5176\u5185\u90e8\u7ba1\u7406\u7684 BSTR \u5b57\u7b26\u4e32,\u65e0\u9700\u624b\u52a8\u91ca\u653e\u3002<\/li>\n<li>\u4f46\u5982\u679c\u9700\u8981\u624b\u52a8\u91ca\u653e BSTR \u5b57\u7b26\u4e32,\u53ef\u4ee5\u4f7f\u7528 <code>::SysFreeString(bstrMyString)<\/code>;<\/li>\n<\/ul>\n<h3>\u5176\u4ed6\u64cd\u4f5c:<\/h3>\n<ul>\n<li>\u652f\u6301\u5404\u79cd\u8d4b\u503c\u64cd\u4f5c, <code>+=<\/code>, <code>-=<\/code>, <code>=<\/code> \u7b49<\/li>\n<li>\u652f\u6301\u6d41\u64cd\u4f5c\u7b26 <code>&lt;&lt;<\/code> \u548c <code>&gt;&gt;<\/code><\/li>\n<li>\u652f\u6301\u6bd4\u8f83\u64cd\u4f5c\u7b26, <code>==<\/code>, <code>!=<\/code>, <code>&lt;<\/code>, <code>&gt;<\/code> \u7b49<\/li>\n<\/ul>\n<p>\u603b\u4e4b, <code>_bstr_t<\/code> \u7c7b\u63d0\u4f9b\u4e86\u4e00\u79cd\u5b89\u5168\u3001\u7b80\u4fbf\u7684\u65b9\u5f0f\u6765\u7ba1\u7406 COM \u7f16\u7a0b\u4e2d\u7684\u5b57\u7b26\u4e32\u3002\u5b83\u53ef\u4ee5\u5927\u5927\u7b80\u5316\u4ee3\u7801,\u63d0\u9ad8\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>_bstr_t \u662f Visual C++ \u4e2d\u7528\u4e8e\u7ba1\u7406 COM \u5b57\u7b26\u4e32\u7684\u4e00\u79cd C++ \u5305\u88c5\u7c7b\u3002\u5b83\u7684\u5168\u79f0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1380,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[31],"class_list":["post-1577","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-learn","tag-cpp"],"_links":{"self":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/1577","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/comments?post=1577"}],"version-history":[{"count":1,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/1577\/revisions"}],"predecessor-version":[{"id":1578,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/1577\/revisions\/1578"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/media\/1380"}],"wp:attachment":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/media?parent=1577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/categories?post=1577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/tags?post=1577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}