{"id":5152,"date":"2019-11-26T21:47:35","date_gmt":"2019-11-26T13:47:35","guid":{"rendered":"http:\/\/blog.qdac.cc\/?p=5152"},"modified":"2019-11-26T21:47:35","modified_gmt":"2019-11-26T13:47:35","slug":"%e6%95%99%e7%a8%8b%e5%9c%a8-delphi-%e4%b8%ad%e8%b0%83%e7%94%a8-net-%e7%9a%84-dll","status":"publish","type":"post","link":"https:\/\/blog.qdac.cc\/?p=5152","title":{"rendered":"[\u6559\u7a0b]\u5728 Delphi \u4e2d\u8c03\u7528 .Net \u7684 DLL"},"content":{"rendered":"\n<p>\u9996\u5148\uff0c\u660e\u786e\u4e00\u70b9\uff1a\u672c\u6587\u63d0\u4f9b\u7684\u65b9\u6cd5\uff0c\u8981\u6c42 .NET \u7684 DLL \u5fc5\u9700\u7f16\u8bd1\u4e3a\u652f\u6301 COM \u63a5\u53e3\u7684\u6a21\u5f0f\uff0c\u8fd9\u6837\u5b50\uff0c\u5b83\u5c31\u53ef\u4ee5\u8ba9\u5176\u5b83\u8bed\u8a00\u901a\u8fc7 IDispatch \u63a5\u53e3\u8fdb\u884c\u8c03\u7528\u3002\u5177\u4f53\u7684\u6559\u7a0b\uff0c\u8bf7\u81ea\u884c\u767e\u5ea6\u3002\u4e0b\u9762\u662f\u7f51\u4e0a\u641c\u5230\u7684\u4e00\u4e2a\u622a\u56fe\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"719\" height=\"676\" src=\"https:\/\/blog.qdac.cc\/wp-content\/uploads\/2019\/11\/com_net_dll.png\" alt=\"\" class=\"wp-image-5153\" srcset=\"https:\/\/blog.qdac.cc\/wp-content\/uploads\/2019\/11\/com_net_dll.png 719w, https:\/\/blog.qdac.cc\/wp-content\/uploads\/2019\/11\/com_net_dll-300x282.png 300w\" sizes=\"auto, (max-width: 719px) 100vw, 719px\" \/><\/figure>\n\n\n\n<p>\u751f\u6210\u540e\u6709 TLB \u548c DLL \u4e24\u79cd\u7c7b\u578b\u7684\u6587\u4ef6\uff0cTLB \u5b58\u8d2e\u4e86 DLL \u4e2d\u76f8\u5173\u7684\u63a5\u53e3\u7684\u5b9a\u4e49\u3002\u63a5\u4e0b\u6765\uff0c\u5982\u679c\u6211\u4eec\u8981\u5728 Delphi \u4f7f\u7528\u8fd9\u4e2a C# \u7684\u52a8\u6001\u94fe\u63a5\u5e93\uff0c\u90a3\u4e48\u9996\u5148\u6211\u4eec\u9700\u8981\u8c03\u7528 .NET \u7684 regasm \u6ce8\u518c\u8fd9\u4e2a DLL \u5230\u7cfb\u7edf\u4e2d\u3002regasm \u8fd9\u4e2a\u7a0b\u5e8f\u7684\u4f4d\u7f6e\u53d6\u51b3\u4e8e\u5177\u4f53\u7684 .Net \u8fd0\u884c\u65f6\u7248\u672c\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>2.x\/3.x : %WinDir%\\Microsoft.NET\\Framework\\v2.0.50727<\/li><li>4.x : %WinDir%\\Microsoft.NET\\Framework\\v4.0.30319<\/li><\/ul>\n\n\n\n<p>\u8fd9\u4e2a\u6839\u636e\u521b\u5efa DLL \u65f6\u7684 .NET \u7684\u76ee\u6807\u73af\u5883\u6765\u9009\u62e9\u5c31\u597d\u3002\u5047\u8bbe\u6211\u4eec\u4f7f\u7528 4.0 \u7684 regasm\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>%WinDir%\\Microsoft.NET\\Framework\\v4.0.30319\\regasm d:\\temp\\dll_dotnet.dll<\/code><\/pre>\n\n\n\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u8981\u5728 Delphi \u4e2d\u8c03\u7528\u8fd9\u4e2a DLL \u7684\u51fd\u6570\u6765\u5b8c\u6210\u76f8\u5e94\u7684\u64cd\u4f5c\u3002\u8fd9\u91cc\u6709\u597d\u51e0\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u7528\u4f60\u559c\u6b22\u7684\u65b9\u5f0f\u6765\u5b8c\u6210\uff1a<\/p>\n\n\n\n<p>1\u3001\u901a\u8fc7 OleVariant \u6765\u5904\u7406\u3002\u8fd9\u79cd\u5904\u7406\u65b9\u5f0f\u662f\u6700\u7b80\u5355\u7684\uff0c\u53c2\u8003\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>var\n  V:OleVariant;\nconst\n  ITestLib:TGuid='{EA2F140A-108F-47AE-BBD5-83EEE646CC0D}';\/\/\u6ce8\u610f\u8fd9\u4e2aID\u8981\u6b63\u786e\u6216\u8005\u7528\u90a3\u4e2a\u53c2\u6570\u683c\u5f0f\u4e3a xxx.yyy \u7684 CreateOleObject \u8fd4\u56de\u5b9e\u4f8b\uff0c\u6ce8\u610f\u8fd4\u56de\u7684\u5b9e\u4f8b\u5f3a\u5236\u8f6c\u6362\u4e3a IDispatch \u63a5\u53e3\uff0c\u5426\u5219\u4f1a\u56e0\u4e3a OleVariant \u7684\u7c7b\u578b\u4e0d\u5bf9\uff0c\u800c\u65e0\u6cd5\u8c03\u7528\u3002\nbegin\n  V:=IDispatch(CreateComObject(ITestLib));\n  ShowMessage(VarToStr(V.Add(1,2)));\nend;<\/code><\/pre>\n\n\n\n<p>2\u3001\u9009\u62e9 Components->Import Component\uff0c\u7136\u540e\u9009\u62e9 Import .net assembly\uff0c\u7136\u540e\u6309\u7167\u5411\u5bfc\u6dfb\u52a0\u8981\u5bfc\u5165\u7684 dll\uff0c\u6700\u540e\u5c31\u4f1a\u751f\u6210\u5bf9\u5e94\u7684\u5bfc\u5165\u5355\u5143\uff0c\u7136\u540e\u5c31\u7b80\u5355\u4e86\uff0c\u76f4\u63a5\u6309\u7167\u666e\u901a\u7684 Com \u521b\u5efa\u5bf9\u5e94\u7684 TOleServer \u7c7b\u578b\u7684\u5b9e\u4f8b\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n\n\n\n<p>3\u3001\u5229\u7528 LoadTypeLib \u6765\u52a0\u8f7d\u83b7\u53d6 ITypeInfo \u4fe1\u606f\uff0c\u7136\u540e\u518d\u8c03\u7528\u5176 CreateInstance \u63a5\u53e3\u521b\u5efa\u5b9e\u4f8b\uff0c\u6700\u7ec8\u518d\u4f7f\u7528 IDispatch \u7684 Invoke \u63a5\u53e3\u5b9e\u73b0\u8c03\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9996\u5148\uff0c\u660e\u786e\u4e00\u70b9\uff1a\u672c\u6587\u63d0\u4f9b\u7684\u65b9\u6cd5\uff0c\u8981\u6c42 . [&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":[],"class_list":["post-5152","post","type-post","status-publish","format-standard","hentry","category-delphi"],"views":4807,"_links":{"self":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/5152","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=5152"}],"version-history":[{"count":2,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/5152\/revisions"}],"predecessor-version":[{"id":5155,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/5152\/revisions\/5155"}],"wp:attachment":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}