Delphi - 調用SuperDll 持續更新

調用SuperDll函數

接上一篇Delphi建立Superdll,將生成的SuperDll.dll文件複製到本工程路徑下,建立以下代碼進行Superdll各個接口的測試。測試

建立uSuperDll.pas單元,用來對接DLL,具體看以下代碼;url

建立Delphi WinFrm窗體,引用uSuperDll.pas單元,窗體界面以下:spa

WinFrm uMain.pas單元代碼:code

  1 unit uMain;
  2 
  3 interface
  4 
  5 uses
  6   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7   Dialogs, RzButton, StdCtrls, RzEdit, cxStyles, cxCustomData, cxGraphics,
  8   cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel,
  9   cxClasses, cxControls, cxGridCustomView, cxGridCustomTableView,
 10   cxGridTableView, cxGridDBTableView, cxGrid, RzTabs;
 11 
 12 type
 13   TFrmMain = class(TForm)
 14     mmDsp: TRzMemo;
 15     btnSuperDll_Init: TRzBitBtn;
 16     btnSuperDll_Ftp_Put: TRzBitBtn;
 17     btnSuperDll_Ftp_Get: TRzBitBtn;
 18     btnSuperDll_EMail_Send: TRzBitBtn;
 19     pcMain: TRzPageControl;
 20     tsOne: TRzTabSheet;
 21     cxGridOneDBTableView1: TcxGridDBTableView;
 22     cxGridOneLevel1: TcxGridLevel;
 23     cxGridOne: TcxGrid;
 24     btnSaveToExcel: TRzBitBtn;
 25     cxGridOneDBTableView1Column1: TcxGridDBColumn;
 26     cxGridOneDBTableView1Column2: TcxGridDBColumn;
 27     cxGridOneDBTableView1Column3: TcxGridDBColumn;
 28     cxGridOneDBTableView1Column4: TcxGridDBColumn;
 29     cxGridOneDBTableView1Column5: TcxGridDBColumn;
 30     cxGridOneDBTableView1Column6: TcxGridDBColumn;
 31     cxGridOneDBTableView1Column7: TcxGridDBColumn;
 32     cxGridOneDBTableView1Column8: TcxGridDBColumn;
 33     cxGridOneDBTableView1Column9: TcxGridDBColumn;
 34     cxGridOneDBTableView1Column10: TcxGridDBColumn;
 35     procedure btnSuperDll_InitClick(Sender: TObject);
 36     procedure MsgDsp(sStr: string);
 37     procedure btnSuperDll_Ftp_PutClick(Sender: TObject);
 38     procedure btnSuperDll_Ftp_GetClick(Sender: TObject);
 39     procedure btnSuperDll_EMail_SendClick(Sender: TObject);
 40     procedure btnSaveToExcelClick(Sender: TObject);
 41   private
 42     { Private declarations }
 43   public
 44     { Public declarations }
 45   end;
 46 
 47 var
 48   FrmMain: TFrmMain;
 49 
 50 implementation
 51 uses
 52   uSuperDll;
 53 {$R *.dfm}
 54 
 55 procedure TFrmMain.MsgDsp(sStr: string);
 56 begin
 57   mmDsp.Lines.Add('[' + sStr + ']-[' + FormatDateTime('YYYY-MM-DD hh:mm:ss zzz', Now()) + ']');
 58 end;
 59 
 60 procedure TFrmMain.btnSuperDll_InitClick(Sender: TObject);
 61 var
 62   bResult: Boolean;
 63 begin
 64   MsgDsp('系統初始化,請稍等!');
 65   bResult := False;
 66   try
 67     bResult := SuperDll_Init;
 68     if bResult then
 69     begin
 70       btnSuperDll_Ftp_Put.Enabled := True;
 71       btnSuperDll_Ftp_Get.Enabled := True;
 72       btnSuperDll_EMail_Send.Enabled := True;
 73       btnSaveToExcel.Enabled := True;
 74       MsgDsp('初始化成功!');
 75     end
 76     else
 77     begin
 78       btnSuperDll_Ftp_Put.Enabled := False;
 79       btnSuperDll_Ftp_Get.Enabled := False;
 80       btnSuperDll_EMail_Send.Enabled := False;
 81       btnSaveToExcel.Enabled := False;
 82       MsgDsp('初始化失敗!');
 83     end;
 84   except
 85     on E: Exception do
 86     begin
 87       MsgDsp('讀取動態連接庫失敗,錯誤信息[' + E.Message + '],請知悉!');
 88     end;
 89   end;
 90 end;
 91 
 92 procedure TFrmMain.btnSuperDll_Ftp_PutClick(Sender: TObject);
 93 var
 94   sType, sUsername, sPassword, sHost, sDir, sSouFilePath: string;
 95   bResult: Boolean;
 96 begin
 97   bResult := False;
 98   MsgDsp('參數初始化,請稍等!');
 99   sType := 'Put';
100   sUsername := 'xxx';
101   sPassword := 'xxx';
102   sHost := 'xxx';
103   sDir := '/xxx/';
104   sSouFilePath := 'C:\Tmp\Test.txt';
105   MsgDsp('參數初始化完成!');
106   try
107     MsgDsp('文件上傳中,請稍等!');
108     bResult := SuperDll_Ftp_PutOrGet(sType, sUsername, sPassword, sHost, sDir, sSouFilePath, sSouFilePath);
109     if bResult then
110     begin
111       MsgDsp('上傳成功!');
112     end
113     else
114     begin
115       MsgDsp('上傳失敗!');
116     end;
117   except
118     on E: Exception do
119     begin
120       MsgDsp('上傳失敗,讀取動態連接庫失敗,錯誤信息[' + E.Message + '],請知悉!');
121     end;
122   end;
123 end;
124 
125 procedure TFrmMain.btnSuperDll_Ftp_GetClick(Sender: TObject);
126 var
127   sType, sUsername, sPassword, sHost, sDir, sDesFilePath: string;
128   bResult: Boolean;
129 begin
130   bResult := False;
131   MsgDsp('參數初始化,請稍等!');
132   sType := 'Get';
133   sUsername := 'xxx';
134   sPassword := 'xxx';
135   sHost := 'xxx';
136   sDir := '/xxx/';
137   sDesFilePath := 'C:\Tmp\1.csv';
138   MsgDsp('參數初始化完成!');
139   try
140     MsgDsp('文件下載中,請稍等!');
141     bResult := SuperDll_Ftp_PutOrGet(sType, sUsername, sPassword, sHost, sDir, sDesFilePath, sDesFilePath);
142     if bResult then
143     begin
144       MsgDsp('下載成功!');
145     end
146     else
147     begin
148       MsgDsp('下載失敗!');
149     end;
150   except
151     on E: Exception do
152     begin
153       MsgDsp('下載失敗,讀取動態連接庫失敗,錯誤信息[' + E.Message + '],請知悉!');
154     end;
155   end;
156 end;
157 
158 procedure TFrmMain.btnSuperDll_EMail_SendClick(Sender: TObject);
159 var
160   vSubject, vFrom, vRecipients, vCCList, vBccList, vBody, vAttachment, vUsername, vPassword, vHost: string;
161   bResult: Boolean;
162 begin
163   bResult := False;
164   MsgDsp('參數初始化,請稍等!');
165   vBody := '-------------------------------------SYSTEM TEST----------------------------------' + #13#10 +
166     '----------------------------------------------------------------------------------' + #13#10 +
167     'It is Function - [SuperDll_EMail_Send] Test, please do not reply deractly, thanks.';
168   vSubject := 'SuperDll_EMail_Send Test';
169   vFrom := 'xxx';
170   vRecipients := 'xxx';
171   vCCList := 'xxx';
172   vBccList := 'xxx';
173   vAttachment := 'C:\Tmp\1.csv';
174   vUsername := 'xxx';
175   vPassword := 'xxx';
176   vHost := 'xxx';
177   MsgDsp('參數初始化完成!');
178   try
179     MsgDsp('郵件發送中,請稍等!');
180     bResult := SuperDll_EMail_Send(vSubject, vFrom, vRecipients, vCCList, vBccList, vBody, vAttachment, vUsername, vPassword, vHost);
181     if bResult then
182     begin
183       MsgDsp('郵件發送成功!');
184     end
185     else
186     begin
187       MsgDsp('郵件發送失敗!');
188     end;
189   except
190     on E: Exception do
191     begin
192       MsgDsp('郵件發送失敗,調用動態連接庫失敗,錯誤信息[' + E.Message + '],請知悉!');
193     end;
194   end;
195 end;
196 
197 procedure TFrmMain.btnSaveToExcelClick(Sender: TObject);
198 var
199   bResult: Boolean;
200   sFullPathName: string;
201   od: TOpenDialog;
202 begin
203   try
204     od := TOpenDialog.Create(nil);
205     MsgDsp('正在嘗試導出Excel,請稍後!');
206     od.Title := '選中您要保存文件的位置';
207     od.Filter := '*.xls';
208     od.Execute;
209     sFullPathName := od.FileName;
210     if Pos('.xls', sFullPathName) <= 0 then
211     begin
212       sFullPathName := sFullPathName + '.xls';
213     end;
214     bResult := SaveCxGridToExcel(cxGridOne, sFullPathName);
215     if bResult then
216     begin
217       MsgDsp('導出Excel成功!');
218     end
219     else
220     begin
221       MsgDsp('導出Excel失敗,請確認!');
222     end;
223     od.Free;
224   except
225     on E: Exception do
226     begin
227       od.Free;
228       MsgDsp('導出Excel失敗,調用動態連接庫失敗,錯誤信息[' + E.Message + '],請知悉!');
229       Exit;
230     end;
231   end;
232 
233   try
234     od := TOpenDialog.Create(nil);
235     MsgDsp('正在嘗試導出CSV,請稍後!');
236     od.Title := '選中您要保存文件的位置';
237     od.Filter := '*.csv';
238     od.Execute;
239     sFullPathName := od.FileName;
240     if Pos('.xls', sFullPathName) <= 0 then
241     begin
242       sFullPathName := sFullPathName + '.xls';
243     end;
244     bResult := SaveCxGridToCSV(cxGridOne, sFullPathName);
245     if bResult then
246     begin
247       MsgDsp('導出CSV成功!');
248     end
249     else
250     begin
251       MsgDsp('導出CSV失敗,請確認!');
252     end;
253     od.Free;
254   except
255     on E: Exception do
256     begin
257       od.Free;
258       MsgDsp('導出CSV失敗,調用動態連接庫失敗,錯誤信息[' + E.Message + '],請知悉!');
259       Exit;
260     end;
261   end;
262 
263 end;
264 
265 end.

uSuperDll.pas代碼以下:orm

 1 unit uSuperDll;
 2 
 3 interface
 4 
 5 uses
 6   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 7   Dialogs, StdCtrls, IniFiles, StrUtils, cxGrid;
 8   
 9 function SuperDll_Init: Boolean; stdcall; //1: 初始化函數
10 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文件上傳下載
11 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; //3: 郵件發送
12 function SaveCxGridToExcel(vCxGrid: TcxGrid; var vFullPathName: string): Boolean; stdcall;
13 function SaveCxGridToCSV(vCxGrid: TcxGrid; var vFullPathName: string): Boolean; stdcall;
14 
15 implementation
16 function SuperDll_Init; external 'SuperDll.dll';
17 function SuperDll_Ftp_PutOrGet; external 'SuperDll.dll';
18 function SuperDll_EMail_Send; external 'SuperDll.dll';
19 function SaveCxGridToExcel; external 'SuperDll.dll';
20 function SaveCxGridToCSV; external 'SuperDll.dll';
21 end.

 

  做者:Jeremy.Wu
  出處:https://www.cnblogs.com/jeremywucnblog/
  本文版權歸做者和博客園共有,歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接,不然保留追究法律責任的權利。 blog

相關文章
相關標籤/搜索