![]() |
|
#1
|
|||
|
|||
|
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!
|
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
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 ... |
|
#4
|
|||
|
|||
|
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. |
|
#5
|
|||
|
|||
|
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 |
|
#6
|
|||
|
|||
|
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. |
|
#7
|
|||
|
|||
|
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 |
![]() |
| Thread Tools | |
| Display Modes | |
|
|