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