C#.NET制作DLL供DELPHI调用

因为工作需求,本来想用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.
复制代码

 

posted on   西点肥牛  阅读(1014)  评论(4编辑  收藏  举报

相关博文:
阅读排行:
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示