PDA

View Full Version : call dll



Lionheart008
29-11-2009, 00:21
hi all.

question: how to call or load a own or existing *.dll ?

test example:


' Empty GUI script created on 11-28-2009 23:06:59 by (ThinAIR)
uses "console"
'----- example one:

Dim MyCmd As String
Dim MyStr As String
Dim MyDLL as string
Dim MyNum As EXT

'-------- ok
MyCmd = "mid$"
Call MyCmd ("lionBasic", 4, 3) To Mystr
MSGBOX 0, Mystr
MyCmd = "sin"
Call MyCmd (0.75) To MyNum
MSGBOX 0, MyNum

'----------- not ok
MyDLL = "lionbasic.dll"
call myDll ("c:\lionbasic.dll")
call mydll (1.24) to MyNum
msgbox 0, MyNum



thanks. best regards, frank

ErosOlmi
29-11-2009, 00:50
To use functions or subs inside an external DLL you have to declare the syntax of the functions using DECLARE ... statement. That's all.
Once you have declared a function that resides into an external dll, you can use as native.

It is like declaring Widows API functions, nothing different. At the end, all Windows API are contained in standard dlls.

In your example here you are not declaring any external function but you are using the dynamic functionalities of CALL statement. But that has nothing to do with external dll function.
Please check DECLARE statement in help: http://www.thinbasic.com/public/products/thinBasic/help/html/declare.htm

Michael Hartlef
29-11-2009, 10:02
Hi Franck,

in your own script you posted in the topic below, you made two declares at the beginning of your code.

http://community.thinbasic.com/index.php?topic=2985.0

Cheers
Michael

Lionheart008
29-11-2009, 13:26
thank you both. Yes I see, but I have thought there might be another way to include "*.dll" into thinbasic script, because I have found one of these examples from early time at the board.

this was important for me for handling:


Declare Function LoadLibrary Lib "KERNEL32.DLL" _
Alias "LoadLibraryA" _ '
( ByVal lpLibFileName As ASCIIZ ) As Long '

hLib = LoadLibrary( "User32.dll" )

I seldom uses these function so I am not very familiar with it.

nice sunday, frank

ErosOlmi
29-11-2009, 13:34
LoadLibrary (http://msdn.microsoft.com/en-us/library/ms684175(VS.85).aspx) function is an API function to be used only if you want to do all by manual.
Than you need to use GetProcAddress (http://msdn.microsoft.com/en-us/library/ms683212(VS.85).aspx) to get a PTR to a function inside the library you just loaded with LoadLibrary.
Than you need to call that function, passing relevant params according to function syntax.

All the above is done automatically by thinBasic if you just correctly declare the function you need.

Michael Hartlef
29-11-2009, 19:14
And i posted that because i though you wanted to know how to do the declaration in general. The way you did it there is the optimal way. Use the declare command. The loadlibrary and getprocadress api is more usefull if you work with plugins and stuff like that. If i want to interface with know functions at build time. I create the declare statement and use the command directly.

Lionheart008
30-11-2009, 16:07
hi all, michael, eros.

ok, I have understood! Here's a good example using for example "kernel32.dll" with thinbasic: a disk information viewer example, very useful!



uses "File", "ui"

Declare Function GetLogicalDriveStrings Lib "KERNEL32.DLL" Alias "GetLogicalDriveStringsA" _
( _
ByVal nBufferLength As Long, _
lpBuffer As Asciiz _
) As Long
DECLARE FUNCTION GetVolumeInformation LIB "KERNEL32.DLL" ALIAS "GetVolumeInformationA" _
( _
lpRootPathName AS ASCIIZ, _
lpVolumeNameBuffer AS ASCIIZ, _
BYVAL nVolumeNameSize AS DWORD, _
lpVolumeSerialNumber AS DWORD, _
lpMaximumComponentLength AS DWORD, _
lpFileSystemFlags AS DWORD, _
lpFileSystemNameBuffer AS ASCIIZ, _
BYVAL nFileSystemNameSize AS DWORD _
) AS LONG

DECLARE FUNCTION GetDriveType LIB "KERNEL32.DLL" ALIAS "GetDriveTypeA" (nDrive AS ASCIIZ) AS DWORD


Global hDlg As Long
Global lZStr As String * 1024

%CMB_DRIVES = %WM_USER + 1
%LBL_DRIVETYPE = %WM_USER + 2
%CHK_CASE_PRESERVED = %WM_USER + 3
%CHK_CASE_SENSITIVE = %WM_USER + 4
%CHK_UNICODE = %WM_USER + 5
%CHK_ACLS = %WM_USER + 6
%CHK_FILE_COMPRESSION = %WM_USER + 7
%CHK_VOL_COMPRESSED = %WM_USER + 8
%LBL_VOLNAME = %WM_USER + 9
%LBL_VOLSERIAL = %WM_USER + 10
%LBL_MAXFILEN = %WM_USER + 11
%LBL_FILFLAGS = %WM_USER + 12
%LBL_FILESYS = %WM_USER + 13
%LBL_DF = %WM_USER + 14
%LBL_DT = %WM_USER + 15

Type VolInfoType
VolName As Asciiz * %MAX_PATH
VolSerial As Dword
FileLength As Dword
FileSysFlags As Dword
FileSysName As Asciiz * %MAX_PATH
DriveType As Asciiz * %MAX_PATH
lZ As Asciiz * %MAX_PATH
End Type

Dim VolInf(100) As VolInfoType
Dialog NEW 0 ,"Disk Volume Information Viewer",-1,-1,200,220,%DS_MODALFRAME Or %WS_CAPTION Or _
%WS_SYSMENU Or %WS_MINIMIZEBOX, 0, To HDlg

Control Add Label , hDlg, -1, "Logical drive strings:", 4, 5, 80, 12
Control Add ComboBox, hDlg, %CMB_DRIVES,, 85, 4, 100, 60, %CBS_DROPDOWNLIST Or %WS_GROUP Or %WS_TABSTOP Or %WS_VSCROLL
Control Add Label , hDlg, -1, "Drive type:", 4, 20, 40,12
Control Add Label , hDlg, %LBL_DRIVETYPE, "", 45, 20, 80,12
Control Add Frame , hDlg, -1, "Volume Information" , 4, 35, 192, 130
Control Add Label , hDlg, -1, "Volume name:" , 10, 50, 75, 12
Control Add Label , hDlg, -1, "Serial number:" , 10, 60, 75, 12
Control Add Label , hDlg, -1, "Max length filename:", 10, 70, 75, 12
Control Add Label , hDlg, -1, "Flags:" , 10, 80, 75, 12
Control Add Label , hDlg, -1, "File system:" , 10,150, 75, 12

Control Add Label , hDlg, %LBL_VOLNAME , "-", 90, 50, 90, 12
Control Add Label , hDlg, %LBL_VOLSERIAL , "", 90, 60, 90, 12
Control Add Label , hDlg, %LBL_MAXFILEN , "", 90, 70, 90, 12
Control Add Label , hDlg, %LBL_FILFLAGS , "", 90, 80, 90, 12
Control Add Label , hDlg, %LBL_FILESYS , "-", 90,150, 90, 12

Control Add CheckBox, hDlg, %CHK_CASE_PRESERVED , "Case is preserved" , 90, 80, 90, 12
Control Add CheckBox, hDlg, %CHK_CASE_SENSITIVE , "Case sensitive" , 90, 90, 90, 12
Control Add CheckBox, hDlg, %CHK_UNICODE , "Can store unicode" , 90,100, 90, 12
Control Add CheckBox, hDlg, %CHK_ACLS , "Persistent ACLS" , 90,110, 90, 12
Control Add CheckBox, hDlg, %CHK_FILE_COMPRESSION , "File compression" , 90,120, 90, 12
Control Add CheckBox, hDlg, %CHK_VOL_COMPRESSED , "Volume is compressed", 90,134, 90, 12

Control Add Frame , hDlg, -1, "Disk free space" , 4,170,192, 40
Control Add Label , hDlg, -1, "Disk free:" , 10,185, 75, 12
Control Add Label , hDlg, -1, "Disk total:" , 10,195, 75, 12

Control Add Label , hDlg, %LBL_DF, "-" , 90,185, 90, 12
Control Add Label , hDlg, %LBL_DT, "-" , 90,195, 90, 12

Dialog Show modeless hDlg

dim Temp As String
dim lString As String
dim lRet As Long
dim lCnt As Long
dim lZ As Asciiz * %MAX_PATH

dim i, OldPause, AboutOn, OldPlay as long

dim Msg AS LONG
dim wParam AS LONG
dim lParam AS LONG


WHILE IsWindow(hDlg)

'---Get the message and fill wParam and lParam
Msg = GetMessage(hDlg, wParam, lParam)


Select Case Msg

Case %WM_INITDIALOG
lZStr = String$(1024,0)
lRet = GetLogicalDriveStrings (SizeOf(lzStr), lZStr)
lZStr = Left$(lzStr, lRet)
For lCnt = 1 To ParseCount (lZStr, Chr$(0)) -1
lZ = Parse$(lzStr, Chr$(0), lCnt)
ComboBox Add hDlg, %CMB_DRIVES, lZ
lRet = GetDriveType (lZ)
Select Case lRet
Case %DRIVE_UNKNOWN : Temp = "Cannot be determined"
Case %DRIVE_NO_ROOT_DIR : Temp = "Path does not exist"
Case %DRIVE_REMOVABLE : Temp = "Removable disk"
Case %DRIVE_FIXED : Temp = "Fixed"
Case %DRIVE_REMOTE : Temp = "Remote(Network)"
Case %DRIVE_CDROM : Temp = "CD-Rom"
Case %DRIVE_RAMDISK : Temp = "RAM Drive"
Case Else : Temp = "Unknown"
End Select
VolInf(lCnt).DriveType = Temp
GetVolumeInformation lZ, _
VolInf(lCnt).VolName, _
SizeOf (VolInf(lCnt).VolName), _
VolInf(lCnt).VolSerial, _
VolInf(lCnt).FileLength, _
VolInf(lCnt).FileSysFlags, _
VolInf(lCnt).FileSysName, _
SizeOf(VolInf(lCnt).FileSysName)
VolInf(lCnt).lZ = lZ
Next
Case %WM_COMMAND
Select Case lowrd(wParam)
Case %CMB_DRIVES
If HIwrd(wParam) = %CBN_SELENDOK Then
ComboBox Get Text hDlg, %CMB_DRIVES To lString
Control Send hDlg, %CMB_DRIVES, %CB_GETCURSEL, 0, 0 To lRet
Incr lRet
Control Set Text hDlg, %LBL_VOLNAME , VolInf(lRet).VolName
Control Set Text hDlg, %LBL_DRIVETYPE , VolInf(lRet).DriveType
Control Set Text hDlg, %LBL_VOLSERIAL, Hex$(VolInf(lRet).VolSerial, 8)
Control Set Text hDlg, %LBL_FILESYS , VolInf(lRet).FileSysName
Control Set Text hDlg, %LBL_MAXFILEN , Format$(VolInf(lRet).FileLength, "") + " Bytes"
Control Set Check hDlg, %CHK_CASE_PRESERVED , (VolInf(lRet).FileSysFlags And %FS_CASE_IS_PRESERVED)
Control Set Check hDlg, %CHK_CASE_SENSITIVE , (VolInf(lRet).FileSysFlags And %FS_CASE_SENSITIVE)
Control Set Check hDlg, %CHK_UNICODE , (VolInf(lRet).FileSysFlags And %FS_UNICODE_STORED_ON_DISK)
Control Set Check hDlg, %CHK_ACLS , (VolInf(lRet).FileSysFlags And %FS_PERSISTENT_ACLS)
Control Set Check hDlg, %CHK_FILE_COMPRESSION , (VolInf(lRet).FileSysFlags And %FS_FILE_COMPRESSION)
Control Set Check hDlg, %CHK_VOL_COMPRESSED , (VolInf(lRet).FileSysFlags And %FS_VOL_IS_COMPRESSED)

Control Set Text hDlg, %LBL_DF , Str$(Disk_Free(Left$(VolInf(lRet).lZ,2))\1024) + " Kb"
Control Set Text hDlg, %LBL_DT , Str$(Disk_Size(Left$(VolInf(lRet).lZ,2))\1024) + " Kb"
End If
End Select

CASE %WM_SYSCOMMAND
IF wParam = %SC_CLOSE THEN

EXIT WHILE
end if

End Select

wend


best regards, frank