Visual Basic calls the application for Windows API functions

zhaozj2021-02-11  207

Visual Basic calls the application of the Windows API function. Example [Summary] This article introduces a method of using the Visual Basic calls the Windows API function, and has passed the functionality of the API function. For example, the VB program has the extent of the initialization parameter, and the detection of the preceding system information. [Key words] Windows Microsoft (Micro-Soft) Division's Windows System. Visual Basic (VB) Microsoft visualization programming tool. API (Application Program Interface) Application Procedure Adjustment. Initialization documents (examples: demo.ini) [Text] First, use VB to call the API function method. (1) Use the declare statement to disclose the API function to be called. If the function is returned to the value, it can be exposed to the SUB process; if there is a return value, it can be applying to the function function. Note: The application of all API functions is in ./vb/winapi/win31api.hlp, only need to be placed in the corresponding place with COPY and PASTE. (2) Once you have an application of an API function, you can use the function of calling VB. However, it is necessary to pay attention, if the parameter is transmitted is wrong, it can lead to the dead. Second, the VB program privately has the deposit of initialization parameters. The acquisition of the initialization parameters of Windows software is implemented by reading a text file with the extended name .ini, that is, before the program is run, you can search for the required parameters from the specified INI file before running, and reflect the running environment of the program. In; when the program exits, saves the current environmental parameters to the specified INI file. These features are available in GetPrivateProfileString and WritePrivateProfileString in the API function provided by Windows. Description: (1) GetPrivateProfileString statement: Declare Function GetPrivateProfileString Lib "kernel32" Alias ​​"GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As long function: Get the parameter (string) corresponding to the specified keyword and copy this parameter to LPRETURNEDSTRING.

Parameter Type Description LPApplicationName String Segment LpKeyName String String LpkeyName String The keyword name of the parameter corresponds to the keyword name of the LPDefault string Specifies the default value of the returned LPRETURNEDSTRING STRING Pre-allocated at least nsize bytes nSized Integer string buffer will be loaded into the buffer name lpReturnedString maximum number of characters lpFileName string initialization file the return value Integer number of characters copied to the buffer lpReturnedString (2) WritePrivateProfileString statement: declare Function WritePrivateProfileString Lib "kernel32" Alias ​​"WritePrivateProfileStringA" (BYVAL LPAPPLICATIONNAME AS STRING, BYVAL LPKEYNAME AS ANY, BYVAL LPSTRING AS ANY, BYVAL LPFILENAME AS STRING) AS LONG Function: Set parameters (string) in the specified keyword item of the initialization file. Return value: If the set is successful, return True; no, return false. Third, VB detection When the API function required for the pre-system information has GetWindowsDirectory, getwinflags, getversion, getsystemdirectory, et al, etc. Fourth, for example, Demo.prj (1) Construction of items Demo.prj; build a module document Demo.bas. The following sound is inserted into Demo.bas.

Declare Function GetWindowsDirectory Lib "kernel32" Alias ​​"GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long Declare Function GetPrivateProfileString Lib "kernel32" Alias ​​"GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String , ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long Declare Function WritePrivateProfileString Lib "kernel32" Alias ​​"WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long Declare Function GetKeyboardType Lib "user32" (ByVal nTypeFlag As Long) As Long Declare Function GetSystemDirectory Lib "kernel32" Alias ​​"GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long Declare Function GetVersion Lib "kernel32" () As long declare function getwinflags lib "kernel32" () AS long Note:

GetWindowsDirectory: This function gets the path of the Windows Directory. GetSystemDirectory: This function gets the path of the Windows system subdirectory. Getversion: This function returns to the front Windows version number and the DOS version number. The low-bit byte of the return value Description Windows main version number, the high-level byte of the return value of the low word Description Windows depth version, the low byte of the high word Description DOS deputy version number, the high-level byte of the high word Description DOS main version number . 4, getwinflags: This function returns to the memory configuration on the Windows running system. Returns the value of the flag meaning WF_80X87 Intel math coprocessor WF_CPU386 80386 CPU WF_CPU486 80486 CPU WF_ENHANCED Windows operating system 386 in enhanced mode WF_PMODE Windows operating system in protected mode WF_STANDARD Windows operating system in a normal mode WF_WLO run under OS / 2 5, GetKeyboardType () : This function gets the system keyboard type, and NTYPEFLAG = 0 returns the keyboard type. Return Value Incidence 1 IBM PC / XT or Compatible Keyboard 2 Olivetti "ICO" keyboard (102 keys) 3 IBM or compatible keyboard (84 keys) 4 IBM enhanced or similar keyboard (101 or 102 keys) 5 Nokia 1050 or Similar keyboard 6 NOKIA 9140 or similar keyboard 7 Japanese keyboard 6, in order to call these API functions more convenient, some contain functions can be written. These user-defined functions also need to be in the module.

Function SysDir () Temp = Space $ (255) StringLen = GetSystemDirectory (Temp, 255) SysDir = Left $ (Temp, StringLen) End Function___________________________________________________________________________Function WinDir () As StringTemp = Space $ (255) StringLen = GetWindowsDirectory (Temp, 255) WinDir = Left $ (Temp, StringLen) End Function___________________________________________________________________________Function KeyType () KbType = GetKeyboardType (0) Select Case KbType Case 1 KeyType = "IBM PC / XT, or compatible (83key)" Case 2 KeyType = "Olivetti 'ICO' (102key) "Case 3 Keytype =" IBM At, or Similar (84key) "Case 4 Keytype =" ibm enhance (101 or 102 keys) "case 5 keytype =" Nokia 1050 or Similar "case 6 keytype =" Nokia 1050 or Similar "CASE 6 keytype = "Nokia 9140 or Similar" case 7 keytype = "japan keyboard" end selected function__________________________________________________fu NCTION initial () form1.left = val (StringFormini ("Formini (" FORM "," Left "," C: /Windows/demo.ini ") Form1.top = Val (StringFormini (" Form "," TOP "," 0 "," c: /windows/demo.ini ")) Form1.WindowState = Val (StringFormini (" Formini, "WindowState", "0", "C: /Windows/demo.ini"))) End Function_______________________________________________________________________Function SaveInfor () x = WritePrivateProfileString ( "Form", "left", Str $ (Form1.Left), "c: /windows/demo.ini") x =

WritePrivateProfileString ("Form", "TOP", Str $ (Form1.top), "C: /Windows/demo.ini") x = WritePrivateProfileString ("Form", "WindowsTate", Str $ (Form1.WindowState, " c: /windows/demo.ini ") End Function___________________________________________________________________________Function StringFormINI (SectionName As String, KeyName As String, Default As String, FileName As String) As StringMaxStringLen% = 255returnedstr $ = Space $ (MaxStringLen%) Result% = GetPrivateProfileString (SectionName, KeyName, Default, returnedstr $, MaxStringLen%, FileName) returnedstr $ = LTrim $ (RTrim $ (returnedstr $)) returnedstr $ = Left $ (returnedstr $, Len (returnedstr $) - 1) StringFormINI = returnedstr $ End Function__________________________________________________________________________Function WinVer ( ) ver & = GetVersion () winhigh = ver & Mod 256winlow = Int (ver & / 256) Mod 256WinVer = ((winhigh * 100) winlow) / 100End Function___________________________________________________________________________Function DosVer () ver & = GetVersion () Temp = ver & / 65536doshigh = Int (Temp / 256) Mod 256doslow = Temp Mod 256DosVer = ((doshigh * 100) doslow) / 100End Function___________________________________________________________________________Function CPU () Flags & = GetWinFlags () Match = 1Select Case Match Case (Flags & And & H8) / & H8 CPU = 486 Case (Flags & And & H4) / & H4 CPU = 386End Select End Function ___________________________________________________________________________Function Mode () Flags & = GetWinFlags () If flags & And & H20 Then Mode = "

转载请注明原文地址:https://www.9cbs.com/read-3969.html

New Post(0)