How can I initialize a dll? Please help with sample code.
--
Thanks
Best Regards
Md. Shariful Alam Khan
This is the way functions inside dlls are created and called.
Supose your dll has an exported function like this:
1º approach:
------------
function SomeFunction(S: string): string; stdcall; export;
From the program that calls the dll you can declare the function in the
interface section using this approach:
interface
function SomeFunction(S: string): string; stdcall; external 'strlib.dll';
And call the function using something like this:
begin
Showmessage(SomeFunction('hello'));
end;
2º approach:
------------
type
TMyType = function (S: string): string; stdcall;
var
myType: TMyType;
lHandle: cardinal;
begin
lHandle := LoadLibrary('yourlibrary.dll');
if lHandle<>0 then
begin
myType := GetProcAddress(lHandle, 'SomeFunction');
if @myType <> nil then
Showmessage(myType('hello'));
end;
end;
Leonardo M. Ramé
http://leonardorame.blogspot.com
Md. Shariful Alam Khan escribió:
library SampleDLL;
uses
Windows,
SysUtils,
Dialogs,
Classes,
uTestDLL in 'uTestDLL.pas' {frmTestDLL},
PUBLICTYPES in '..\New DLL\PublicTypes.pas',
FDPluginAPI in '..\New DLL\FDPluginAPI.pas';
{$R *.res}
{$E 4DX}
procedure FourDPack(selector: Longint; parameters: Pointer; var data:
Pointer; result: Pointer); stdcall;
var
params:PA_PluginParameters;
begin
params.fParameters := parameters;
params.fResult := result;
params.fData := data;
// Package is initialized. It's time to read the callback address
// if ( ( selector = kServerInitPlugin ) or ( selector = kInitPlugin ) )
then
// begin
// // get the callback address
// gCall4D := TPackInitBlock(parameters).fCall4D();
// //( (PackInitBlock*)parameters )->fSupportedVersion = 0x00000660;
// begin
// Call "PluginMain" provided by the user of the 4DPluginAPI
PluginMain(selector, params );
end;
var
pathName: array[0..MAX_PATH] of char;
(* selector: Longint;
parameters: Pointer;
data: Pointer;
result: Pointer; *)
procedure DllMain(reason: integer) ;
var
buf : array[0..MAX_PATH] of char;
loader : string;
begin
case reason of
DLL_PROCESS_ATTACH:
begin
GetModuleFileName(hInstance, buf, SizeOf(buf)) ;
loader := buf;
if Pos('4D.exe', loader) > 0 then
ExitCode := -1;
StrCopy(pathname, buf);
end;
end;
end; (*DllMain*)
exports
FourDPack;
begin
DllProc := @DllMain;
DllProc(DLL_PROCESS_ATTACH) ;
//FourDPack(selector, parameters, data, result);
//Showmessage(pathName);
end.
=========
I need dll will initialize automatically and after that it will call
FourDPack method which has some parameter. How to do this? How to intialize
a method which has some parameter in Intialization section?
Regards
Md. Shariful Alam Khan
"Peter Below (TeamB)" <none> wrote in message
news:xn0eukqz...@newsgroups.borland.com...
> Md. Shariful Alam Khan wrote:
>
> > Hello,
> >
> > How can I initialize a dll? Please help with sample code.
>
> If a DLL needs its host program to initialize it it has to export a
> function the host program needs to call first before it can call any of
> the other functions the DLL exports.
>
> If the DLL needs to initialize itself when it is loaded then you would
> place the code for that (for a DLL written in Delphi) into a unit
> Initializatiob section. This code runs once, when the DLL is loaded, in
> the context of the host apps thread that loaded the DLL (the main
> thread for implicitely loaded DLLs). A DLL written in C/C++ would
> probably use the DLLMain entry point to trigger the initialization.
>
> If a DLL is used by more than one thread of the host program it may
> also have to perform per-thread initialization. A Delphi DLL can assign
> a procedure to the DLLProc variable (see help) in its main block to get
> informed when a new thread attaches to the DLL. Another option is to
> use ThreadVars for variables that have to have separate instances for
> each thread and simple check whether they have been initialized yet
> every time an exported function tries to access them.
>
> --
> Peter Below (TeamB)
> Don't be a vampire (http://slash7.com/pages/vampires),
> use the newsgroup archives :
> http://www.tamaracka.com/search.htm
> http://groups.google.com
> http://www.prolix.be
> Hello,
>
> How can I initialize a dll? Please help with sample code.
If a DLL needs its host program to initialize it it has to export a
library Test;
var
SaveDllProc: Pointer;
procedure LibExit(Reason: Integer);
begin
if Reason = DLL_PROCESS_DETACH then
begin
... // library exit code
end;
SaveDllProc(Reason); // call saved entry point procedure
end;
begin
... // library initialization code
SaveDllProc := DllProc; // save exit procedure chain
DllProc := @LibExit; // install LibExit exit procedure
end.
Leonardo M. Ramé
http://leonardorame.blogspot.com
Md. Shariful Alam Khan escribió:
You should probably add:
if assigned(SaveDllProc) then
> SaveDllProc(Reason); // call saved entry point procedure
--
Marc Rohloff [TeamB]
marc rohloff -at- myrealbox -dot- com
> In my library I use the following:
>
>
> procedure FourDPack(selector: Longint; parameters: Pointer; var data:
> Pointer; result: Pointer); stdcall;
> procedure DllMain(reason: integer) ;
You are not thinking, sorry. Where do you intend to get the parameters
from? There is nothing preventing you from uncommenting the statement
//Fourdpack.... above, and it would run once when the DLL is loaded
(the main block of a Delphi DLL is basically the DLL_PROCESS_ATTACH
case for the DLLMain entry point, which is itself buried somewhere in
the RTL code). It would execute the FourDPack function you declared
further up, passing it whatever values your selector etc. variables
have at that time. And you can set them to whatever values you want
before the call, of course.
So, where is your problem?