Как использовать формы, объявленные в DLL
Как использовать формы, объявленные в DLL
Как использовать формы, объявленные в DLL in the example that follows the exe only sees a totally "virtual abstract" interface to the object as is being exported from the dll but it still can create the object and use it. of course the exe can not see or execute any methods declared in the exe but that is the whole purpose of implementing them in a custom dll to begin with. im folgenden beispiel sieht die exe-datei nur ein total "virtuelles, abstraktes" interface zum objekt, welches aus der dll importiert wird aber es kann doch dieses objekt erzeugen und es gebrauchen. } // example code: program dlloader; uses sharemem, forms, exeunit1 in 'exeunit1.pas' {form1}, dllintfu in 'dllintfu.pas'; {$r *.res} begin application.initialize; application.createform(tform1, form1); application.run; end. //-------------------------- unit dllintfu; interface type tdllobject = class protected function get_username: string; virtual; abstract; procedure set_username(value: string); virtual; abstract; public property username: string read get_username write set_username; end; tdllobjectclass = class of tdllobject; implementation end. //--------------------------- unit exeunit1; interface uses windows, messages, sysutils, classes, graphics, controls, forms, dialogs, dllintfu, stdctrls; type tform1 = class(tform) button1: tbutton; procedure button1click(sender: tobject); private { private declarations } public { public declarations } end; var form1: tform1; implementation {$r *.dfm} type tdllfunc = function: tdllobjectclass; stdcall; procedure tform1.button1click(sender: tobject); var i: dword; fhandle: thandle; fdllfunc: tdllfunc; fdllobject: tdllobject; fusername: string; begin fhandle := loadlibrary('username.dll'); if (fhandle <> 0) then begin @fdllfunc := getprocaddress(fhandle, 'dllfunc'); if assigned(@fdllfunc) then begin i := 255; setlength(fusername, i); getusername(pchar(fusername), i); fusername := strpas(pchar(fusername)); fdllobject := fdllfunc.create; fdllobject.username := fusername; showmessage(fdllobject.username); fdllobject.free; end; freelibrary(fhandle); end; end; end. //------------------------------- library username; uses sharemem, sysutils, dllintfu; type tcustomdllobject = class(tdllobject) private fusername: string; function getfilecount: integer; protected function get_username: string; override; procedure set_username(value: string); override; end; tcustomdllobjectclass = class of tcustomdllobject; function tcustomdllobject.getfilecount: integer; var doserr: integer; fsrch: tsearchrec; begin result := 0; doserr := findfirst('*.*', faanyfile, fsrch); if (doserr = 0) then begin while (doserr = 0) do begin if (fsrch.attr and fadirectory) = 0 then inc(result); doserr := findnext(fsrch); end; findclose(fsrch); end; end; function tcustomdllobject.get_username: string; begin result := 'you signed on as ''' + fusername + '''' + ' and there ' + inttostr(getfilecount) + ' files in this directory.'; end; procedure tcustomdllobject.set_username(value: string); begin fusername := value; end; function dllfunc: tcustomdllobjectclass; stdcall; begin result := tcustomdllobject; // class type only end; exports dllfunc name 'dllfunc'; begin end.