Delphi SuperDllapp
做爲一名5年的Delpher,一直認爲Delphi是桌面應用的王者,我相信其餘的Delpher也這麼認爲。this
可是,慢慢的我發現普通方式的Delphi開發會形成代碼的嚴重臃腫,特別是MDI類大型項目、多人同時開發的狀況下。spa
舉個例子,一個Delphi經常使用的業務邏輯,數據導出到Excel,徹底能夠寫成一個公用的模塊放置在業務單元,子窗體用到時直接調用便可,可是通常狀況下,事情並不止想象的那麼簡單,維護人員的思想真的一言難盡。日誌
後來,我有了將Delphi中經常使用的業務邏輯功能封裝成DLL的想法,全部的業務邏輯只能在DLL中實現,系統中不容許直接寫業務邏輯,只能調用DLL。code
這麼作的好處是,相同的業務功能不會被重複開發,大大減小了代碼的臃腫,同時,業務邏輯開發人員和前臺開發人員獨立開來,提升了開發效率。orm
這裏就是SuperDLL的由來,後續將持續更新。blog
請看以下代碼:ip
更新日誌:ci
//初始化開發
//郵件發送
//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.