{"id":5170,"date":"2020-01-01T10:31:13","date_gmt":"2020-01-01T02:31:13","guid":{"rendered":"http:\/\/blog.qdac.cc\/?p=5170"},"modified":"2020-01-01T10:36:26","modified_gmt":"2020-01-01T02:36:26","slug":"%e5%85%83%e6%97%a6%e7%a4%bc%e7%89%a9-%e4%bd%bf%e7%94%a8-tcomtypelib-%e8%b0%83%e7%94%a8-net-dll","status":"publish","type":"post","link":"https:\/\/blog.qdac.cc\/?p=5170","title":{"rendered":"[\u5143\u65e6\u793c\u7269] \u4f7f\u7528 TComTypeLib \u8c03\u7528 .Net DLL"},"content":{"rendered":"\n<p>\u9996\u5148\u611f\u8c22 2019 \u5e74\u8bb8\u591a\u7684\u670b\u53cb\u5bf9 QDAC \u9879\u76ee\u7684\u652f\u6301\uff0c2019 \u5e74\u56e0\u4e3a\u7cbe\u529b\u7684\u539f\u56e0\uff0c\u9879\u76ee\u7684\u66f4\u65b0\u548c\u8fdb\u5ea6\u53ef\u80fd\u5e76\u4e0d\u5982\u5927\u5bb6\u7684\u610f\u3002\u4f46\u662f\u4f5c\u8005\u60f3\u8bf4\uff0c\u6211\u5e76\u6ca1\u6709\u653e\u5f03 QDAC \u9879\u76ee\uff0c\u53ea\u8981\u5927\u5bb6\u8fd8\u5728\uff0c QDAC \u9879\u76ee\u5c31\u8fd8\u5728\uff0c\u5c31\u4f1a\u7ee7\u7eed\u7ef4\u62a4\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u8fdb\u5165\u6b63\u9898\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u3010\u524d\u7f6e\u6761\u4ef6\u3011<\/h4>\n\n\n\n<p>1\u3001\u8fd9\u4e2a\u5e93\u662f\u7ee7\u4e8e COM \u7684 IDispatch \u63a5\u53e3\u5b9e\u73b0\u7684\uff0c\u6240\u4ee5\u8981\u6c42\u8981\u8c03\u7528\u7684 DLL \u9700\u8981\u7f16\u8bd1\u4e3a\u652f\u6301 COM \u7684\u7248\u672c\uff0c\u5177\u4f53\u53c2\u8003\u6559\u7a0b<a href=\"https:\/\/blog.qdac.cc\/?p=5152\">\u5728 Delphi \u4e2d\u8c03\u7528 .Net \u7684 DLL<\/a>\u3002\u4e0d\u8981\u5bf9\u4f5c\u8005\u7684\u80fd\u529b\u62a5\u592a\u9ad8\u7684\u671f\u5f85\uff0c\u6bd5\u7adf\u4f5c\u8005\u7cbe\u529b\u6709\u9650\uff0c\u6df1\u5165\u7814\u7a76 .NET DLL \u7684\u5185\u5bb9\uff0c\u53cd\u5411\u7f16\u8bd1\u51fa PAS \u5355\u5143\u5b9e\u5728\u662f\u9700\u8981\u592a\u591a\u7684\u65f6\u95f4\u3002<\/p>\n\n\n\n<p>2\u3001\u8fd9\u4e2a\u5e93\u53ea\u652f\u6301 Windows \uff0c\u9700\u8981\u5ba2\u6237\u673a\u5668\u4e0a\u5df2\u7ecf\u6709 .NET \u8fd0\u884c\u65f6\u3002\u5177\u4f53\u9700\u8981\u7684\u7248\u672c\u53d6\u51b3\u4e8e DLL \u5b9e\u73b0\u8981\u6c42\u7684\u7248\u672c\u3002<\/p>\n\n\n\n<p>3\u3001\u5982\u679c\u53ef\u4ee5\u4e0d\u7528 TLB \u652f\u6301\u4e5f\u80fd\u5b9e\u73b0\uff0c\u4f46\u6211\u8fd8\u662f\u9009\u62e9\u8fd9\u4e2a\u5e93\u9700\u8981 TLB \u6587\u4ef6\u652f\u6301\u3002\u5982\u679c\u4e0d\u60f3\u7528 TLB \u652f\u6301\uff0c\u5219\u65e0\u6cd5\u5b9e\u73b0\u8fd0\u884c\u65f6\u52a8\u6001\u8c03\u7528\uff0c\u53ea\u80fd\u5728\u8bbe\u8ba1\u671f\u5c31\u8ba1\u5212\u597d\u4ee3\u7801\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u3010\u8c03\u7528\u6b65\u9aa4\u3011<\/h4>\n\n\n\n<p>1\u3001\u5728 uses \u5f15\u7528 comlib \u5355\u5143\u3002<br>2\u3001\u8c03\u7528 CoClassExists \u51fd\u6570\uff0c\u6765\u5224\u65ad\u4f60\u8981\u8c03\u7528\u7684 .NET DLL \u4e2d\u7684\u7ec4\u4ef6\u662f\u5426\u5df2\u7ecf\u6ce8\u518c\u3002\u5982\u679c\u6ca1\u6709\u6ce8\u518c\uff0c\u8c03\u7528 RegisterClrLibrary \u6765\u6ce8\u518c DLL \u7ec4\u4ef6\u3002\u5982\u679c\u4e0d\u5b58\u5728\u5bf9\u5e94\u7684  .TLB \u6587\u4ef6\uff0c\u5219\u5728 regasm \u8c03\u7528\u6ce8\u518c\u7ec4\u4ef6\u6210\u529f\u65f6\uff0c\u4f1a\u76f4\u63a5\u8fd4\u56de true\uff0c\u5982\u679c\u5b58\u5728\uff0c\u5219\u4f1a\u5b9e\u9645\u68c0\u67e5\u5176\u4e2d\u7684\u7c7b\u662f\u5426\u771f\u6b63\u6ce8\u518c\u3002<br>3\u3001\u8fd9\u91cc\u5c31\u8981\u5206\u53c9\u4e86\uff0c\u4e24\u79cd\u9009\u62e9\uff1a<br>\uff081\uff09\u3001\u5982\u679c\u4e0d\u4f7f\u7528 TLB \u652f\u6301\uff0c\u90a3\u4e48\u4f60\u5c31\u8981\u7528 OLE \u65b9\u5f0f\u8c03\u7528\u3002\u4f60\u9700\u8981\u652f\u6301\u8981\u521b\u5efa\u7684\u5bf9\u5e94\u7684 CoClass \u5b9e\u4f8b\u7684 ClassId\uff08GUID\u7f16\u7801\uff09 \u6216\u8005\u662f ProgId\uff08\u7c7b\u4f3c\u4e8e AAA.BBB \u683c\u5f0f\uff09 \u3002\u5982\u679c\u662f ClassId \u8c03\u7528 CreateComObject \u521b\u5efa\u5b9e\u4f8b\uff0c\u5982\u679c\u662f ProgId \u8c03\u7528 CreateOleObject \u521b\u5efa\u5b9e\u4f8b\u3002\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\u4ee5 ClassId \u4e3a\u4f8b\uff0cITestLib \u5c31\u662f\u5bf9\u5e94\u7684 .NET \u4e2d\u7684\u4e00\u4e2a CoClass \u5b9e\u4f8b ID\u3002\u8981\u83b7\u53d6\u8fd9\u4e2a\u4fe1\u606f\uff0c\u53ef\u4ee5\u7528 Delphi \u76f4\u63a5\u6253\u5f00\u5bf9\u5e94\u7684 TLB \u6587\u4ef6\u5c31\u53ef\u4ee5\u770b\u5230\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var\n  V: OleVariant;\nconst\n  ITestLib: TGuid = '{EA2F140A-108F-47AE-BBD5-83EEE646CC0D}';\nbegin\n  V := IDispatch(CreateComObject(ITestLib));\n  ShowMessage(VarToStr(V.Add(1, 2)));\nend;<\/code><\/pre>\n\n\n\n<p>\u5173\u4e8e\u8fd9\u79cd\u8c03\u7528\u65b9\u5f0f\u7b80\u5355\uff0c\u4f46 Delphi \u5b9e\u9645\u4e0a\u540e\u53f0\u505a\u4e86\u5f88\u591a\u91cd\u590d\u5de5\u4f5c\uff1a<br>\u2192 \u7f16\u8bd1\u65f6\u5c06\u8c03\u7528\u6539\u4e3a\u5bf9 System.Variants._DispInvoke \u51fd\u6570\u8c03\u7528\u5e76\u5b8c\u6210\u53c2\u6570\u8f6c\u6362<br>\u2192 \u8c03\u7528 System.Variants.DispInvokeCore \u51fd\u6570<br>\u2192 \u8c03\u7528 System.Win.ComObj.VarDispInvoke \u51fd\u6570<br>\u2192 \u8c03\u7528 GetIDsOfNames \u83b7\u53d6 Add \u8fd9\u4e2a\u51fd\u6570\u7684 DispID <br>\u2192 \u8c03\u7528  System.Win.ComObj. DispatchInvoke \u51fd\u6570<br>\u2192 \u8c03\u7528 GetDispatchInvokeArgs \u5c06\u53c2\u6570\u5217\u8868\u4ece TVarArgList \u8f6c\u6362\u4e3a TDispParams \u683c\u5f0f\uff0c\u4ee5\u51c6\u5907\u8c03\u7528\u3002<br>\u2192 \u8c03\u7528 IDispatch \u63a5\u53e3\u7684 Invoke \u51fd\u6570\u5b8c\u6210\u5b9e\u9645\u7684\u8c03\u7528\u5e76\u8fd4\u56de\u3002<\/p>\n\n\n\n<p>\u4f60\u7684\u6bcf\u4e00\u6b21\u8c03\u7528\u6bbe\u5bf9\u5e94\u8fd9\u4e9b\u5f00\u9500\uff0c\u5728\u4f60\u65b9\u4fbf\u7684\u80cc\u540e\uff0c\u7f16\u8bd1\u5668\u4e3a\u4f60\u505a\u4e86\u5f88\u591a\u5f88\u591a\u3002<\/p>\n\n\n\n<p>\u5f53\u7136\u4f60\u4e5f\u53ef\u4ee5\u81ea\u5df1\u5b9e\u73b0\u76f8\u5173\u7684\u903b\u8f91\uff0c\u6765\u5b8c\u6210\u8fd0\u884c\u65f6\u52a8\u6001\u786e\u5b9a\u8981\u8c03\u7528\u7684\u51fd\u6570\u52a8\u6001\u8c03\u7528\u3002<\/p>\n\n\n\n<p>\uff082\uff09\u5982\u679c\u4f7f\u7528 TLB \u652f\u6301\uff0c\u5219\u53ef\u4ee5\u521b\u5efa TComTypeLib \u5b9e\u4f8b\uff0c\u5e76\u7528 LoadFromFile \u6765\u52a0\u8f7d\u5bf9\u5e94\u7684 TLB \u6587\u4ef6\uff0c\u7136\u540e\u4e0a\u9762\u7684\u4ee3\u7801\u5c31\u7b80\u5316\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  var\n    ATypeLib: TComTypeLib;\n    AType: TComType;\n    AFunc: TComFunction;\n    AInst: IDispatch;\n    AParams: TArray&lt;TValue>;\n  begin\n      ATypeLib := TComTypeLib.Create;\n      try\n        ATypeLib.LoadFromFile(AFileName);\n        if not ATypeLib.Registered then\n          RegisterClrLibrary(FLibraryFile);\n        AType := ATypeLib.FindType(\u7c7b\u578b\u7684\u540d\u79f0\u6216ID);\n        if not Assigned(AType) then\n          raise Exception.Create('Class not found.');\n        \/\/\u521b\u5efa\u5b9e\u4f8b\n        AInst := IDispatch(AType.CreateInstance);\n        \/\/\u67e5\u627e\u51fd\u6570\n        AFunc := AType.FindFunction('Add');\n        if Assigned(AFunc) then\n        begin\n          SetLength(AParams, 2);\n          \/\/\u53c2\u6570\u8d4b\u503c\n          AParams[0]:=1;\n          AParams[1]:=2;\n          Result:=AFunc.Invoke(AInst, AParams);\n        end\n      finally\n        FreeAndNil(ATypeLib);\n      end;<\/code><\/pre>\n\n\n\n<p>\u603b\u7ed3\u4e0b\u9700\u8981\u505a\u7684\u5de5\u4f5c\uff1a<\/p>\n\n\n\n<p>\u2192 \u521b\u5efa TComTypeLib \u5b9e\u4f8b\u5e76\u52a0\u8f7d TLB \u6587\u4ef6<br>\u2192 \u67e5\u627e\u7c7b\u578b\u548c\u8981\u8c03\u7528\u7684\u65b9\u6cd5<br>\u2192 \u8bbe\u7f6e\u53c2\u6570<br>\u2192 \u8c03\u7528\u51fd\u6570\u7684 Invoke \u65b9\u6cd5<\/p>\n\n\n\n<p>\u76f8\u6bd4\u4e0a\u4e00\u79cd\u65b9\u6848\uff0c\u9700\u8981\u989d\u5916\u52a0\u8f7d TLB\uff0c\u4f46\u51fd\u6570\u53ef\u4ee5\u5bf9\u8c61\u53ef\u4ee5\u7f13\u5b58\u91cd\u590d\u8c03\u7528\uff0c\u907f\u514d\u91cd\u590d\u8c03\u7528\u65f6\u7684\u67e5\u8be2\u5f00\u9500\u3002<\/p>\n\n\n\n<p>\u5b9e\u9645\u4e0a\uff0c\u901a\u8fc7 TComTypeLib \u4f60\u53ef\u4ee5\u505a\u7684\u5de5\u4f5c\u9664\u4e86\u4e0a\u9762\u7684\u5de5\u4f5c\u5916\uff0c\u8fd8\u53ef\u4ee5\u679a\u4e3e\u51fa\u91cc\u9762\u7684\u51fd\u6570\u548c\u7c7b\u578b\u5217\u8868\uff0c\u4ee5\u53ca\u5404\u4e2a\u53c2\u6570\u7684\u7c7b\u578b\u3002\u66f4\u591a\u7684\u7528\u6cd5\uff0c\u8bf7\u81ea\u5df1\u7814\u7a76\u4ee3\u7801\u3002comlib \u5355\u5143\u5728 QDAC \u5b98\u65b9\u7fa4\u4e2d\u53ef\u4ee5\u4e0b\u8f7d\u3002<\/p>\n\n\n\n<p>\u6700\u540e\uff0c\u795d\u5927\u5bb6\u65b0\u5e74\u5de5\u4f5c\u987a\u5229\uff0c\u4e8b\u4e1a\u8fdb\u6b65\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9996\u5148\u611f\u8c22 2019 \u5e74\u8bb8\u591a\u7684\u670b\u53cb\u5bf9 QD [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","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":[776,777],"class_list":["post-5170","post","type-post","status-publish","format-standard","hentry","category-delphi","tag-net","tag-777"],"views":2897,"_links":{"self":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/5170","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=5170"}],"version-history":[{"count":2,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/5170\/revisions"}],"predecessor-version":[{"id":5172,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/5170\/revisions\/5172"}],"wp:attachment":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}