Requirements : Since we received such a project recently, Party A and others asked us to add the functions we developed to a PB developed application that they already have. The only purpose is not to have two exe applications.
Solution : Package Delphi's application in the form of a dll and then call it in the PB.
design :
1. PB part
1. Create a new non-visual class n_cst_demo, and we put all calls from the same module into this class.
2. Define API calls. Definition in Declare---Local External Functions
//handle: is the handle of the current window; param: other required parameters
SUBROUTINE show_demo(uLong handle , String param) LIBRARY "PRoject1.dll"
3. Create a new form w_main.
4. Create a new menu m_dll.
5. Create a menu item called test dll. Write in the Clicked event:
n_cst_demo n_pda n_pda = Create n_cst_demo n_pda.Show_Demo( Handle(ParentWindow) ,"ABCD1234") Destroy n_pda |
In this way, our PB part is completed.
2. Delphi part
1. Create a new dll project.
2. Create a new Form1. save.3. Write code in the project:
library Project1; uses SysUtils, Forms, Windows, Classes, Controls, Dialogs, Unit1 in 'Unit1.pas' {Form1}, DM in 'DM.pas' {DataModule1: TDataModule}; {$R *.RES} procedure DLLMain(Reason: integer); Begin case Reason of DLL_PROCESS_ATTACH: Begin application.CreateForm(TDataModule1, DataModule1); end; DLL_PROCESS_DETACH: Begin end; DLL_THREAD_ATTACH: begin end; DLL_THREAD_DETACH: begin end; end end; //Remember, in Delphi, string parameters need to be defined with PChar type procedure show_demo(handle: THandle;const param: pchar); stdcall; var oldHandle : THandle; Begin //Save the handle of the dll oldHandle := Application.Handle ; //Change the DLL application handle to the window handle in PB //The purpose is to achieve that the executable files of DLL and PB belong to an application //Effect: After opening the window in the DLL, there will be no more icon in the taskbar Application.Handle := handle ; Application.CreateForm(TForm1, Form1); try Form1.s_param := StrPas( param ); Form1.ShowModal; Finally Form1.Free; Application.Handle := oldHandle ; end; end; exports show_demo; Begin DllProc := @DLLMain; DllMain(DLL_PROCESS_ATTACH) end. |