Formatting partitions is a common operation in our opinion. You can click twice to solve the problem, but it is not easy to implement it in the program. Some people may say that it is not good to directly call the format command, but the system command contains the problem described in Section 1 http://cutebunny.blog.51cto.com/301216/624027.
I have found three methods for calculating the format command,
1. Windows API SHFormatDrive. The function prototype is as follows:
DWORD SHFormatDrive (
HWND hwnd,
UINT drive,
UINT fmtID,
UINT options
);
The drive parameter is the partition to be formatted and starts counting from 0. 0 represents A:, 2 represents C:, and so on. For other parameters, see MSDN. When you call this function, a dialog box that we are very familiar with is displayed. You can right-click it to format it.
The call method is simple:
SHFormatDrive (0, 8, SHFMT_ID_DEFAULT, SHFMT_OPT_FULL );
Unfortunately, this API cannot automatically start formatting. In any case, you must manually click Start ". Depressed, some people suggest you set up a timer and simulate Enter again. After formatting, Enter again. It works in principle, but it seems to be a bit nondescribable.
2. Use the undisclosed function FormatEx in FMIFS. dll
A Daniel named Mark Russinovich wrote the specific code. I haven't taken the time to tune it for your reference. For more information about the code, see the attachment.
Someone seems to have succeeded, please refer to the http://forum.sysinternals.com/creating-partitions-on-xp_topic3029.html
3. Format command
There is no way to solve this problem. The Code is as follows, which is very simple and I will not explain it much.
/*************************************** ***************************************
* Function:
* Input: disk, disk name
* Output: N/
* Return: Succeed, 0
* Fail, 1
**************************************** **************************************/
DWORD FormatVolume (CHAR letter)
{
DWORD ret;
CHAR cmd [64];
Sprintf (cmd, "format % c:/FS: NTFS/Q/Y", letter );
Ret = (DWORD) system (cmd );
Return ret;
}
Summary
The discussion on disk operation programming in windows has come to an end. Finally, let's summarize all the examples we have listed above.
DWORD GetAllPresentDisks (DWORD ** disks); // all physical disk numbers in the query system
DWORD GetSystemDiskPhysicalNumber (void); // obtain the physical disk Number of the Operating System
DWORD GetPhysicalDriveFromPartitionLetter (CHAR letter); // query the physical disk number based on the Logical Partition Number
DWORD GetDriveGeometry (const CHAR * disk, DISK_GEOMETRY * pdg); // query disk Physical Information
DWORD GetDiskDriveLayout (const CHAR * disk, DRIVE_LAYOUT_INFORMATION_EX * driveLayout); // query disk partition information
DWORD DestroyDisk (DWORD disk); // delete a disk partition table
DWORD CreateDisk (DWORD disk, WORD partNum); // initialize the disk and create a partition
DWORD GetPartitionLetterFromPhysicalDrive (DWORD pDriveNumber, CHAR ** letters); // query all partitions on a physical disk
DWORD FormatVolume (CHAR letter); // format the partition
Some of the above functions are quite dangerous and may immediately destroy the disk partition table. Please be careful when debugging. Of course, do not use it to do bad things.
This article is from the "bunny Technology Workshop" blog