Disk formatting programming implementation

xiaoxiao2021-03-06  66

Disk formatting programming implementation

Transfer from http://www.ccw.com.cn Beijing Machinery Industry College

Disk formatting is one of the operations frequently used by computer users, but due to the operating system's console and housing (shell) provides the formatted perfect support, making programmers easily ignoring the basic needs of the user's formatting. In their view, disk formatting is not a big problem for users, and the application does not have to support it. In fact, this is an illusion that when the user is running the application needs to format the disk, it is very inconvenient and cumbersome to format the disk or the resource manager.

There is also a part of the programmer that the disk format is easy to implement, there is no technical obstacle, so it is not given enough attention. In fact, the author discovered that the disk formatted is a problem that the disk is unclear. On the one hand, it requires the programming as simple as possible, do not involve interruption, port reading, physical sector access, VXD, WDM driver and other complex details, as long as it can be formatted, the more simple and better, preferably only Call a function can be implemented. On the other hand, the author finds that all programming reference books do not mention this problem, and it is difficult to find a formatDisk function with such a function in MSDN. Of course, we can use functions such as Winexec, CreateProcess, and Shellexecute to execute the format command, and this program is completely feasible, but the generated console window will make our window programs can't be classified, destroy the original program interface. Although we can also implement hidden consists of console window by setting the parameters of the CreateProcess function, make Format.exe hide in the background, but this requires us to maintain its full path, which also has certain difficulties.

Main function

From the perspective of practical applications, this formatDisk function should be provided from Windows 9x to Windows NT, otherwise how to implement the disk formatting of the resource manager window? Through carefully finding it, Windows does provide such a function, although documentation is not indicated, but if we use View Dependencies to open shell2.dll, you will find this function. It is shFormatDrive, compiled to shell32.lib. Although this function can be found through the platform's SDK document and shellapi.h file, the Win32 can find its trace. The application can implement a disk format dialog formatted disk formatting dialog box by calling this function. The call is agreed as follows:

DWORD WINAPI SHFORMATDRIVE (HWND HWND, UINT DRIVE, UINT FMTID, UINT OPTION);

The parameters are as follows:

HWND: Handle the window handle of the dialog, pay attention to the hwnd is NULL, does not cause this dialog to generate an upper application, that is, this parameter is always non-null, this dialog can only be used as a window window. And not as a separate application;

Drive: The drive letter to format, based on A: == 0, according to this type;

FMTID: Must be set to SHFMT_ID_DEFAULT, 0xffff;

Options: There are two selection items: shfmt_opt_full 0x0001 and SHFMT_OPT_SYSONLY 0x0002, if this is set to zero, the system will use fast format as the default setting.

The return value of this function either a value starting with SHFMT_ or a successful formatted disk ID. / / The last formatted error, the disk may be formatted

#Define shfmt_error 0xffffffl

/ / Formatted was canceled

#Define shfmt_cancel 0xfffffffffel

/ / Cannot be formatted

#Define shfmt_noformat 0xffffffdl

Implementation steps

1. Function interface declaration

TypeDef DWORD (WinApi * PfnshformatDrive) (HWND HWND, UINT DRIVE, UINT FMTID, UINT OPTION);

2. Load the shell32.dll library

Hinstance hinstance = loadingLibrary (_t

("Shell32.dll")));

IF (hinstance == null) return;

3. Get function pointers

Pfnshformatdrive PfnshFormatDrive = (

Pfnshformatdrive) getProcessAddress (_t

("SHFORMATDRIVE"));

IF (pfnshformatdrive == null)

{

Freelibrary (Hinstance);

Return;

}

4. Call the function

(Pfnshformatdrive) (hwnd, drive / * a: == 0 * /, fmtid / * shfmt_id_default * /, options);

5. Release handle resources

Freelibrary (Hinstance);

Return;

This program is debugged in the Windows 2000 environment. Using Delphi, users of Visual Basic can implement disk formatting by calling the above Windows SDK functions.

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

New Post(0)