Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 02-08-2001, 05:15 AM
glutus glutus is offline
Junior Member
 
Join Date: Feb 2001
Posts: 29
Default How do I access functions in a .DLL?

How do I easiest and best access functions within a .DLL? Haven't tried it before and my boss will kill me if I don't have a thing ready by tomorrow. Please help me stay alive!
Reply With Quote
  #2  
Old 02-08-2001, 05:34 AM
Claude Claude is offline
Senior Member
 
Join Date: Jan 2001
Posts: 178
Default RE: How do I access functions in a .DLL?

First, check out a few examples. See Delphi's Help (look for the "external" keyword). You can also look at Windows.pas.

The tricky part is that you have to know what your DLL is made of. You have to know the names AND the arguments of every function you want to call. Most commercial DLL I worked with came with at least a few C and VB samples on how to call their functions. Some of them came with Delphi samples.

If you are unsure of the way the Delphi arguments you have to declare, try posting a C example and I am sure some people will be able to help.
Reply With Quote
  #3  
Old 02-08-2001, 05:53 AM
PoseFant PoseFant is offline
Senior Member
 
Join Date: Jan 2001
Posts: 1,002
Default RE: How do I access functions in a .DLL?

The easiest way is static loading :

Function FunctionName(ParamterList) ReturnType; StdCall; External 'DLLNAME.DLL';

If we take a look at a simple Function in kernel32.dll

BOOL GetComputerName(

LPTSTR lpBuffer,
LPDWORD nSize
);

This is declared in Delphi like this :

Function GetComputerName; External 'KERNEL32.DLL' Name 'GetComputerNameA';

How do we call it :

Var
ComputerName : Array[0..255] Of Char;
Size : DWord;
Begin
Size := 256;
GetComputerName(@ComputerName,Size);
End;

Lets take a look at a more complicated Function from netapi32.dll, NetUserGetGroups :

NET_API_STATUS NetUserGetGroups(

LPWSTR servername,
LPWSTR username,
DWORD level,
LPBYTE *bufptr,
DWORD prefmaxlen,
LPDWORD entriesread,
LPDWORD totalentries
);

This is how we declare it in Delphi :

Function NetUserGetGroups(ServerName, UserName : lpWStr; Level : DWord; Var Buffer : Pointer; PrefMaxLen : DWord; EntriesRead, TotalEntries : lpDWord) : LongInt; StdCall; External 'NETAPI32.DLL';

How do we call it :

Type
USER_INFO_0 = Record
Group_Name : Array[0..30] Of Char;
End;

Var
Buffer : Pointer;
User_Info_0_Struct : USER_INFO_0;
EntriesRead,
TotalEntries : lpDWord;
ServerName,
Username : Array[0..255] Of Char;
PServerName,
PUserName : lpWStr;
Result : LongInt;
I : Integer;
Begin
StringToWideChar(ServerName,@PServerName,256);
StringToWideChar(UserName,@PUserName,256);

Result := NetUserGetGroups(@PServerName,@PUserName,0,Buffer, SizeOf(USER_INFO_0),@EntriesRead,@TotalEntries);

If Result = 0 Then
Begin
User_Info_0_Struct := USER_INFO_0(Buffer^);

For I := 0 To EntriesRead - 1 Do
ShowMessage(WideCharToString(User_Info_0_Struct.Gr oup_Name[I]);

End;
End;

So ...

As we started ...

Function FunctionName(ParameterList) : ReturnType; StdCall; External 'DLLNAME.DLL';

"StdCall" is a directive which gives the compiler instructions on how to pass the parameters and how to return parameters to the function.

Other directives may be "CDecl" or "Pascal"


Hope you learned something from this ...

Regards

/Filip

PS : I'm not able to check my code for errors, I'm not at my Delphi computer at the moment ...
Reply With Quote
  #4  
Old 02-08-2001, 06:53 AM
glutus glutus is offline
Junior Member
 
Join Date: Feb 2001
Posts: 29
Default This is how they have the dll in VB.

Thanks for your speedy answer. It did help, but made me aware of some new problems. This is how they called it in the VB example of how to use it:

Private WithEvents aRecord As SSLRecord.Record

Private Sub Form_Load()
Set aRecSvcPool = New RecordServicePool
Set aRecord = New Record
aRecord.FieldNames = _
"PROD_PERM DSPLY_NAME OFFCL_CODE RDN_EXCHID CURRENCY TRADE_DATE TIMACT"
nextFieldIndex = 7
Dim i%, j%
loadDefault
' These are the cells that can accept user input for field name
For i = 1 To Fg2.Rows - 1 Step 2
For j = 0 To Fg2.Cols - 1
Fg2.col = j
Fg2.row = i
Fg2.CellForeColor = &H8000000D
Fg2.CellBackColor = &H80000018
Next j
Next
End Sub


Private Sub StartCmd_Click()
If (aRecord.Active) Then
MsgBox "You have to stop the current record first"
Exit Sub
End If
aRecord.Name = NameText
If (ServiceList.ListIndex = -1) Then
MsgBox "Please select a service"
Exit Sub
End If
aRecord.ServiceName = ServiceList
aRecord.Start
End Sub

How do I call this in Delphi? I know the name of the .DLL, but i don't know the functions within. When I try to call it, I get several errors compiling that I really don't understand.
Reply With Quote
  #5  
Old 02-08-2001, 07:32 AM
The Smurf The Smurf is offline
Member
 
Join Date: Jan 2001
Posts: 40
Default RE: This is how they have the dll in VB.

I'm not a VB expert, but it's apparent that the code you showed isn't calling any DLL functions at all, for the simple reason that there are no API declarations. VB isn't equipped to natively deal with DLL files, so it must use the LoadLibrary, FreeLibrary and GetProcAddress API functions, which are not present in the code.

-- The Smurf
Reply With Quote
  #6  
Old 02-08-2001, 11:22 AM
MrBaseball34 MrBaseball34 is offline
Senior Member
 
Join Date: Jan 2001
Posts: 7,260
Default RE: This is how they have the dll in VB.

I totally agree. When I first looked at the code, I saw that
it was a COM object it was calling. Not sure how to call one
of those from Delphi without the TLB.
Reply With Quote
  #7  
Old 02-09-2001, 02:05 AM
glutus glutus is offline
Junior Member
 
Join Date: Feb 2001
Posts: 29
Default The .DLL is loaded as a reference

The .DLL is loaded as a reference (Project - References). The VB code surely uses its functions. I just don't know how.


The first line of the program defines a variable, or link to .dll:

Private WithEvents aRecord As SSLRecord.Record


The second line in the FormCreate procedure creates a new instance of it, n'est-ce pas?

Set aRecord = New Record
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 04:15 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.