Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help to improve my code 2

Status
Not open for further replies.

michaenh

Programmer
Aug 7, 2002
205
NO
I have created...

type
//Dll functions
TFunc = function: string; stdcall;
TFunc2 = function(test: string): string; stdcall;
.
.
.

procedure GetDllMethods(Dllname: string; Methods: string);
.
.
.

//call Dll library and metods on the fly
procedure TMainForm.GetDllMethods(Dllname: string; Methods: string);
var
FPointer: TFarProc;
Instance: THandle;
Funk: TFunc;
begin
Instance := SafeLoadLibrary (Dllname);

if Instance <> 0 then //check library
begin
FPointer := GetProcAddress (Instance,
PChar (Methods));
if FPointer <> nil then //check methods exists
begin
Funk := GetProcAddress (Instance,
PChar (Methods));
Funk;
end
else
ShowMessage('Can not find ' + Methods + 'method');
end
else
ShowMessage('Can not find ' + Dllname + 'libary');
end;

My questions have to use the same procedure to call TFunc2 instead of creating same procedure for TFunc2 but use Func: TFunc2 as variable.

Parameter passing or pointing object, something or somehow?
Any help

Many Thanks
m

 
Just trying to explain further more...

If I want to get TFunc2 have to create another procedure like this..

//call Dll library and metods on the fly
procedure TMainForm.GetDllMethods2(Dllname: string; Methods: string);
var
FPointer: TFarProc;
Instance: THandle;
Funk: TFunc2;
begin
Instance := SafeLoadLibrary (Dllname);

if Instance <> 0 then //check library
begin
FPointer := GetProcAddress (Instance,
PChar (Methods));
if FPointer <> nil then //check methods exists
begin
Funk := GetProcAddress (Instance,
PChar (Methods));
ShowMessage(Funk('michael'));
end
else
ShowMessage('Can not find ' + Methods + 'method');
end
else
ShowMessage('Can not find ' + Dllname + 'libary');
end;

Many thanks any help...

Cheers, m

 
My question is how to use the same procedure to call TFunc2 and TFunc instead of creating allmost the same procedure for TFunc and TFunc2... ??

Any help.. many thanks.. and I will give you a star.. :)

michael
 
Maybe this will help:
Code:
type
 //Dll functions
 TFunc = function: string; stdcall;
 TFunc2 = function(test: string): string; stdcall;
...

function TMainForm.GetDllMethods(var AFunc; Dllname: String; Methods: String): Boolean;
var
 Instance: THandle;
begin
 Result := False;
 Instance := SafeLoadLibrary (Dllname);
 if (Instance <> 0) then
 begin
   Pointer(AFunc) := GetProcAddress (Instance, PChar (Methods));
   Result := (Assigned(Pointer(AFunc));
 end
 else
  ShowMessage('Can not find ' + Dllname + 'libary');
end;

procedure TMainForm.Button1OnClick(Sender: TObject);
var
  AFunc : Pointer;
begin
  // calling the first function
  if GetDllMethods(AFunc, 'MyLibrary', 'function1') then
   TFunc(AFunc);
end;

procedure TMainForm.Button2OnClick(Sender: TObject);
var
  AFunc : Pointer;
begin
  // calling the second function
  if GetDllMethods(AFunc, 'MyLibrary', 'function2') then
   TFunc2(AFunc)('michael');
end;
HTH

--- markus
 
I will try it but as far as I can see it seems great...
I will give you a star for your great opinion..

I will try to implement your ideas... thanks

:)

Many thanks.. lol... your great.

cheers, m
 
Hi markus.

It seems that TFunc(AFunc) call does not manage to read the pointers adresse or somehow if it is outside the getDllMetods function.

If TFunc(AFunc) call is inside then it works...

like this:
Pointer(AFunc) := GetProcAddress (Instance, PChar (Methods));
TFunc(AFunc);
//FreeLibrary(Instance);
Result := (Assigned(Pointer(AFunc)));


Do you know how to make the call work.. ? outside the getDllMethods..

or how to pass TFunc(X) through the parameter some how...??

A lot of thanks.. I am really happy for our discussion..
michael
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top