Use undisclosed functions to write disk formatting applications

Source: Internet
Author: User
If you want to add the format (Format) function of the operating system to your application, like the format utility of the system, what should you do? If you search by Google, the most popular Chinese document is "disk formatting programming implementation", which uses the shformatdrive function to call the system's formatting tool to complete the Formatting Function. Is there a way to implement it using your own code? Refer to an article in sysinternals and try to use fmifs. an undisclosed function in DLL implements the format function. All parameters are set by yourself, such as the size of each cluster and waiting by the file system. Here I only demonstrated the most basic functions. If you want to make the program more robust, you still need to do more work.

Formatex function prototype:

Void formatex (pwchar driveroot,
DWORD mediaflag,
Pwchar format,
Pwchar label,
Bool quickformat,
DWORD clustersize,
Pfmifscallback callback );

Fmifscallback:

Typedef Boolean (_ stdcall * pfmifscallback )(
Callbackcommand command,
DWORD subaction,
Pvoid actioninfo );

The specific work is as follows:
First define a header file fmifs. h:
// ================================================ ======================================
//
// Fmifs. h
//
// ================================================ ======================================

//
// Output command
//
Typedef struct {
DWORD lines;
Pchar output;
} Textoutput, * ptextoutput;

//
// Callback command types
//
Typedef Enum {
Progress,
Donewithstructure,
Unknown2,
Unknown3,
Unknown4,
Unknown5,
Insufficientrights,
Unknown7,
Unknown8,
Unknown9,
Unknowna,
Done,
Unknownc,
Unknownd,
Output,
Structureprogress
} Callbackcommand;

//
// Fmifs callback Definition
//
Typedef Boolean (_ stdcall * pfmifscallback) (callbackcommand command, DWORD subaction, pvoid actioninfo );

//
// Chkdsk command in fmifs
//
Typedef void (_ stdcall * pchkdsk) (pwchar driveroot,
Pwchar format,
Bool correcterrors,
Bool verbose,
Bool checkonlyifdirty,
Bool scandrive,
Pvoid unused2,
Pvoid unused3,
Pfmifscallback callback );

//
// Format command in fmifs
//

// Media flags
# Define fmifs_harddisk 0xc
# Define fmifs_floppy 0x8

Typedef void (_ stdcall * pformatex) (pwchar driveroot,
DWORD mediaflag,
Pwchar format,
Pwchar label,
Bool quickformat,
DWORD clustersize,
Pfmifscallback callback );

//
// Enable/disable volume Compression
//
Typedef Boolean (_ stdcall * penablevolumecompression) (pwchar driveroot,
Bool enable );
//////////////////////////////////////// //////////////////////////////////

The following is the main file:
# Include "stdafx. H"
# Include <windows. h>
# Include <stdio. h>
# Include "fmifs. H"
# DEFINE _ Unicode 1
# Include "tchar. H"

Bool error = false;

Bool quickformat = false;
DWORD clustersize = 0;
Bool compressdrive = false;
Bool gotalabel = false;
Pwchar label = l "";
Pwchar drive = NULL;
Pwchar format = l "FAT32 ";

Wchar rootdirectory [max_path];
Wchar labelstring [12];

Pformatex formatex;
Penablevolumecompression enablevolumecompression;

Typedef struct {
Wchar sizestring [16];
DWORD clustersize;
} Sizedefinition, * psizedefinition;

Sizedefinition legalsizes [] = {
{L "512", 512 },
{L "1024", 1024 },
{L "2048", 2048 },
{L "4096", 4096 },
{L "8192", 8192 },
{L "16 K", 16384 },
{L "32 KB", 32768 },
{L "64 kB", 65536 },
{L "128 K", 65536*2 },
{L "256 k", 65536*4 },
{L "", 0 },
};

//----------------------------------------------------------------------
//
// Formatexcallback
//
// The file system library will call us back with commands that we
// Can interpret. If we wanted to halt the chkdsk we cocould return false.
//
//----------------------------------------------------------------------
Boolean _ stdcall formatexcallback (callbackcommand command, DWORD modifier, pvoid argument)
{
Pdword percent;
Ptextoutput output;
Pboolean status;
Static createstructures = false;

//
// We get other types of commands, but we don't have to pay attention to them
//
Switch (command ){

Case progress:
Percent = (pdword) argument;
_ Tprintf (L "% d percent completed./R", * percent );
Break;

Case output:
Output = (ptextoutput) argument;
Fprintf (stdout, "% s", output-> output );
Break;

Case done:
Status = (pboolean) argument;
If (* status = false ){

_ Tprintf (L "formatex was unable to complete successfully./n ");
Error = true;
}
Break;
}
Return true;
}

//----------------------------------------------------------------------
//
// Loadfmifsentrypoints
//
// Loads fmifs. dll and locates the entry point (s) we are going to use
//
//----------------------------------------------------------------------
Boolean loadfmifsentrypoints ()
{
Loadlibrary ("fmifs. dll ");

If (! (Formatex = (pformatex) getprocaddress (getmodulehandle ("fmifs. dll "),
"Formatex "))){

Return false;
}

If (! (Enablevolumecompression = (penablevolumecompression) getprocaddress (getmodulehandle ("fmifs. dll "),
"Enablevolumecompression "))){

Return false;
}
Return true;
}

Int main (INT argc, wchar * argv [])
{
If (argv [1] [1]! = L': ') return 0;
Drive = argv [1];

Wcscpy (rootdirectory, drive );
Rootdirectory [2] = l '//';
Rootdirectory [3] = (wchar) 0;

DWORD media;
DWORD drivetype;

Drivetype = getdrivetypew (rootdirectory );

If (drivetype! = Drive_fixed)
Media = fmifs_floppy;
If (drivetype = drive_fixed)
Media = fmifs_harddisk;

//
// Get function pointers
//
If (! Loadfmifsentrypoints ()){

_ Tprintf (L "cocould not located fmifs entry points./n ");
Return-1;
}

Formatex (rootdirectory, media, format, label, quickformat,
Clustersize, formatexcallback );
 
Return 0;
}

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.