1,183
社区成员
发帖
与我相关
我的任务
分享
library leshua;
uses
System.SysUtils,
System.Classes,
Unit1 in 'Unit1.pas',
Unit2 in 'Unit2.pas' {Form2};
{$R *.res}
exports
initLeshua,
LeshuaPay;
begin
end.
unit Unit1;
interface
uses
Winapi.Windows, System.SysUtils, System.Classes, System.Hash, Vcl.Controls,
Vcl.Forms, Vcl.ExtCtrls, IdHTTP, IdSSLOpenSSL, IdGlobal, XMLDoc, XMLIntf,
StrUtils, messages, unit2, IniFiles;
type
str_thread_date = record
ls_djno: string;
ls_amount: string;
ls_paycode: string;
end;
type
TMyThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override; {执行}
procedure Run; {声明多一个过程,把功能代码写在这里再给Execute调用}
end;
function initLeshua(as_shh: PAnsiChar; as_t0: PAnsiChar; handle: THandle): Integer; stdcall;
function LeshuaPay(as_djno: PAnsiChar; as_amount: PAnsiChar; as_paycode: PAnsiChar; as_error: PAnsiChar): Integer; stdcall;
function GetDllPath: string;
procedure writeLog(logstr: string);
var
Form2: TForm2;
gs_error: string; //错误内容
gi_return: Integer; //返回结果 < 0为失败, >= 0为成功
lstr_thread_data: str_thread_date;
appHandle: THandle;
oldHandle: THandle;
gs_url: string; //请求的地址
gs_key: string; //KEY
gs_shh: string; //商户号
implementation
var
MyThread: TMyThread; {声明一个线程类对象}
procedure TMyThread.Execute;
begin
{ Place thread code here }
writeLog('线程开始执行');
FreeOnTerminate := True; {加上这句线程用完了会自动注释}
writeLog('准备运行线程涵数run');
Run;
end;
procedure TMyThread.Run;
var
i: integer;
ls_showtext: string;
ls_shh: string;
myinifile: TIniFile; //配置文件
begin
writeLog('线程涵数run开始');
myinifile := Tinifile.Create(ExtractFilePath(GetDllPath()) + 'leshuapay.ini');
ls_shh := myinifile.Readstring('system', 'business_no', '');
myinifile.Destroy;
writeLog('线程涵数run获取shh=' + ls_shh);
for i := 1 to 10 do
begin
writeLog('线程涵数run:第' + IntToStr(i) + '次循环');
Form2.Canvas.Lock;
Form2.Panel1.Caption := '第' + IntToStr(i) + '次循环';
Form2.Canvas.Unlock;
writeLog('线程涵数run:进行延时1秒');
Sleep(1000);
end;
writeLog('线程涵数run:准备关闭Form2');
Form2.Close;
writeLog('线程涵数run:结束');
end;
function initLeshua(as_shh: PAnsiChar; as_t0: PAnsiChar; handle: THandle): Integer; stdcall;
var
ls_shh: PAnsiChar;
myinifile: TIniFile; //配置文件
begin
if (appHandle = 0) then
appHandle := handle;
myinifile := Tinifile.Create(ExtractFilePath(GetDllPath()) + 'leshuapay.ini');
myinifile.writestring('system', 'business_no', as_shh);
myinifile.Destroy;
gs_key := 'ADB8145F4D8A820D30B20B1930';
gs_url := 'https://paygate.shuazf.com/cgi-bin/pay_gateway.cgi';
writeLog('初始化:商户号=[' + as_shh + '],请求地址=[' + gs_url + ']');
Result := 1;
end;
function LeshuaPay(as_djno: PAnsiChar; as_amount: PAnsiChar; as_paycode: PAnsiChar; as_error: PAnsiChar): Integer; stdcall;
var
ls_error: string;
begin
writeLog('开始支付涵数');
MyThread := TMyThread.Create(False); {实例化线程类,为False时立即运行,为True时可加MyThread.Resume用来启动}
writeLog('打开线程完毕');
oldHandle := Application.Handle;
Application.Handle := appHandle;
writeLog('实例华窗体');
Application.CreateForm(TForm2, Form2);
try
writeLog('显示窗体');
Form2.ShowModal;
finally
writeLog('准备释放资源');
Form2.Free;
writeLog('窗体释放完毕');
Application.Handle := oldHandle;
writeLog('句柄还原');
end;
ls_error := 'WXZF|0000290005619263';
writeLog('复制要返回的信息');
StrPCopy(as_error, AnsiString(ls_error));
writeLog('支付涵数返回');
Result := 1;
end;
function GetDllPath: string;
var
ModuleName: string;
begin
SetLength(ModuleName, 255);
//取得Dll自身路径
GetModuleFileName(HInstance, PChar(ModuleName), Length(ModuleName));
Result := PChar(ModuleName);
end;
procedure writeLog(logstr: string);
var
filev: TextFile;
path, name, logfile: string;
begin
//写日志
path := ExtractFilePath(GetDllPath()) + 'log\';
if not FileExists((path)) then
ForceDirectories(path);
name := 'LESHUA_' + FormatDateTime('yyyymmdd', now); //取得日期
logfile := path + name + '.txt'; //日志存取路径
name := logfile;
if FileExists(logfile) then
begin
AssignFile(filev, logfile);
append(filev);
writeln(filev, FormatDateTime('d', now) + '/' + FormatDateTime('hh:nn:ss.zzz', now) + ' ' + logstr);
end
else
begin
AssignFile(filev, logfile);
ReWrite(filev);
writeln(filev, FormatDateTime('d', now) + '/' + FormatDateTime('hh:nn:ss.zzz', now) + ' ' + logstr);
end;
CloseFile(filev);
end;
end.
unit Unit2;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls;
type
TForm2 = class(TForm)
Panel1: TPanel;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
end.
function LeshuaPay(as_djno: PAnsiChar; as_amount: PAnsiChar; as_paycode: PAnsiChar; as_error: PAnsiChar): Integer; stdcall;
var
ls_error: string;
begin
writeLog('开始支付涵数');
writeLog('处理窗口句柄Application.Handle=' + IntToStr(Application.Handle) + ' appHandle=' + IntToStr(appHandle));
oldHandle := Application.Handle;
Application.Handle := appHandle;
writeLog('开始实例化窗体createform');
Application.CreateForm(TForm2, Form2);
writeLog('准备打开线程');
MyThread := TMyThread.Create(False); {实例化线程类,为False时立即运行,为True时可加MyThread.Resume用来启动}
writeLog('打开线程完毕');
try
writeLog('显示窗体');
Form2.ShowModal;
finally
writeLog('准备释放资源');
Form2.Free;
writeLog('窗体释放完毕');
Application.Handle := oldHandle;
writeLog('句柄还原');
end;
ls_error := 'WXZF|0000290005619263';
writeLog('复制要返回的信息');
StrPCopy(as_error, AnsiString(ls_error));
writeLog('支付涵数返回====================================================');
Result := 1;
end;