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!

Newbie Question:- Dynamically Loading Libraries 1

Status
Not open for further replies.

Jasonic

Technical User
Jun 20, 2001
69
GB
Dear All

I have read the online help about loadLibrary and Calling dynamically loadable libraries, but still seem to have trouble.

I have an MDI application that hopefully extends the functionality of an application through the API's supplied

I have read the application API example for a C program

I.E
#include apiproto.h
#define SZ_MODEL_LEN 200
long lCnx ;
long lQuery ;
long lStatus ; /* to store error code */
char szModel[SZ_MODEL_LEN] ;
/* dll initialization */
AmStartup();
/* Open a connection */
lCnx = AmOpenConnection(connection,user,pword);

In the section about calling dynamic libraries I have to declare the functions/procedures requried, therefore I created

procedure AmStartUp(); external 'MyDll.dll';

But it complains about this

Can anyone point me in the right direction or provide easier to understand information about loading and calling dll functions

Thanks

Jason Thomas
AssetCenter Consultant
:)
 
Hi
Try this...
procedure AmStartUp; stdcall; external 'MyDll.dll';

procedure TForm1.BtnClick(Sender: TObject);
begin
AmStartUp;
end;

michaenh
 
Michaenh

I seem to be getting an error just after the word external the comipler complains about 'Field definition not allowed after methods or properties'

any ideas, have I put the code in the wrong place?

Cheers Jason Thomas
AssetCenter Consultant
:)
 
This is part of my code for dynamically connecting to functions from some DLL: It is in a unit, and it's partly an original work, but also some parts are from external sources. This code has no real function as published here. (Disclaimer)

Code:
...
Var
NetShareAdd: function(ServerName : PChar; ShareLevel : SmallInt; Buffer : Pointer; Size : Word) : Integer; StdCall;
NetShareAddNT: function(ServerName : PChar; ShareLevel : DWord; Buffer : Pointer; Error : Pointer) : Integer; StdCall;
...
Var Hand : LongWord;
Initialization
If IsNT then
    Hand := LoadLibrary('NetApi32')
Else
    Hand := LoadLibrary('SvrApi');
If Hand <> 0 then
Begin
    If IsNT then
        NetShareAddNT := GetProcAddress(Hand,'NetShareAdd')
    Else
        NetShareAdd := GetProcAddress(Hand,'NetShareAdd');
End;
Finalization
    If Hand <> 0 then
        FreeLibrary(Hand);

HTH
TonHu
 
You have a dll as ex.'MyDll.dll' that you have created in C or C++ and a procedure AmStartUp which just give a msg 'hi' and a function which returns 123.


To get that proc from your unit:
unit CallCppF;

interface

uses
SysUtils, etc...

type
TForm1 = class(TForm)
BtnProc: TButton;
BtnFunc: TButton;
....
procedure BtnClick(Sender: TObject);
procedure BtnDoubleClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

{definition of the functions or procedure of the DLL}
procedure AmStartUp; stdcall; external 'MyDll.dll';
function Triple: Integer; stdcall; external 'MyDll.dll';

procedure TForm1.BtnClick(Sender: TObject);
begin
AmStartUp; //trigger the dll's proc -> msg hi
end;

procedure TForm1.BtnDoubleClick(Sender: TObject);
begin
ShowMessage(IntToStr(Triple)); //trigger the dll's func -> returns 123 msg
end;

You could also do as TonHu suggested.
:)
 
if your function dll have a name..
ex.

function Add (A, B: Integer): Integer;
stdcall; external 'CPPDLL.DLL' name '@Add$qqsii';

:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top