欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Delphi - 创建SuperDll 持续更新

程序员文章站 2022-06-15 14:31:25
Delphi SuperDll 作为一名5年的Delpher,一直认为Delphi是桌面应用的王者,我相信其他的Delpher也这么认为。 但是,慢慢的我发现普通方式的Delphi开发会造成代码的严重臃肿,特别是MDI类大型项目、多人同时开发的情况下。 举个例子,一个Delphi常用的业务逻辑,数据 ......

delphi superdll

作为一名5年的delpher,一直认为delphi是桌面应用的王者,我相信其他的delpher也这么认为。

但是,慢慢的我发现普通方式的delphi开发会造成代码的严重臃肿,特别是mdi类大型项目、多人同时开发的情况下。

举个例子,一个delphi常用的业务逻辑,数据导出到excel,完全可以写成一个公用的模块放置在业务单元,子窗体用到时直接调用即可,但是一般情况下,事情并不止想象的那么简单,维护人员的思想真的一言难尽。

后来,我有了将delphi中常用的业务逻辑功能封装成dll的想法,所有的业务逻辑只能在dll中实现,系统中不允许直接写业务逻辑,只能调用dll。

这么做的好处是,相同的业务功能不会被重复开发,大大减少了代码的臃肿,同时,业务逻辑开发人员和前台开发人员独立开来,提高了开发效率。

这里就是superdll的由来,后续将持续更新。

请看如下代码:

更新日志:

//初始化

//邮件发送

//ftp上传、下载

//cxgrid数据导出

  1 library superdll;
  2 
  3 { important note about dll memory management: sharemem must be the
  4   first unit in your library's uses clause and your project's (select
  5   project-view source) uses clause if your dll exports any procedures or
  6   functions that pass strings as parameters or function results. this
  7   applies to all strings passed to and from your dll--even those that
  8   are nested in records and classes. sharemem is the interface unit to
  9   the borlndmm.dll shared memory manager, which must be deployed along
 10   with your dll. to avoid using borlndmm.dll, pass string information
 11   using pchar or shortstring parameters. }
 12 
 13 uses
 14   sysutils, classes, variants, graphics, controls, idbasecomponent, idcomponent, idftp,
 15   idftpcommon, idtcpconnection, idtcpclient, idmessage, idmessageclient, idsmtp, cxgrid,
 16   cxgridexportlink, comobj,
 17   cxcustomdata, cxgraphics,
 18   cxdata, cxdatastorage, cxedit, cxdbdata, cxgridlevel,
 19   cxclasses, cxcontrols, cxgridcustomview, cxgridcustomtableview,
 20   cxgridtableview, cxgriddbtableview;
 21 
 22 {$r *.res}
 23 
 24 function superdll_init: boolean; stdcall;
 25 begin
 26   result := true;
 27 end;
 28 
 29 function superdll_ftp_putorget(vtype: string; var vusername: string; var vpassword: string; var vhost: string; var vdir: string; var vdesfilepath: string; vsoufilepath: string): boolean; stdcall; //2: ftp文件上传下载
 30 var
 31   idftp: tidftp;
 32 begin
 33   idftp := tidftp.create(nil);
 34   result := false;
 35   try
 36     with idftp do
 37     begin
 38       if connected then disconnect;
 39       username := vusername;
 40       password := vpassword;
 41       host := vhost;
 42       port := 21;
 43       connect;
 44       changedir(vdir);
 45       transfertype := ftbinary;
 46       if vtype = 'put' then
 47       begin
 48         put(vsoufilepath, extractfilename(vsoufilepath));
 49       end
 50       else if vtype = 'get' then
 51       begin
 52         get(extractfilename(vdesfilepath), vdesfilepath, true);
 53       end;
 54     end;
 55   finally
 56     idftp.disconnect;
 57     idftp.free;
 58     result := true;
 59   end;
 60 end;
 61 
 62 function superdll_email_send(vsubject: string; var vfrom: string; var vrecipients: string; var vcclist: string; var vbcclist: string; var vbody: string; var vattachment: string; var vusername: string; var vpassword: string; var vhost: string): boolean; stdcall;
 63 var
 64   idsmtp: tidsmtp;
 65   idmessage: tidmessage;
 66 begin
 67   result := false;
 68   idsmtp := tidsmtp.create(nil);
 69   idmessage := tidmessage.create(nil);
 70   try
 71     with idmessage do
 72     begin
 73       clear;
 74       subject := vsubject;
 75       from.text := vfrom;
 76       recipients.emailaddresses := vrecipients;
 77       cclist.emailaddresses := vcclist;
 78       bcclist.emailaddresses := vbcclist;
 79       priority := tidmessagepriority(4);
 80       if trim(vattachment) <> '' then
 81       begin
 82         tidattachment.create(messageparts, trim(vattachment));
 83       end;
 84       vbody := vbody + #13#10;
 85       vbody := vbody + #13#10;
 86       vbody := vbody + #13#10;
 87       vbody := vbody + #13#10;
 88       vbody := vbody + 'it is auto mail system,please do not reply this mail directly,thank you!';
 89       body.add(vbody);
 90     end;
 91 
 92     with idsmtp do
 93     begin
 94       if connected then disconnect;
 95       authenticationtype := atlogin;
 96       port := 25;
 97       username := vusername;
 98       password := vpassword;
 99       host := vhost;
100       connect;
101     end;
102 
103     idsmtp.send(idmessage);
104     idsmtp.disconnect;
105 
106     result := true;
107   finally
108     idsmtp.free;
109     idmessage.free;
110   end;
111 end;
112 
113 function savecxgridtoexcel(vcxgrid: tcxgrid; var vfullpathname: string): boolean; stdcall;
114 begin
115   result := false;
116   vcxgrid := tcxgrid.create(nil);
117   exportgridtoexcel(vfullpathname, vcxgrid);
118   vcxgrid.free;
119   result := true;
120 end;
121 
122 function savecxgridtocsv(vcxgrid: tcxgrid; var vfullpathname: string): boolean; stdcall;
123 begin
124   result := false;
125 
126   exportgridtotext(vfullpathname + '.xls', vcxgrid, true, true, ',', '', '', 'csv');
127   result := true; ;
128 end;
129 
130 exports
131   superdll_init,
132   superdll_ftp_putorget,
133   superdll_email_send,
134   savecxgridtoexcel,
135   savecxgridtocsv;
136 
137 begin
138 end.