首页 > 编程语言 >C#.NET制作DLL供DELPHI调用

C#.NET制作DLL供DELPHI调用

时间:2023-01-12 07:33:11浏览次数:40  
标签:begin end C# DELPHI DLL procedure Integer TMyFunc IMyFunc

因为工作需求,本来想用C#做一个WebService,但是弄了两天没做成。于是想,反正都是我这一台电脑,做个DLL吧。

 1 namespace U8Service
 2 {
 3     public interface IMyFunc
 4     {
 5         int Jia(int a, int b);
 6         int Jian(int a, int b);
 7     }
 8     [ClassInterface(ClassInterfaceType.None)]
 9     public class MyFunc : IMyFunc
10     {
11         public int Jia(int a, int b)
12         {
13             return (a + b);
14         }
15 
16         public int Jian(int a, int b)
17         {
18             return (a - b);
19         }
20     }
21 
22 }

C#里代码就得这么写,如果不写IMyFunc的定义的话,生成的DLL里是空的。

然后在程序集信息里,要把这一项勾选。

 

另外在输出选项中,这一项也需要勾选上:

这一项如果不勾选,生成DLL后,有几个情况:

 

1、因为没有TLB文件,所以只能使用Import .NET Assembly直接加载DLL,DELPHI程序能编译通过,但是运行时会报一个错:

2、使用regasm u8service.dll 对DLL进行注册,会提示:

regasm u8service.dll /tlb:u8service.tlb 这种方式我也试过了,仍然不行。

所以我也不知道到底问题出在哪里。

如果在C#中勾选了“为COM互操作注册”一项的话,在DELPHI中Import a Type Library中就可以直接看到这一项:

 

这样生成的DELPHI文件是没问题的,可以正常运行。但是我在想,如果我要拿到别人的电脑上该如何注册呢?上面那些问题该怎么解决?

最后一个问题,在DELPHI中通过Import .NET Assembly加载DLL生成的pas文件中,比用上述Import a Type Library方式生成的pas文件多了很多内容,多生成了一个TMyFunc的类,而且这个类里增加了很多函数。

  1 // *********************************************************************//
  2 // OLE Server Proxy class declaration
  3 // Server Object    : TMyFunc
  4 // Help String      : 
  5 // Default Interface: IMyFunc
  6 // Def. Intf. DISP? : No
  7 // Event   Interface: 
  8 // TypeFlags        : (2) CanCreate
  9 // *********************************************************************//
 10   TMyFunc = class(TOleServer)
 11   private
 12     FIntf: IMyFunc;
 13     function GetDefaultInterface: IMyFunc;
 14   protected
 15     procedure InitServerData; override;
 16   public
 17     constructor Create(AOwner: TComponent); override;
 18     destructor  Destroy; override;
 19     procedure Connect; override;
 20     procedure ConnectTo(svrIntf: IMyFunc);
 21     procedure Disconnect; override;
 22     function Jia(a: Integer; b: Integer): Integer;
 23     function Jian(a: Integer; b: Integer): Integer;
 24     property DefaultInterface: IMyFunc read GetDefaultInterface;
 25   published
 26   end;
 27 
 28 procedure Register;
 29 
 30 resourcestring
 31   dtlServerPage = '(none)';
 32 
 33   dtlOcxPage = '(none)';
 34 
 35 implementation
 36 
 37 uses System.Win.ComObj;
 38 
 39 class function CoMyFunc.Create: IMyFunc;
 40 begin
 41   Result := CreateComObject(CLASS_MyFunc) as IMyFunc;
 42 end;
 43 
 44 class function CoMyFunc.CreateRemote(const MachineName: string): IMyFunc;
 45 begin
 46   Result := CreateRemoteComObject(MachineName, CLASS_MyFunc) as IMyFunc;
 47 end;
 48 
 49 procedure TMyFunc.InitServerData;
 50 const
 51   CServerData: TServerData = (
 52     ClassID:   '{7F9DF1C0-61B0-3579-AD03-81BB0F9E7972}';
 53     IntfIID:   '{76EED873-51B7-3DFC-87BE-A88C77FD3E44}';
 54     EventIID:  '';
 55     LicenseKey: nil;
 56     Version: 500);
 57 begin
 58   ServerData := @CServerData;
 59 end;
 60 
 61 procedure TMyFunc.Connect;
 62 var
 63   punk: IUnknown;
 64 begin
 65   if FIntf = nil then
 66   begin
 67     punk := GetServer;
 68     Fintf:= punk as IMyFunc;
 69   end;
 70 end;
 71 
 72 procedure TMyFunc.ConnectTo(svrIntf: IMyFunc);
 73 begin
 74   Disconnect;
 75   FIntf := svrIntf;
 76 end;
 77 
 78 procedure TMyFunc.DisConnect;
 79 begin
 80   if Fintf <> nil then
 81   begin
 82     FIntf := nil;
 83   end;
 84 end;
 85 
 86 function TMyFunc.GetDefaultInterface: IMyFunc;
 87 begin
 88   if FIntf = nil then
 89     Connect;
 90   Assert(FIntf <> nil, 'DefaultInterface is NULL. Component is not connected to Server. You must call "Connect" or "ConnectTo" before this operation');
 91   Result := FIntf;
 92 end;
 93 
 94 constructor TMyFunc.Create(AOwner: TComponent);
 95 begin
 96   inherited Create(AOwner);
 97 end;
 98 
 99 destructor TMyFunc.Destroy;
100 begin
101   inherited Destroy;
102 end;
103 
104 function TMyFunc.Jia(a: Integer; b: Integer): Integer;
105 begin
106   Result := DefaultInterface.Jia(a, b);
107 end;
108 
109 function TMyFunc.Jian(a: Integer; b: Integer): Integer;
110 begin
111   Result := DefaultInterface.Jian(a, b);
112 end;
113 
114 procedure Register;
115 begin
116   RegisterComponents(dtlServerPage, [TMyFunc]);
117 end;

因为对C#不太熟悉,所以出现了这么多问题,谁知道怎么做的麻烦告诉我。

正常的DELPHI代码:

 

 1 unit Unit1;
 2 
 3 interface
 4 
 5 uses
 6   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
 7   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons;
 8 
 9 type
10   TForm1 = class(TForm)
11     btn1: TBitBtn;
12     mmo1: TMemo;
13     procedure btn1Click(Sender: TObject);
14   private
15     { Private declarations }
16   public
17     { Public declarations }
18   end;
19 
20 var
21   Form1: TForm1;
22 
23 implementation
24 
25 uses
26   U8Service_TLB;
27 
28 {$R *.dfm}
29 
30 procedure TForm1.btn1Click(Sender: TObject);
31 var
32   C1: IMyFunc;
33 begin
34   C1 := CoMyFunc.Create;
35   mmo1.Lines.Add(IntToStr(C1.Jia(5, 7)));
36 end;
37 
38 end.

 

标签:begin,end,C#,DELPHI,DLL,procedure,Integer,TMyFunc,IMyFunc
From: https://www.cnblogs.com/kenlewis/p/17045352.html

相关文章

  • 我曾经用“UC震惊部”震碎了很多人的三观
    Hi,欢迎大家在有空的时候做客【江涛学编程】,这里是2023年的第9篇原创文章,今天写的这篇是当事人对昨天上热搜的统一回复。我没有曾经跨过山河大海,我也没有曾经穿越人山人海,......
  • [LeetCode] 1807. Evaluate the Bracket Pairs of a String
    Youaregivenastring s thatcontainssomebracketpairs,witheachpaircontaininga non-empty key.Forexample,inthestring "(name)is(age)yearsold",......
  • Codeforces Edu Round 106 Div2
    解题A.DominoonWindowsill这个题给一个2xn的方格,一个行有k1个白块,第二行有k2个白块,那么现在有w个2x1的白块和b个2x1黑块,白对白,黑对黑,问能不能全放下这个就是判断下白......
  • 【table master mmocr】Windows下模型训练的配置
    processed_data就是mmocr_pubtabnet_recognition,注意统一命名由图可以看出,那个processed_data就是mmocr_pubtabnet_recognition,而且后面后缀_0927之类的都是日期,可能是......
  • CS:APP--Chapter05 : optimizing program performance (part 2)
    CS:APP--Chapter05:optimizingprogramperformance(part2)标签(空格分隔):CS:APP目录CS:APP--Chapter05:optimizingprogramperformance(part2)8.loopuprollin......
  • Leaflet.js | 官方控件Control
    1、比例控件一个简单的比例控件,以公制(m/km)和英制(mi/ft)系统显示当前屏幕中心的比例,可扩展。使用示例L.control.scale().addTo(map);创造构造描述L.contr......
  • strapi系列-如何创建一个定时任务-Cron Jobs
    Cron是什么?Cron有多种用途。CronJobs用于安排服务器上的任务运行。它们最常用于自动化系统管理或维护。然而,它们也与Web应用程序的构建相关。Web应用程序可能需......
  • C#、TS和Dart对比2:变量和作用域
    一、变量的声明、赋值和类型推断1、C#//C#是完全面向对象语言,所有代码都必须在类中//Program类的Main方法,是程序的入口函数publicclassProgram{staticvoidM......
  • Luogu P5465 [PKUSC2018] 星际穿越
    观察可以发现一个结论,可以视作每个点\(i\)可以一步到达\(l_i\simn\)的每一个点。发现对于\(a<b<x\),\(dist(a,x)\gedist(b,x)\)第一步是相当特殊的,因为第一步......
  • 3. Longest Substring Without Repeating Characters [Medium]
    3.LongestSubstringWithoutRepeatingCharactersGivenastrings,findthelengthofthelongestsubstringwithoutrepeatingcharacters.Constraints:0<=s......