Determination of system configuration parameters by the VB Application

Source: Internet
Author: User
Determination of system configuration parameters by vb5 applications
For programmers, understanding system configurations is of great significance for application development. When developing software that is closely related to system configurations, such as multimedia applications and graphic software, user machine configurations are different, the software dynamically acquires these configuration parameters during execution to determine the execution process, which is a crucial factor for the smooth running of the software. The system configuration here refers to the system configuration of the Windows 95 operating system. Programmers often need to understand the physical configuration information of the system, such as the central processor type, physical memory, virtual memory, external memory, Windows path, display and printer information.

Visual Basic is an excellent software development tool and has become a programming language covering most of Microsoft's Desktop products. Visual Basic 5.0 Professional Edition and Enterprise Edition introduce functions such as local code, ActiveX documents, multi-threaded environment, Distributed COM and remote automation tools, and push Visual Basic applications to a higher level. However, vb5 does not directly provide statements or functions for obtaining system configuration parameters, but requires programmers to use Windows application interfaces (APIS) to obtain these parameters. Windows APIs are designed for C or C ++ programmers. It is very difficult for VB programmers to deeply Apply Windows APIs, this document describes how to call Win32 API to determine system configuration.

  

1. Basic Concepts and calls of Win32 APIs

Windows 32-bit application interface (Win32 API) contains more than 1000 complex functions and hundreds of Windows constants, messages, and structures, this allows programmers to develop applications running on Windows 95 and Windows NT operating systems using different programming languages. C and C ++ languages can access all Windows messages and usage, while VB cannot access all of them, however, by using Win32 APIs, you can extend VB to go beyond basic attributes, methods, and events, so that the program has more control and flexibility over the system.

Win32 APIs include the following four common function types.

1. Windows Management (User)

Provides basic window construction blocks and management program input display and retrieval of user input functions. You can use Windows Management Layer of Win32 API to create and manage user interfaces for applications. Windows Management supports Dynamic Data Exchange (DDE) and clipboard functions.

2. Graphics Device Interface (GDI)

Supports the functions of existing physical and logical devices in the system, such as the monitor, printer, and memory device description table. You can use GDI to define different drawing objects and provide the functions of drawing lines, circles, and other shapes to provide powerful support for bitmap operations.

3. System server (kernel)

Provides access to system resources and tools, including memory, file system, and Operation Processing. VB allows you to access and use a dynamic link library (DLL: Dynamic Link Libraries), such as an API or custom DLL. You can obtain the device parameters and change the working status under the control of the operating system.

4. Multimedia

Supports audio (WAV), Midi Music, AVI Video, game joystick, and high-precision timer. In vb5, It is very convenient to call Win32 APIs. Microsoft specially compiled the functions, type structure statements, and global constant values used in Win32 API functions to form the file WIN32API. TXT files are stored in the winapi subdirectory in the path of the VB Professional edition or Enterprise Edition. You can reference these sorted parameters in vb5 in two ways: (1) directly start the API text viewer in the vb5 folder; (2) use the "external program manager" in the vb5 editing environment to call the API text viewer. Microsoft sometimes modifies WIN32API. txt and publishes it to its outlets for users to download and use.

  

2. Obtain the path in Windows

When the application configures and calls basic parameters of the operating system, it must obtain the installation path for Windows, including the system path. Win32 API provides function calls to obtain the windows and system paths.

1. Obtain the Windows path

Declare function getwindowsdirectory lib "Kernel32" alias _

"Getwindowsdirectorya" (byval lpbuffer as string ,_

Byval nsize as long) as long

Getwindowsdirectory () is a function member of system server. It is used to obtain the path name of the Windows directory. getwindowsdirectorya is the alias of its function (alias ). In most cases, when an API function is declared, an alias is used to replace the real function name. The alias provides a method to call 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. Obtain the system path

Declare function getsystemdirectory lib "Kernel32" alias _

"Getsystemdirectorya" (byval lpbuffer as string ,_

Byval nsize as long) as long

Getsystemdirectory () is a function member of system server. It is used to obtain the path name of the Windows/system directory.

First, create a new project in vb5, add a module, and 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 variables in the path of windows and Windows/system.

Public windowsdir as string, windowssysdir as string

To prevent spaces in the strings returned by API function calls, it is necessary to process them and remove all spaces. Vb5 does not provide a function to remove spaces between characters, so we compile a custom function in module1 to complete this function.

'Alltrim () removes the white space character function from the string

Function alltrim $ (incoming $)

TEMP $ = incoming $

N % = instr (TEMP $, CHR $(0 ))

If n % then TEMP $ = left $ (TEMP $, N %-1)

TEMP $ = ltrim $ (rtrim $ (TEMP $ ))

Alltrim $ = TEMP $

End Function

Set up the process of obtaining the windows and system paths in module1:

'Winsysdir --- obtain the windows and Windows/system paths

Sub winsysdir ()

Windowsdir = space (144)

Windowssysdir = space (144)

If getwindowsdirectory (windowsdir, 144) = 0 or _

Getsystemdirectory (windowssysdir, 144) = 0 then

Msgbox "cannot locate the Windows path! ", 16," error"

Else

Windowsdir = alltrim $ (windowsdir)

Windowssysdir = alltrim $ (windowssysdir)

End if

End sub

Call winsysdir () in the form_load () event of form1 to obtain the name of the windows and system paths. The Demo code is as follows:

Winsysdir

Debug. Print windowsdir

Debug. Print windowssysdir

When running in the vb5 environment, the two paths are provided in the "immediate" window used for debugging.

  

3. Determination of drive Parameters

The getdrivetype () function in Win32 API is used to identify the drive type. This function has only one character parameter to specify the drive letter to be recognized. The Return Value of the getdrivetype () function is a long integer. Different values indicate different drive types. The specific correspondence is as follows:

2 --- floppy disk drive

3 --- hard drive

4 --- network drive

5 --- Optical Drive

6 --- memory drive

The getdiskfreespace () function of Win32 API can obtain the capacity parameters of the drive in the system.

Create a new project and declare the API function 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 drive

Private const drive_fixed = 3' fixed drive

Private const drive_remote = 4' network drive

Private const drive_cdrom = 5' disc drive

Private const drive_ramdisk = 6' memory analog drive

Add three label controls and a ComboBox control to form form1. Write the following code in the form_load () and combow.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 combow.click ()

Dim drivetype as long

Dim drivesize as 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 a network drive"

Case drive_cdrom

Label1.caption = "this is a CD-Rom drive"

Case drive_ramdisk

Label1.caption = "This Is A Memory Simulated drive"

Case else

Label1.caption = "this is invalid for this drive"

End select

Drivesize = getdiskfreespace (buff, sectorsc, bytess, totalc, numc)

If drivesize then

Totalc = totalc * sectorsc * bytess

Numc = numc * sectorsc * bytess

Label2.caption = "all space on the drive" + _

STR (Val (format $ (numc, "######### 0")/1024) + "Kbytes"

Label3.caption = "remaining space on the drive" + _

STR (Val (format $ (totalc, "######### 0")/1024) + "Kbytes"

Else

Label2.caption = ""

Label3.caption = ""

End if

End sub

Run this program and select a drive in the system from the drop-down list to obtain relevant parameters.

  

4. Obtain the type of the central processor

The getsysteminfo () function of Win32 API identifies the CPU type. First, declare the call to this function in the "common" section of form form1:

Private declare sub getsysteminfo lib "Kernel32" (lpsysteminfo as system-Info)

Because system-info is a type, you need to define it:

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)

Select case sysinfo. dwprocessortype

Case 1, 386

Label1.caption = "the machine's central processor type is 386"

Case 1, 486

Label1.caption = "the machine's central processor type is 486"

Case 1, 586

Label1.caption = "the machine's central processor type is Pentium"

Case else

Label1.caption = "unable to determine the machine's central processor type"

End select

  

5. Obtain memory information

The globalmemorystatus () of Win32 API can be used to conveniently obtain the total physical memory, total virtual memory, used virtual memory, and remaining virtual memory in the system.

Declare the call to the globalmemorystatus () function in the "common" section of form form1:

Private declare sub globalmemorystatus lib "Kernel32" (lpbuffer as memorystatus)

Define the 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 three label controls in form form1, label1, label2, and label3, and write the following code in the form_load () event:

Call globalmemorystatus (meminfo)

Label1.caption = "total physical memory:" + STR $ (meminfo. dwtotalphys) + "bytes"

Label2.caption = "Total virtual memory:" + STR $ (meminfo. dwtotalvirtual) + "bytes"

Label3.caption = "used virtual memory:" + STR $ (meminfo. dwavailphys) + "bytes"

Label4.caption = "remaining virtual memory:" + STR $ (meminfo. dwavailvirtual) + "bytes"

  

6. Get the resolution of the current display

The getsystemmetrics () function in Win32 API provides measurement parameters in this regard. Declare the call in the "common" section of the form:

Private declare function getsystemmetrics lib "USER32" (byval nindex as long) as long

Const sm_cxscreen = 0

Const sm_cyscreen = 1

Add a label control to the form and call the getsystemmetrics () function in the form_load () event to determine the resolution of the current display.

Dim X as string

Dim y as string

N = STR (getsystemmetrics (sm_cxscreen ))

Y = STR (getsystemmetrics (sm_cyscreen ))

Label1.caption = "current display resolution:" + N + "X" + Y

  

7. Call the msinfo32.exe Program

Microsoft provides the msinfo32.exe utility in the MS Office suite, it provides users with a comprehensive system, printing, DLL, Font, proofreading, graphics filter, Text Converter, display, audio, video, CD-Rom, Ole registry, activity module, input Method and other information. This program is generally stored in the/program files/Microsoft shared/msinfo path of the drive where windows is located. In VB Applications, this utility is generally called in the "about" window. Although the information cannot be directly applied, it increases the professionalism of the program.

To successfully call msinfo32.exe, the key is to obtain its path. This path cannot be directly obtained by Win32 API functions, and does not exist in the Windows configuration file, but only exists in the Windows registry. The office suite registers msinfo32.exe during installation and can be called by office components. The path information is stored in HKEY_LOCAL_MACHINE/software/Microsoft/shared tools/msinfo in the registry. Therefore, the problem of obtaining the path is converted to querying the registry.

Win32 API provides a series of functions to manipulate the registry. The regopenkeyex (), regqueryvalueex (), and regclosekey () functions are used here, it is used to open the Registry keyword, retrieve the Registry keyword value, and disable the Registry keyword. If regopenkeyex () and regqueryvalueex () return 0, it indicates that the Registry keyword is successfully opened and the predefined msifo32.exe keyword value is retrieved. Finally, the shell () function is called to start msinfo32.exe.

The vb5 Form Wizard provides a standard call process. Create a new project, select "delete form1" from the "project" menu, add a form, and select "about dialog box" in the displayed dialog box ", in this way, a customized standard "about" form is generated, in which msinfo32.exe is called and corresponding processing is set for running errors. Microsoft has made a detailed comment on the code, so I will not go into details here.

The Enterprise Edition of Visual Basic provides the Microsoft Developer Network (msdn) Startup toolbox. After installation, you can access the Win32 software development kit (SDK), which provides a file description for Win32 APIs, it helps programmers understand the working principles of every function, structure, and constant. For VB programmers, it is difficult to understand and use Windows APIs. It requires programmers to have a deep understanding of the working principles of windows. If they have a good understanding of how to use Windows APIs, in this way, the functions of Visual Basic can be greatly extended, allowing applications to have more in-depth and flexible control over the system.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.