VB application determines the system configuration parameters

zhaozj2021-02-11  236

VB5 applications are important for programmers for programmers for programmers. The system configuration is important for the development of applications. During the development of software such as multimedia applications, graphics software, etc. A critical factor. The system configuration refers to the system configuration of the Windows 95 operating system. Programmers often need to understand the system-physically configured information, physical memory, virtual memory, external memory, Windows path, display, and printer information. Visual Basic is an excellent software development tool that has become a programming language covering most of the desktop products of Microsoft. Visual Basic 5.0's professional and enterprise version introduces this unit, ActiveX document, multi-threaded environment, distributed COM, and remote automation tools, and push Visual Basic application to a higher realm. However, VB5 does not directly give the statement or function of the acquisition system configuration parameters, but the programmer needs to use the Windows Application Interface (API) to get these parameters. The Windows API is designed for C or C programmers. For a general VB programmer, it is difficult to apply the Windows API in depth. This article will give the basic method of determining the system configuration by calling the Win32 API. One. Win32 API Basic Concept and Call Windows 32-bit Application Interface (WIN32 API) contains more than 1,000 complex functions and hundreds of Windows constants, messages, and structures, making programmers to develop running in different types of programming languages Applications on Windows 95 and Windows NT operating systems. C and C languages ​​can access all Windows messages and usage, and VB can not access all, but by using Win32 API can extend VB to transcend basic properties, methods, and events, make programs to have more control over the system. And flexibility. The Win32 API includes the following four commonly used functions. 1. Windows Management (user) provides basic window constructing blocks and creation management program inputs, retrieving functions entered by the user, creates and manages user interfaces for the application via the Win32 API's Windows Management Layer. Windows Management supports system features and clipboard functions for dynamic data exchange DDE (Dynamic Data Exchange). 2. Graphics Device Interface (GDI) provides functions for supporting existing physical devices and logical devices in the system, such as monitors, printers, memory device descriptors, and more. Different drawing objects can be defined via GDI, providing a function of drawing, circle, and other shapes, providing powerful support for bitmap operations. 3. System Server (kernel) provides a way to access system resources and tools, including memory, file systems, running processing, and more. VB Visense and uses dynamic link libraries (DLL: Dynamic Link Libraries), such as API, or custom DLL, from here you can get the device parameters under the control system control and change its working state. 4. MultiMedia provides support for audio (WAV), MIDI music, AVI video, game joystick, and high-precision timer. The call to Win32 API is convenient in VB5.

Microsoft specializes in the functions used in the Win32 API function, the type structure declaration, and the global constant of the global constant, and the form of file Win32API.txt is placed in the WinAPI subdirectory in the VB Professional or Enterprise Edition. In VB5, these finishing parameters can be referenced in two ways: (1) Start the API text viewer in the VB5 folder; (2) Turn to API text View through the VB5 Editing Environment . Microsoft sometimes makes amendments to Win32API.txt, published on its outlets for users to download. Two. The WINDOWS system path Get Application When configuring and calling the basic parameters of the operating system, you must obtain the installation path of Windows, including the path of System. The WIN32 API gives a function call for getting Windows and System paths. 1. Get Windows path Declare Function GetWindowsDirectory Lib "kernel32" Alias ​​_ "GetWindowsDirectoryA" (ByVal lpBuffer As String, _ ByVal nSize As Long) As Long GetWindowsDirectory () member function belongs System Server for Windows to obtain the directory path name; GetWindowsDirectoryA It is an alias of its function (Alias). In most cases, when an API function is declared, an alias is used instead of the true function name, the alias provides a method of calling an API function with another name. If the API function name conflicts with the reserved word in VB, you must use an alias, such as getObject. 2. Acquisition system path Declare Function GetSystemDirectory Lib "kernel32" Alias ​​_ "GetSystemDirectoryA" (ByVal lpBuffer As String, _ ByVal nSize As Long) As Long GetSystemDirectory () member function belongs System Server for acquiring windows / system directory path name . First, a project is created in VB5, add a module, add the following code in the "Declaration" section of Module1. Declare Function GetWindowsDirectory Lib "kernel32" Alias ​​_ "GetWindowsDirectoryA" (ByVal lpBuffer As String, _ ByVal nSize As Long) As Long Declare Function GetSystemDirectory Lib "kernel32" Alias ​​_ "GetSystemDirectoryA" (ByVal lpBuffer As String, _ ByVal nSize As Long) As long 'defines the global variable of Windows and Windows / System Peddle PUBLIC WindowsDir as string, WindowsSysdir AS String In order to prevent space in the string returned in the API function call, it is necessary to process it, remove all spaces.

VB5 does not provide a function that eliminates characters space, so we prepare custom functions in Module1 to complete this feature. 'Alltrim () Delete the blank character function in a string Function Alltrim $ (incoming $) Temp $ = incoming $ n% = INSTR (Temp $, ChR $ (0)) IF N% Ten Temp $ = Left $ (Temp $ , N% - 1) Temp $ = LTRIM $ (RTRIM $) AllTrim $ = Temp $ END FUNCTION Establish the process of obtaining the Windows and System paths in Module1: 'Winsysdir --- Get Windows and Windows / System paths the process Sub WinSysDir () WindowsDir = Space (144) WindowsSysDir = Space (144) If GetWindowsDirectory (WindowsDir, 144) = 0 Or _ GetSystemDirectory (WindowsSysDir, 144) = 0 Then MsgBox "unable to locate Windows path!", 16, "error" Else WindowsDir = AllTrim $ (WindowsDir) WindowsSysDir = AllTrim $ (WindowsSysDir) End If End Sub () event called Form1's Form_Load WinSysDir () can get the name of the windows and the system path, demo code as follows: WinSysDir Debug.print WindowsDir debug.print WindowsSysdir runs in the VB5 environment, gives these two paths in the "Immediate" window used in debugging. three. The determination of the driver parameter is used to identify the drive type in the WIN32 API. This function has only one character type parameter to specify the drive letter you wish to identify. GetDriveType () function return value is a long-intensive value, different values ​​represent different drive types, specific correspondences are as follows: 2 --- Floppy drive 3 --- Hard drive 4 --- Network drive 5 --- CD Drive 6 --- The GetDiskFreespace () function of the Memory Drive Win32 API can get the capacity parameters of the drive in the system.

The establishment of a new project, declaring API functions in the "General" section of Form1: Private Declare Function GetDriveType Lib "kernel32" Alias ​​"GetDriveTypeA" _ (ByVal nDrive As String) As Long Private Declare Function GetDiskFreeSpace Lib "kernel32" Alias ​​_ "GetDiskFreeSpaceA "(ByVal lpRootPathName As String, _ lpSectorsPerCluster As Long, lpBytesPerSector As Long, _ lpNumberOfFreeClusters As Long, lpTtoalNumberOfClusters As Long) _ As Long Private Const DRIVE_REMOVABLE = 2 'removable drives Private Const DRIVE_FIXED = 3' fixed drive Private Const DRIVE_REMOTE = 4 'Network Drive Private Const Drive_cdrom = 5' CD Drive Private const drive_ramdisk = 6 'Memory Analog Drives Add three Label controls to form in Form1 and a ComboBox control. The following code is written separately in the form_load () and combo1_click () events.

PRIVATE SUB FORM_LOAD () DIM NUM As long Dim Buff as string label1.caption = "" label2.caption = "" label3.caption = "" for i = 0 to 25 buff = CHR $ (65 i) ": / "Num = getDriveType (BUFF) if Num> 1 Then Combo1.additem chr $ (65 i) end if next i combo1.text =" c "end sub private sub combo1_click () DIM DriveType As Long Dim DRIVESIZE Long Dim Totalc As Long Dim NumC As Long Dim SectorsC As Long Dim BytesS As Long Dim buff As String buff = Combo1.Text ": /" DriveType & = GetDriveType (buff) Select Case DriveType & Case DRIVE_REMOVABLE Label1.Caption = "this is a removable drive." Case drive_fixed label1.caption = "This is a fixed drive" Case Drive_Remote Label1.caption = "This is the network drive" Case Drive_cdrom Label1.caption = "This is a CD-ROM drive" Case Drive_ramdisk label1.caption = "This is a memory analog drive "Case Else Label1.caption =" This is an invalidated drive "End Select Drivesize = GetDiskFreespace (buff, Sectorc, bytes, Totalc, Numc) if drivesize kiln = Totalc * Sectorsc * bytes Numc = NUMC * SECTORSC * BYTESS LABEL2.CAPTION = "All Spaces of the drive" _ str (Val (Format $ (NUMC, "### ####### 0 ")) / 1024) " kbytes "label3.caption =" The remaining space of the drive " _ str (Val (field $ (Totalc," ########## 0 ")) / 1024) " kbytes "else label2.caption =" "label3.caption =" "END IF END SUB runs this program, through the drop-down box, select the drive in the system,

Thus, its related parameters are obtained. Four. Gets the type of WIN32 API for the central processor provides a discrimination of the CPU type. First declare the call to this function in the "General" section of the form FORM1: Private Declare Sub getSysteminfo LIB "kernel32" (LPSysteminfo As System-Info) Since System-Info is a type, it is also defined: Private Type SYSTEM_INFO dwOemID As Long dwPageSize As Long lpMinimumApplicationAddress As Long lpMaximumApplicationAddress As Long dwActiveProcessorMask As Long dwNumberOrfProcessors As Long dwProcessorType As Long dwAllocationGranularity As Long dwReserved As Long End Type Dim SysInfo As SYSTEM-INFO Add a Label control to the form. Add the following code to the form_load () event: Call getSystemInfo (sysinfo) selectortype case 386 label1.caption = "This machine central processor type is: 386" Case 486 label1.caption = "The machine central processor Type: 486 "Case 586 Label1.caption =" The central processor type of this machine is: Pentium "Case Else Label1.caption =" Unable to determine the central processor type "End Select 5" End Select 5 of the machine. Getting Memory Information Use the Win32 API's globalMemoryStatus () to easily get the amount of physical memory in the system, the total amount of virtual memory, the number of virtual memory numbers, and the amount of virtual memory.

In the form "universal" in the declaration portion of the GlobalMemoryStatus Form1 () function call: Private Declare Sub GlobalMemoryStatus Lib "kernel32" (lpBuffer As MEMORYSTATUS) defined MEMORYSTATUS Type: Private Type MEMORYSTATUS dwLength As Long dwMemoryLoad As Long dwTotalPhys As Long dwAvailPhys As Long dwTotalPageFile as Long dwAvailPageFile as Long dwTotalVirtual as Long dwAvailVirtual as Long End Type Dim MemInfo as MEMORYSTATUS add in the form Form1 three Label controls, respectively Label1, Label2 and Label3, write the following code in the Form_Load () event: Call GlobalMemoryStatus (Meminfo) Label1.caption = "Total physical memory:" Str $ (Meminfo.dwtotalphys) "BYTES" label2.caption = "virtual memory total:" Str $ (Meminfo.dwtotalVirtual) "Bytes" label3 .Caption = "Use virtual memory:" Str $ (Meminfo.dwavailphys) "BYTES" Label4.caption = "Remaining Virtual Memory:" Str $ (MemInfo.dwavailvirtual "Bytes" 6. Gets the getSystemMetrics () function in the resolution of the current display, the GetSystemMetrics () function in this area provides this measurement parameters. Declaring call in the "General" section of the form: Private Declare Function GetSystemMetrics LIB "User32" (Byval Nindex as long) (Byval Nindex As Long) AS Long const SM_CXSCREEN = 0 const SM_CYSCREEN = 1 Add a Label control in the form, in form_load () event The GetSystemMetrics () function is used to determine the resolution of the current display.

DIM X AS STRING DIM Y AS STRING N = STR (GetSystemMeen)) Y = Str (getSystemMetrs (SM_CYSCREEN)) label1.caption = "Current Display Resolution:" N "X" Y. Calling the msinfo32.exe program In the MS Office Suite, Microsoft provides the msinfo32.exe utility, which fully provides users with system, print, DLL, font, proofreading, graphics filter, text converter, display, audio, Video, CD-ROM, OLE registry, activity module, input method, etc. This program is typically placed under the drive / program files / microsoft shared / msinfo path where Windows is located. In the VB application, generally call this utility in the "About" window, although the information cannot be applied directly, it increases the professionalism of the program. To successfully call msinfo32.exe, the key is to get the path it is. This path cannot be obtained directly from the Win32 API function, nor does it exist in the Windows configuration file, and exists only in Windows registry. The Office Kit is registered at Msinfo32.exe when installing. It can be called for office components. The path information is saved in the hkey_local_machine / software / msinfo of the registry, so the problem of getting the path is transformed into For the query of the registry. The Win32 API provides a series of functions for manipulating the registry. It uses the regopenkeyex (), regQueryValueex (), and regcloseKey () functions, which are used to open the registry keyword, the value of the registry keyword, and close the registry keyword. . If regoPENKEYEX () and regQueryValueex () return to 0, the registry keyword is successfully opened, and the predetermined MSIFO32.exe keyword value is retrieved, and finally call the shell () function to launch Msinfo32.exe. The VB5 Form Wizard provides a standard call process. First create a new project, then select "Delete Form1" in the Projects menu, add a form, select "Dialog Box" in the pop-up dialog, which produces a custom standard "About "Form, where there is a call to msinfo32.exe, and set corresponding processing for running errors. Microsoft has made detailed comments on the code, and details will not be described here. Visual Basic's Enterprise Edition provides Microsoft Developer Network (MSDN) launching toolbox, after installation, you can access Win32 Software Development Kit (SDK), which provides file instructions for Win32 API, which can help programmers understand each function, structure And constant work principle. For VB programmers, understanding and using the Windows API have certain difficulties. It requires the programmer to have a more in-depth understanding of Windows work. If you master the use of Windows API, you can greatly expand the Visual Basic language. Function makes the application more deeper, flexible.