How to detect hardware in C ++ Builder

zhaozj2021-02-17  52

In the process we have written, it is often dealing with hardware, so how is the device in the system in the program, what is its running status? For beginners, this problem is often unresolved, in fact, just use several API functions, hardware problems are not mysterious. Let's take a look at how to detect hardware in C Build ER.

1. Detect the model of the CPU

Let us take a look at your own CPU model first. First, in C Builder, the form shown in Figure 1 is drawn. In the following examples we will always use this form as demonstration, which includes a METON for activating the test and a Memo for displaying results. . We can use the getSystemInfo this API to get the model number of the CPU. Adding the following code to the Button's Click event:

Void __fastcall tform1 :: button1click (Tobject * Sender)

{

// Get the CPU model

System_info systeminfo;

GetSystemInfo (& SystemInfo);

Memo1 → Lines → Add ("Your CPU type is:" String (SystemInfo.dwprocessORTY PE);

}

Run it, click TEST to try, the CPU model is coming out!

2. Detect memory status

The method of obtaining a memory state is similar to the CPU model, but he uses another API: Globalme MoryStatus. Among them, members dwtotalphys are used to get the total amount of physical memory, and DWAVAILPHYS is the meaning of effective physical memory. As long as we add the following lines of code to the above program (do not need to be done, the same):

// Get memory status

MemoryStatus memory;

Memory.dwlength = sizeof (memory); // Initialization

Memory.dwlength = sizeof (memory); // Initialization

GlobalMemoryStatus (& Memory);

Memo1 → Lines → Add ("Your physical memory is (MB):" String (int (Memory.dwtotalph YS / 1024/1024)))

Memo1 → Lines → Add ("where available memory is (KB):" String (int (Memory. / 1024)))

How, look at the door? The format of the two programs is almost exactly the same. In fact, GetSysteminf O and GlobalMemoryStatus can also get many other information about CPU and memory, follow the formats above, more detailed information can go to C Builder4 Help.

3. Detect the available hard disk space

Ok, after the warm-up of the two simple questions, let's deal with a slightly complicated issue: We know that the installation program has a process of detecting the hard disk space, then this is achieved? What is the API letter? Number GetDiskFreespace, this function enters a parameter: the path to the target disk; returns four parameters, the number of sectors per cluster, the number of bytes per sector, the number of idle clusters, total clusters. If we need to detect the total capacity and available capacity of the C drive, then the following code can be added to the above program:

// Get the C disk available space

DWORD SECTOR, BYTE, Cluster, Free;

Long Int FreeSpace, Totalspace;

GetDiskFreespace ("C:", & Sector, & Byte, & Free, & Cluster); // Get Returns Totalspace = INT (Cluster) * INT (Byte) * INT (SECTOR) / 1024/1024; // Computing total capacity free atpace = INT (Free) * INT * INT (SECTOR) / 1024/1024; // Calculates available space

Memo1 → Lines → Add ("C disk total space (MB):" String (Totalspace));

Memo1 → Lines → Add ("C disk can be available space (MB):" String (FreeSpace);

How? I can do my own installation now!

4. Detect CD-ROM

We often need to read the CD-ROM when writing a program, but which one disk is the optical drive? Someone is a tangent to towering. Putting the last drive as a CD-ROM, but it often errors when encountering a double-driven drive or MO. In fact, this problem is to solve it with an API. This is: getDriveType (), this function returns a value between 0 ~ 6, represents: 0- Unknown Disk, 1- does not exist, 2-removable disk, 3- Fixed disk, 4-network disk, 5-CD-ROM, 6-memory virtual disk. So we can add the following code to find CD-ROM:

// Get CD-ROM information

Uint type;

CHAR NAME;

For (Name = 'C'; Name <= 'Z'; Name ) // Cycle Detection A ~ Z

{Type = getDriveType ((String (Name) String (':')). C_str ()); // Get a disk type

IF (Type == 5)

Memo1 → Lines → Add ("Your light drive letter is:" string (name));

}

After getting the optical drive, we can further use the API function getVolumeInformation to detect if there is a disc in the optical drive. If the function is successfully called, it will get information such as the scroll sequence number of the disk; if the call failed, it is known that the optical drive is as follows: / / Detect CD (assuming the optical drive is g :)

Char Volname [255], FileName [100]; // Buffer [512];

DWORD SNO, MAXL, FILEFLAG;

IF (! ("G:", Volname, 255, & Sn, & Maxl, & Fileflag, FileName, 100)))

// If the return value is false

Memo1 → Lines → Add ("no disc is found in G Drive");

Else

// If the return value is true

{MEMO1 → LINES → Add ("G drive disc coal:" string (volume));

Memo1 → Lines → Add ("G drive disc serial number:" String (SNO));

}

5. Detect sound card configuration

When preparing multimedia programs, we often use sound files, and when these programs are running on machines that do not configure the sound card, we should give the necessary warnings. For detection of sound cards, the waveform devices and MIDI devices can be detected via WaveoutG etNumdevs () and MIDIOUTGETNUMDEVS (), and the details of the sound device will be obtained by WaveoutGetDevC APS () and MIDIOUTGETDEVCAPS (). Add the following code to the above program, but pay attention to add #include to the program header: // Detect sound card

INT Wavedevice, Mididevice;

Waveoutcaps Wavecap;

Midioutcaps Midicap;

WaveDevice = (int) WaveoutGetNumdevs (); // Waveform device information mididevice = (int) midiOutGetNumdevs (); // MIDI device information mididevice = (int) midiOutGetNumdevs (); // MIDI device information

IF (WaveDevice == 0)

Memo1 → Lines → Add ("No Walte Device");

Else

{WaveOutgetDevcaps (0, & Wavecap, Sizeof (WaveoutCaps));

Memo1 → Lines → Add ("Current Waveform Device:" String (Wavecap.szpname);

}

IF (Mididevice == 0)

Memo1 → Lines → Add ("No MIDI device found);

Else

{MIDIOUTGETDEVCAPS (0, & Midicap, SizeOf (MidioutCaps));

MEMO19 → Lines → Add ("Current MIDI Device:" String (Midicap.Szpname);

}

6. Detect display information

When writing and graphically related programs, you often need to detect the resolution of the display and color depth, and finally let's take a look at this problem. The resolution is simple, and the properties of the Screen object are called directly. The requirements of the requirement need to obtain the number of bits and colors of each pixel using the API function getDeviceCaps, and then calculate 2 "Bit number of each pixel" power to the gradient of color, then calculate the "color gradient number" "The number of colors" power is deep. Since the paragraph 蛴 蛴 菰 菰 菰   砸   由 希 希 希 希 希 希 希 希 希 希 希 希 希 希 希 希 希 希 希 希

// Detect the display

Int TCS;

Long int BPP, CP, TC;

Memo1 → Lines → Add ("Current resolution is:" String (Screen → width * " S TRING (Screen → height));

BPP = getDeviceCaps (Form1 → Canvas → Handle, Bitspixel);

BPP = getDeviceCaps (Form1 → Canvas → Handle, Bitspixel);

TCS = POW (2, bpp); // Calculate the gradient number of colors

CP = getDeviceCaps (Form1 → Canvas → Handle, Plaso);

Tc = POW (TCS, CP); // Calculating color depth

Memo1 → Lines → Add ("The current color is:" String (TC));

Ok, now let's click Test, the hardware situation is completed (Figure 2)! In fact, the features of the API functions involved in this article are more than these, and everyone can check the Win32 API manual, or see Help directly in C Builder 4. I believe that I have developed a hardware detection software is not difficult! All the above programs are in the Windows98 Chinese version, C Builder 4 debug passes.

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

New Post(0)