Home > Back-end >  Written using delphi10.2.3 take the form of DLL, in other programs call, the second call will result
Written using delphi10.2.3 take the form of DLL, in other programs call, the second call will result

Time:11-09

Such as: I use delphi10.2.3 wrote a DLL, the DLL, brought a form, and deduced two culvert number for external calls,
One of them is initialized,
One is the call function,
Now found in any other language development program, if it is a simple demo, such as: the window of the application is a simple, call the number two culvert on the form, 90% is no problem, but the call number is much, occasionally appear the situation of the program crashes,
But if it is in a fully functional application calls the DLL is 100%, the second call, the DLL can cause collapse of the main program,
May I express a little mess, I directly with the code below, exclude pleaded:
DLL, I use the form, also use the multithreading, because on the DLL requires interface display effect,

DLL code is as follows:
 
The library leshua.

USES the
System. The SysUtils,
System. Classes,
Unit1 in 'Unit1. Pas,
Unit2 in 'Unit2. Pas' {Form2};

{$R *. Res}
Exports
InitLeshua,
LeshuaPay;

The begin
End.


Unit 1 code is as follows:
 
The unit Unit1;

Interface

USES the
Winapi. Windows System. SysUtils, System. Classes, System. The Hash, Vcl.
Controls,The 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 the Execute; Override. {performed}
Procedure Run; {statement more than a process, write functional code here again to call the Execute}
end;

The function initLeshua (as_shh: PAnsiChar; As_t0: PAnsiChar; Handle: THandle) : Integer; Stdcall;

The function LeshuaPay (as_djno: PAnsiChar; As_amount: PAnsiChar; As_paycode: PAnsiChar; As_error: PAnsiChar) : Integer; Stdcall;

The function GetDllPath: string;

Procedure writeLog (logstr: string);

Var
Form2: TForm2;
Gs_error: string;//error content
Gi_return: Integer;//return the result & lt; 0 for failure, & gt; Success as a=0
Lstr_thread_data: str_thread_date;
AppHandle: THandle;
OldHandle: THandle;
Gs_url: string;//request address
Gs_key: string;//KEY
Gs_shh: string; No.//merchants

Implementation

Var
MyThread: TMyThread; {statement a thread class object}

Procedure TMyThread. Execute;
The begin
{Place thread code here}
WriteLog (' start executing thread);
FreeOnTerminate:=True; {and ran out of the thread automatically annotation}
WriteLog (' ready to run thread number of culvert run ');
Run;
end;

Procedure TMyThread. Run;
Var
I: integer;
Ls_showtext: string;
Ls_shh: string;
Myinifile: TIniFile;//configuration file
The begin
WriteLog (' thread number of culvert run start);

Myinifile:=Tinifile. Create (ExtractFilePath (GetDllPath ()) + 'leshuapay. Ini');
Ls_shh:=myinifile Readstring (' system ', 'business_no', ');
Myinifile. Destroy;
WriteLog (='+' thread number of culvert run for SHH ls_shh);

For I:=1 to 10 do
The begin
WriteLog (' thread culvert for the run: the first '+ IntToStr (I) +' cycles');
The Lock; Form2. Canvas.
Form2. Panel1. Caption:='first' + IntToStr (I) + 'loops;
Form2. Canvas. Unlock;
WriteLog (' thread number of culvert run: delay 1 seconds');
Sleep (1000);
end;
WriteLog (' thread number of culvert run: ready to close the Form2);
Form2. Close;
WriteLog (' end of thread number of culvert run: ');
end;

The function initLeshua (as_shh: PAnsiChar; As_t0: PAnsiChar; Handle: THandle) : Integer; Stdcall;
Var
Ls_shh: PAnsiChar;
Myinifile: TIniFile;//configuration file
The 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 (' initialization: merchant number=[' + as_shh +], request address=[' + gs_url + '] ');
Result:=1;
end;

The function LeshuaPay (as_djno: PAnsiChar; As_amount: PAnsiChar; As_paycode: PAnsiChar; As_error: PAnsiChar) : Integer; Stdcall;
Var
Ls_error: string;
The begin
WriteLog (' begin paying culvert number);
MyThread:=TMyThread. Create (False); {instantiation thread class, run immediately when False and True can add MyThread. When Resume to start the}
WriteLog (' open the thread end ');
OldHandle:=Application. Handle;
Application. Handle:=appHandle;
WriteLog (' form instance China);
Application CreateForm (TForm2, Form2);
Try
WriteLog (' display form);
Form2. ShowModal;
The finally
WriteLog (' ready to release resources');
Form2. Free;
WriteLog (' form released all ');
Application. Handle:=oldHandle;
WriteLog (' handle reduction);
end;

Ls_error:='WXZF | 0000290005619263';
WriteLog (' copy to return information);
StrPCopy (as_error, AnsiString (ls_error));
WriteLog (' pay culvert for return);
Result:=1;
end;

The function GetDllPath: string;
Var
ModuleName: string;
The begin
SetLength (ModuleName, 255);
//Dll's own path
GetModuleFileName (HInstance, PChar (ModuleName), Length (ModuleName));
Result:=PChar (ModuleName);
end;

Procedure writeLog (logstr: string);
Var
Filev: TextFile;
Path, name, logfile: string;
The begin
//write log
Path:=ExtractFilePath (GetDllPath ()) + 'log \';
If not FileExists ((path)) then
ForceDirectories (path);

Name:='LESHUA_ + FormatDateTime (' yyyymmdd', now);//date
The logfile: path + name +='. TXT ';//log access path
Name:=logfile;

If FileExists (logfile) then
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related