This is an overview about the possibility to use the WinAPI or any DLL files inside your KBasic code on Windows.
In KBasic you may use the syntax of VB6 to delcare DLL calls, but it is strongly recommend to use the new syntax, because it makes it easier to work with and helps you to get organized your code.
Before you can use any DLL file you must tell KBasic, which DLL file you would like to use and which functions are available through the DLL file, after that you can call that routine as you would call any KBasic function.
Warning! If you use predeclared DECLARE statements of VB6, be aware that the size of the datatypes differs between VB6 and KBasic, namely Long in VB6 must be Integer in KBasic. Whenever a null-terminated string is expected, you have to write CString and NOT String as datatype, otherwise you will get an error. You have to change it. |
DLL files' extension are ”.so” on Linux, ”.dylib” on Mac OS X, and ”.dll” on Windows.
' DLL USING (new style) ' Warning! If you use predeclared DECLARE statements of VB6, be aware ' that the size of the datatypes differs between VB6 and KBasic, ' namely Long in VB6 must be Integer in KBasic! You have to change it. Class comdlg32 Alias Lib "comdlg32.dll" Static Function ChooseColor_Dlg Alias "ChooseColorA"_ (lpcc As CHOOSECOLOR_TYPE) As Integer Type CHOOSECOLOR_TYPE lStructSize As Integer hwndOwner As Integer hInstance As Integer rgbResult As Integer lpCustColors As Integer flags As Integer lCustData As Integer lpfnHook As Integer lpTemplateName As CString End Type ' Anwender kann alle Farben wählen Const CC_ANYCOLOR = &H100 ' Nachrichten können "abgefangen" werden Const CC_ENABLEHOOK = &H10 ' Dialogbox Template Const CC_ENABLETEMPLATE = &H20 ' Benutzt Template, ignoriert aber den Template-Namen Const CC_ENABLETEMPLATEHANDLE = &H40 ' Vollauswahl aller Farben anzeigen Const CC_FULLOPEN = &H2 ' Deaktiviert den Button zum Öffnen der Dialogbox-Erweiterung Const CC_PREVENTFULLOPEN = &H4 ' Vorgabe einer Standard-Farbe Const CC_RGBINIT = &H1 ' Hilfe-Button anzeigen Const CC_SHOWHELP = &H8 ' nur Grundfarben auswählbar Const CC_SOLIDCOLOR = &H80 End Class Dim CC_T As comdlg32.CHOOSECOLOR_TYPE, Retval As Integer Dim BDF(16) As Integer 'Dim k As CString 'CC_T.lpTemplateName = AddressOf(k) 'CC_T.lpTemplateName = "fdgfg" 'Print CC_T.lpTemplateName 'Einige Farben vordefinieren (Benutzerdefinierte Farben) BDF(0) = RGB(255, 255, 255) BDF(1) = RGB(125, 125, 125) BDF(2) = RGB(90, 90, 90) 'Print Len(CC_T) 'Strukturgröße With CC_T .lStructSize = Len(CC_T) 'Strukturgröße .hInstance = 0'App.hInstance 'Anwendungs-Instanz .hwndOwner = 0 'Me.hWnd 'Fenster-Handle .flags = comdlg32.CC_RGBINIT Or comdlg32.CC_ANYCOLOR Or comdlg32.CC_FULLOPEN Or comdlg32.CC_PREVENTFULLOPEN 'Flags .rgbResult = RGB(0, 255, 0) 'Farbe voreinstellen .lpCustColors = AddressOf(BDF(0)) 'Benutzerdefinierte Farben zuweisen End With Retval = comdlg32.ChooseColor_Dlg(CC_T) 'Dialog anzeigen If Retval <> 0 Then MsgBox Hex$(CC_T.rgbResult) 'gewählte Farbe als Hintergrund setzen Else MsgBox "Das Auswählen einer Farbe ist fehlgeschlagen," & _ "oder Sie haben Abbrechen gedrückt", kbCritical, "Fehler" End If
' DLL USING (old style) ' Warning! If you use predeclared DECLARE statements of VB6, be aware ' that the size of the datatypes differs between VB6 and KBasic, ' namely Long in VB6 must be Integer in KBasic! You have to change it. ' Play midi file using the windows api. Not portable! ' Be sure that the midi files are correctly named to the install path of KBasic ' in this example! Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA"_ (ByVal lpszCommand As CString, ByVal lpszReturnString As CString, _ ByVal cchReturnLength As Integer, ByVal hwndCallback As Integer) As Integer Dim s As CString Dim k As CString Dim r As Integer k = Space(1024) r = mciSendString("close all", k, Len(k), 0) Randomize Timer Select Case Int(RND * 4) + 1 Case 1 s = "Open c:\kbasic15\examples\test\mond_1.mid Type sequencer Alias MUSIC" Case 2 s = "Open c:\kbasic15\examples\test\mond_3.mid Type sequencer Alias MUSIC" Case 3 s = "Open c:\kbasic15\examples\test\pathetique_1.mid Type sequencer Alias MUSIC" Case 4 s = "Open c:\kbasic15\examples\test\pathetique_2.mid Type sequencer Alias MUSIC" End Select r = mciSendString(s, k, Len(k), 0) If r = 0 Then r = mciSendString("play MUSIC from 0", k, Len(k), 0) End If
' DLL USING (old style) ' Warning! If you use predeclared DECLARE statements of VB6, be aware ' that the size of the datatypes differs between VB6 and KBasic, ' namely Long in VB6 must be Integer in KBasic! You have to change it. ' Play wav file using the windows api. Not portable! ' Be sure that the wav files are correctly named to the install path of KBasic ' in this example! Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA"_ (lpszName As CString, ByVal hModule As Integer, ByVal dwFlags As Integer) As Integer Dim s As CString Randomize Timer Select Case Int(RND * 2) + 1 Case 1 s = "c:\kbasic14\ide\gong.wav" Case 2 s = "c:\kbasic14\ide\neon_light.wav" End Select Dim r = PlaySound(s, 0, 0)
hwnd in form class Declare Function Beep Lib "kernel32.dll" ( _ ByVal dwFreq As Long, _ ByVal dwDuration As Long) As Long Declare Function BringWindowToTop Lib "user32.dll" (ByVal hwnd As Long) As Long Declare Function ChangeDisplaySettings Lib "user32.dll" _ Alias "ChangeDisplaySettingsA" ( _ lpDevMode As Any, _ ByVal dwFlags As Long) As Long Declare Function EnumWindows Lib "user32.dll" ( _ ByVal lpEnumFunc As Long, _ ByVal lParam As Long) As Long Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long) Declare Sub ExitWindowsDialog Lib "shell32.dll" _ Alias "#60" ( _ ByVal hwndOwner As Long) Declare Function ExitWindowsEx Lib "user32" ( _ ByVal uFlags As Long, _ ByVal dwReserved As Long) As Long Declare Function FindExecutable Lib "shell32.dll" _ Alias "FindExecutableA" ( _ ByVal lpFile As String, _ ByVal lpDirectory As String, _ ByVal lpResult As String) As Long Declare Function FindWindow Lib "user32.dll" _ Alias "FindWindowA" ( _ ByVal lpClassName As String, _ ByVal lpWindowName As String) As Long Declare Function FlashWindow Lib "user32.dll" ( _ ByVal hwnd As Long, _ ByVal bInvert As Long) As Long Declare Function GetComputerName Lib "kernel32.dll" _ Alias "GetComputerNameA" ( _ ByVal lpBuffer As String, _ nSize As Long) As Long Declare Function GetCurrentProcessId Lib "kernel32" () As Long Declare Function GetDiskFreeSpace Lib "kernel32.dll" _ Alias "GetDiskFreeSpaceA" ( _ ByVal lpRootPathName As String, _ lpSectorsPerCluster As Long, _ lpBytesPerSector As Long, _ lpNumberOfFreeClusters As Long, _ lpTotalNumberOfClusters As Long) As Long Declare Function GetEnvironmentVariable Lib "kernel32" _ Alias "GetEnvironmentVariableA" ( _ ByVal lpName As String, _ ByVal lpBuffer As String, _ ByVal nSize As Long) As Long Declare Function GetForegroundWindow Lib "user32.dll" () As Long Declare Function GetLastError Lib "kernel32.dll" () As Long Private Declare Function GetLogicalDrives Lib "kernel32.dll" () As Long Declare Function GetUserName Lib "advapi32.dll" _ Alias "GetUserNameA" ( _ ByVal lpBuffer As String, _ nSize As Long) As Long Declare Function GetWindowText Lib "user32" _ Alias "GetWindowTextA" ( _ Byval hwnd As Long, _ Byval lpString As String, _ Byval cch As Long) As Long Declare Function GetWindowThreadProcessId Lib "user32.dll" ( _ ByVal hwnd As Long, _ lpdwProcessId As Long) As Long Declare Function IsWindowEnabled Lib "user32.dll" (ByVal hwnd As Long) As Long Declare Function MessageBeep Lib "user32.dll" (ByVal wType As Long) As Long Declare Function MoveWindow Lib "user32.dll" ( _ ByVal hwnd As Long, _ ByVal x As Long, _ ByVal y As Long, _ ByVal nWidth As Long, _ ByVal nHeight As Long, _ ByVal bRepaint As Long) As Long Declare Function RestartDialog Lib "shell32.dll" _ Alias "#59" ( _ ByVal hwndOwner As Long, _ ByVal lpstrReason As String, _ ByVal uFlags As Long) As Long Declare Function SetFocusWnd Lib "user32.dll"Alias "SetFocus" ( _ ByVal hwnd As Long) As Long Declare Function ShowWindow Lib "user32" ( _ ByVal hwnd As Long, _ ByVal nCmdShow As Long) As Long Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long ) LPTCH WINAPI GetEnvironmentStrings(void); BOOL WINAPI TerminateProcess( __in HANDLE hProcess, __in UINT uExitCode ); google translator für übersetzerprgoramm