Disk operations for windows 6-obtain the physical disk NUMBER OF THE SYSTEM

Source: Internet
Author: User

 

In many cases, we need to know in the program which partition or physical disk the operating system is installed on, and then perform some operations or avoid some operations on it. For example, to avoid deletion of the system disk partition table. This section describes how to obtain the logical Partition Number and physical drive Number of the operating system.

Or code first.

/*************************************** ***************************************

* Function: get the number of disks which the system installed on

* Input: N/

* Output: N/

* Return: Succeed, disk number

* Fail,-1

**************************************** **************************************/

DWORD GetSystemDiskPhysicalNumber (void)

{

CHAR sysPath [DISK_PATH_LEN];

CHAR diskLetter;

DWORD diskNumber;

DWORD ret = GetSystemDirectory (sysPath, sizeof (sysPath ));

If (ret = 0)

{

Fprintf (stderr, "GetSystemDirectory () Error: % ld \ n", GetLastError ());

Return (DWORD)-1;

}

DiskLetter = sysPath [0];

DiskNumber = GetPhysicalDriveFromPartitionLetter (diskLetter );

Return diskNumber;

}

 

Code Analysis:

1. Call the GetSystemDirectory function to obtain the windows path. The path stored in sysPath is C: \ WINDOWS \ system32.

2. Extract the first character of the sysPath string to obtain the partition drive letter of the system.

3. Based on the logical partition number, call the GetPhysicalDriveFromPartitionLetter function discussed in section 4 to obtain the physical disk Number of the operating system.

This article is from the "bunny Technology Workshop" blog

Related Article

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.