{"id":612,"date":"2014-09-15T17:13:47","date_gmt":"2014-09-15T09:13:47","guid":{"rendered":"http:\/\/blog.qdac.cc\/?p=612"},"modified":"2014-09-15T20:12:03","modified_gmt":"2014-09-15T12:12:03","slug":"c-builder%e4%b8%ad%e6%8e%a5%e5%8f%a3%e7%9a%84%e8%ae%bf%e9%97%ae%e4%bd%bf%e7%94%a8queryinterface","status":"publish","type":"post","link":"https:\/\/blog.qdac.cc\/?p=612","title":{"rendered":"C++ Builder\u4e2d\u4f7f\u7528\u63a5\u53e3"},"content":{"rendered":"<p>\u8fd9\u662f\u4e00\u4e2a\u6211\u60f3\u9a82\u4eba\u7684\u8bdd\u9898\uff0c\u4f46\u6211\u8fd8\u662f\u5fcd\u4f4f\u88c5\u4e00\u628a\u6587\u660e\u4eba\u5427\uff01<\/p>\n<p>\u9996\u5148\u8bf4\u95ee\u9898\u7684\u6765\u6e90\uff0c\u5929\u5730\u5f26\u8ba9\u6211\u5e2e\u7740\u770b\u4e00\u4e2a\u5728C++ Builder\u8c03\u7528QueryInterface\u7684\u95ee\u9898\uff0c\u7ed3\u679c\u53d1\u73b0\u90c1\u95f7\u7684\u7d27\uff0c\u603b\u62a5\u5f02\u5e38\uff0c\u8981\u4e48\u5c31\u662f\u63d0\u793a\u8c03\u7528\u4e86\u7eaf\u865a\u51fd\u6570\uff0c\u627e\u6765\u627e\u53bb\uff0c\u539f\u6765\u9700\u8981\u81ea\u5df1\u5b9e\u73b0\u4e00\u904dAddRef\u548cRelease\u51fd\u6570\uff0c\u77ac\u5373\u86cb\u75bc\u3002<\/p>\n<p>\u597d\u5728EMB\u6709\u4e00\u70b9\u826f\u5fc3\u5427\uff0c\u5728systobj.h\u91cc\u521b\u5efa\u4e86\u4e00\u4e2a\u5b8fINTFOBJECT_IMPL_IUNKNOWN\uff0c\u4f46\u5374\u86cb\u75bc\u7684\u7531\u4e8eQueryInterface\u4e0e\u57fa\u7c7b\u7684\u58f0\u660e\u5f62\u5f0f\u4e0d\u4e00\u81f4\uff0c\u9020\u6210\u4e24\u4e2a\u8b66\u544a\uff0c\u6240\u4ee5\u81ea\u5df1\u5b9e\u73b0\u4e00\u4e2a\u5b8f\u5b9a\u4e49\uff1a<\/p>\n<pre class=\"lang:c++ decode:true\">#define INTF_IMP_REFCOUNT(BASE) \\\r\n ULONG __stdcall AddRef() { return BASE::_AddRef();} \\\r\n ULONG __stdcall Release(){ return BASE::_Release();}\\\r\n HRESULT __stdcall QueryInterface(REFIID iid, void*p) { return BASE::QueryInterface(iid, p);}\r\n<\/pre>\n<p>\u5728\u76f8\u5e94\u63a5\u53e3\u7684\u5b9a\u4e49\u91cc\u52a0\u5165\u5b83\u5c31OK\uff0c\u5982TForm1\u6211\u4eec\u5b9e\u73b0\u4e86\u63a5\u53e3ITest\uff0c\u90a3\u4e48\u4ee3\u7801\u53c2\u8003\u5982\u4e0b\uff1a<\/p>\n<pre class=\"lang:c++ decode:true\">class TForm1:public TForm, public ITest\r\n{\r\n...\r\npublic:\r\n   INTF_IMP_REFCOUNT(TForm)\r\n};<\/pre>\n<p>\u7136\u540e\u518d\u8c03\u7528TForm1\u7684QueryInterface\u5c31\u6ca1\u6709\u95ee\u9898\u4e86\u3002<\/p>\n<p>\u73b0\u5728\u603b\u7ed3\u4e00\u4e0bC++ Builder\u4e2d\u4f7f\u7528\u63a5\u53e3\u7684\u57fa\u672c\u89c4\u5219\uff1a<\/p>\n<p>1\u3001\u9996\u5148\uff0c\u8981\u901a\u8fc7QueryInterface\u6765\u83b7\u53d6\u4e00\u4e2a\u63a5\u53e3\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u5fc5\u9700\u4fdd\u8bc1\u5b9e\u73b0\u4e86AddRef\/Release\u51fd\u6570\uff0c\u56e0\u4e3a\u4f60\u7684TInterfacedObject\u5f88\u9057\u61be\u7684\u6ca1\u6709\u5b9e\u73b0AddRef\/Release\uff0cTInterfacedObject\u5b9e\u73b0\u7684\u662f_AddRef\/_Release\uff0cDelphi\u8c03\u7528\u7684\u5b83\uff0c\u4f46CB\u5374\u6ca1\u6709\uff0c\u6240\u4ee5\u4f60\u9700\u8981\u81ea\u5df1\u52a0\u5165AddRef\/Release\u7684\u5b9e\u73b0\u3002<\/p>\n<p>2\u3001C++ Builder\u7684systobj.h\u5b9e\u73b0\u4e86\u4e00\u4e2aTCppInterfacedObject\uff0c\u53ea\u80fd\u8bf4\u9e21\u808b\u3002\u9996\u5148\u662fVCL\u5bf9\u8c61\u4e0d\u5141\u8bb8\u591a\u91cd\u7ee7\u627f\uff0c\u50cfTForm\u5565\u7684\u73b0\u6210\u7684\u7ec4\u4ef6\u4e0a\uff0c\u4f60\u8981\u7ee7\u627f\u5b9e\u73b0\u4e00\u4e2a\u63a5\u53e3\uff0c\u5c31\u6ca1\u6cd5\u7ee7\u627f\u81ea\u5b83\uff0c\u5b9e\u9645\u4e0a\u4f60\u8fd8\u662f\u9700\u8981\u91cd\u5199\u5b83\u3002\u91cd\u5199\u4e5f\u5c31\u91cd\u5199\u5427\uff0c\u7ed3\u679c\u4e24\u4e2a\u63a5\u53e3\u8fd8\u4e0d\u4e00\u81f4\uff0c\u9020\u6210\u8b66\u544a\u3002<\/p>\n<p>3\u3001\u6240\u6709\u7684\u63a5\u53e3\u5fc5\u9700\u7ee7\u627f\u81eaIUnknown\uff0c\u5426\u5219QueryInterface\u4f1a\u50bb\u6389\u3002<\/p>\n<p>4\u3001\u5982\u679c\u4f60\u662f\u7c7b\u7684\u5b9e\u4f8b\u5730\u5740\uff0c\u90a3\u4e48\u76f4\u63a5\u7528dynamic_cast&lt;\u63a5\u53e3\u7c7b\u578b*&gt;(\u5bf9\u8c61\u5b9e\u4f8b)\u8981\u6bd4QueryInterface\u597d\u7684\u591a\uff0c\u56e0\u4e3a\u5728C++\u91cc\uff0c\u6240\u6709\u7684\u63a5\u53e3\u662f\u7eaf\u865a\u51fd\u6570\uff0c\u6240\u4ee5\u5b50\u7c7b\u80af\u5b9a\u53ef\u4ee5\u8f6c\u6362\u6210\u7236\u7c7b\u3002<\/p>\n<p>5\u3001\u5982\u679c\u4f60\u5f97\u5230\u7684\u662fIUnknown\u7684\u63a5\u53e3\u5b9e\u4f8b\uff0c\u53ef\u4ee5\u7528interface_cast\u6216\u8005\u7528QueryInterface\u65b9\u6cd5\u4e86\u3002<\/p>\n<p>\u7b80\u5355\u9644\u4e00\u4e0binterface\u7684\u8bed\u6cd5\uff1a<\/p>\n<pre class=\"lang:c++ decode:true \">interface\/struct declspec(uuid(GUID\u7f16\u7801\u5b57\u7b26\u4e32)) \u63a5\u53e3\u7c7b\u578b\u540d\u79f0:public \u7236\u63a5\u53e3\u7c7b\u578b\r\n{\r\nvirtual \u51fd\u6570\u540d(\u51fd\u6570\u53c2\u6570)=0;\r\n...\r\n};<\/pre>\n<p>\u5b9e\u9645\u4e0a\u5728C++\u4e2d\uff0cinterface\u4e5f\u4e0d\u8fc7\u662fstruct\uff0c\u6240\u4ee5\u4e24\u8005\u5199\u6210\u5565\u90fd\u884c\u3002<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u662f\u4e00\u4e2a\u6211\u60f3\u9a82\u4eba\u7684\u8bdd\u9898\uff0c\u4f46\u6211\u8fd8\u662f\u5fcd\u4f4f\u88c5\u4e00 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[8],"tags":[],"class_list":["post-612","post","type-post","status-publish","format-standard","hentry","category-delphi"],"views":4503,"_links":{"self":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/612","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=612"}],"version-history":[{"count":7,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/612\/revisions"}],"predecessor-version":[{"id":620,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/612\/revisions\/620"}],"wp:attachment":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}