How can format a drive ?

Hi,

I am writing a program to format the drive in NTFS FS.
I searched on net and got a code like this…

CREATE_DISK dsk;
CREATE_DISK_MBR dskmbr;
dskmbr.Signature = 1;
dsk.PartitionStyle = PARTITION_STYLE_MBR;
dsk.Mbr = dskmbr;

DRIVE_LAYOUT_INFORMATION_EX pdg;
DRIVE_LAYOUT_INFORMATION_MBR mbrlayout;
mbrlayout.Signature = 1234;
pdg.PartitionStyle = PARTITION_STYLE_MBR;
pdg.Mbr = mbrlayout;

LARGE_INTEGER lint;
LARGE_INTEGER lint2;
lint.QuadPart=cl_list[19].expected_bytes;
lint2.QuadPart = 0;
PARTITION_INFORMATION_EX dskinfo;
PARTITION_INFORMATION_MBR mbrinfo;
mbrinfo.PartitionType = PARTITION_NTFS;
mbrinfo.HiddenSectors = 1;
dskinfo.PartitionStyle = PARTITION_STYLE_MBR;
dskinfo.StartingOffset = lint2;
dskinfo.PartitionLength = lint;
dskinfo.PartitionNumber = 2;
dskinfo.RewritePartition = TRUE;
dskinfo.Mbr = mbrinfo;

bool bResult=FALSE; // generic results flag
HANDLE hDevice; // handle to the drive
to be examined
DWORD junk; // discard results

hDevice = CreateFile(driver,//“\\.\PhysicalDrive1”,
// drive to open
0, // no access
to the drive
FILE_SHARE_READ | // share
mode
FILE_SHARE_WRITE,
NULL, // default
security attributes
OPEN_EXISTING, //
disposition
0, // file
attributes
NULL); // do not
copy file attributes
if (hDevice == INVALID_HANDLE_VALUE) // cannot
open the drive
{
error[dr] =
ERROR_CODE_CANNOT_OPEN_THE_DRIVE;
AbnormalUnplugDevice=true;
sprintf(Buf, “ERROR,
CMTA178_PLUSDlg::Get_geom INVALID_HANDLE_VALUE
(%s”,driver);
if((m_CK_OVERNIGHT.GetCheck()!=TRUE))
AfxMessageBox(Buf);
else
PrintMSG_RESULT(“Geometry Test”,Buf);

}else{

sprintf(Buf,“Trying first IOCTRL \r\n”);
PrintMSG(Buf);
bResult = DeviceIoControl(hDevice,
// device to be queried

IOCTL_DISK_SET_DRIVE_LAYOUT_EX, // operation to
perform
&pdg, sizeof(pdg),
//sizeof(pdg), // output buffer
NULL, 0, // no
output buffer
&junk, // #
bytes returned
NULL);

//DWORD tmp = GetLastError();
if (!bResult){
sprintf(Buf,“RESULT %d\r\n”,
GetLastError());
PrintMSG(Buf);
}
int ret = Check_write_protection(dr);
bResult = DeviceIoControl(hDevice,
// device to be queried
IOCTL_DISK_CREATE_DISK, //
operation to perform
&dsk, sizeof(dsk),
//sizeof(pdg), // output buffer
NULL, 0, // no
output buffer
&junk, // #
bytes returned
NULL);
// &olap); //
synchronous I/O

//tmp = GetLastError();
if (!bResult){
sprintf(Buf,“RESULT %d\r\n”,
GetLastError());
PrintMSG(Buf);
}}
bResult = DeviceIoControl(hDevice,
// device to be queried

IOCTL_DISK_SET_PARTITION_INFO_EX, // operation to
perform
&dskinfo, sizeof(dskinfo),
//sizeof(pdg), // output buffer
NULL, 0, // no
output buffer
&junk, // #
bytes returned
//(LPOVERLAPPED)
NULL);
if (!bResult){
sprintf(Buf,“RESULT %d\r\n”,
GetLastError());
PrintMSG(Buf);
}
}
CloseHandle(hDevice);


can some one tell me about a complete source cod to
format a drive of a disk.

thanks in advance,
Regards,
rakesh


Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

Well you might take a look at
http://www.sysinternals.com/ntw2k/source/fmifs.shtml


Don Burn (MVP, Windows DDK)
Windows 2k/XP/2k3 Filesystem and Driver Consulting
Remove StopSpam from the email to reply

“Rakesh Yadav” wrote in message
news:xxxxx@ntdev…
> Hi,
>
> I am writing a program to format the drive in NTFS FS.
> I searched on net and got a code like this…
> ---------------------------------------------------
> CREATE_DISK dsk;
> CREATE_DISK_MBR dskmbr;
> dskmbr.Signature = 1;
> dsk.PartitionStyle = PARTITION_STYLE_MBR;
> dsk.Mbr = dskmbr;
>
> DRIVE_LAYOUT_INFORMATION_EX pdg;
> DRIVE_LAYOUT_INFORMATION_MBR mbrlayout;
> mbrlayout.Signature = 1234;
> pdg.PartitionStyle = PARTITION_STYLE_MBR;
> pdg.Mbr = mbrlayout;
>
> LARGE_INTEGER lint;
> LARGE_INTEGER lint2;
> lint.QuadPart=cl_list[19].expected_bytes;
> lint2.QuadPart = 0;
> PARTITION_INFORMATION_EX dskinfo;
> PARTITION_INFORMATION_MBR mbrinfo;
> mbrinfo.PartitionType = PARTITION_NTFS;
> mbrinfo.HiddenSectors = 1;
> dskinfo.PartitionStyle = PARTITION_STYLE_MBR;
> dskinfo.StartingOffset = lint2;
> dskinfo.PartitionLength = lint;
> dskinfo.PartitionNumber = 2;
> dskinfo.RewritePartition = TRUE;
> dskinfo.Mbr = mbrinfo;
>
> bool bResult=FALSE; // generic results flag
> HANDLE hDevice; // handle to the drive
> to be examined
> DWORD junk; // discard results
>
> hDevice = CreateFile(driver,//“\\.\PhysicalDrive1”,
> // drive to open
> 0, // no access
> to the drive
> FILE_SHARE_READ | // share
> mode
> FILE_SHARE_WRITE,
> NULL, // default
> security attributes
> OPEN_EXISTING, //
> disposition
> 0, // file
> attributes
> NULL); // do not
> copy file attributes
> if (hDevice == INVALID_HANDLE_VALUE) // cannot
> open the drive
> {
> error[dr] =
> ERROR_CODE_CANNOT_OPEN_THE_DRIVE;
> AbnormalUnplugDevice=true;
> sprintf(Buf, “ERROR,
> CMTA178_PLUSDlg::Get_geom INVALID_HANDLE_VALUE
> (%s”,driver);
> if((m_CK_OVERNIGHT.GetCheck()!=TRUE))
> AfxMessageBox(Buf);
> else
> PrintMSG_RESULT(“Geometry Test”,Buf);
>
> }else{
>
> sprintf(Buf,“Trying first IOCTRL \r\n”);
> PrintMSG(Buf);
> bResult = DeviceIoControl(hDevice,
> // device to be queried
>
> IOCTL_DISK_SET_DRIVE_LAYOUT_EX, // operation to
> perform
> &pdg, sizeof(pdg),
> //sizeof(pdg), // output buffer
> NULL, 0, // no
> output buffer
> &junk, // #
> bytes returned
> NULL);
>
> //DWORD tmp = GetLastError();
> if (!bResult){
> sprintf(Buf,“RESULT %d\r\n”,
> GetLastError());
> PrintMSG(Buf);
> }
> int ret = Check_write_protection(dr);
> bResult = DeviceIoControl(hDevice,
> // device to be queried
> IOCTL_DISK_CREATE_DISK, //
> operation to perform
> &dsk, sizeof(dsk),
> //sizeof(pdg), // output buffer
> NULL, 0, // no
> output buffer
> &junk, // #
> bytes returned
> NULL);
> // &olap); //
> synchronous I/O
>
> //tmp = GetLastError();
> if (!bResult){
> sprintf(Buf,“RESULT %d\r\n”,
> GetLastError());
> PrintMSG(Buf);
> }}
> bResult = DeviceIoControl(hDevice,
> // device to be queried
>
> IOCTL_DISK_SET_PARTITION_INFO_EX, // operation to
> perform
> &dskinfo, sizeof(dskinfo),
> //sizeof(pdg), // output buffer
> NULL, 0, // no
> output buffer
> &junk, // #
> bytes returned
> //(LPOVERLAPPED)
> NULL);
> if (!bResult){
> sprintf(Buf,“RESULT %d\r\n”,
> GetLastError());
> PrintMSG(Buf);
> }
> }
> CloseHandle(hDevice);
>
> ----------------------------------------------
> can some one tell me about a complete source cod to
> format a drive of a disk.
>
> thanks in advance,
> Regards,
> rakesh
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
>

http://linux-ntfs.sourceforge.net/

Matt

“Rakesh Yadav” wrote: xxxxx@ntdev…
> Hi,
>
> I am writing a program to format the drive in NTFS FS.
> I searched on net and got a code like this…
> ---------------------------------------------------
> CREATE_DISK dsk;
> CREATE_DISK_MBR dskmbr;
> dskmbr.Signature = 1;
> dsk.PartitionStyle = PARTITION_STYLE_MBR;
> dsk.Mbr = dskmbr;
>
> DRIVE_LAYOUT_INFORMATION_EX pdg;
> DRIVE_LAYOUT_INFORMATION_MBR mbrlayout;
> mbrlayout.Signature = 1234;
> pdg.PartitionStyle = PARTITION_STYLE_MBR;
> pdg.Mbr = mbrlayout;
>
> LARGE_INTEGER lint;
> LARGE_INTEGER lint2;
> lint.QuadPart=cl_list[19].expected_bytes;
> lint2.QuadPart = 0;
> PARTITION_INFORMATION_EX dskinfo;
> PARTITION_INFORMATION_MBR mbrinfo;
> mbrinfo.PartitionType = PARTITION_NTFS;
> mbrinfo.HiddenSectors = 1;
> dskinfo.PartitionStyle = PARTITION_STYLE_MBR;
> dskinfo.StartingOffset = lint2;
> dskinfo.PartitionLength = lint;
> dskinfo.PartitionNumber = 2;
> dskinfo.RewritePartition = TRUE;
> dskinfo.Mbr = mbrinfo;
>
> bool bResult=FALSE; // generic results flag
> HANDLE hDevice; // handle to the drive
> to be examined
> DWORD junk; // discard results
>
> hDevice = CreateFile(driver,//“\\.\PhysicalDrive1”,
> // drive to open
> 0, // no access
> to the drive
> FILE_SHARE_READ | // share
> mode
> FILE_SHARE_WRITE,
> NULL, // default
> security attributes
> OPEN_EXISTING, //
> disposition
> 0, // file
> attributes
> NULL); // do not
> copy file attributes
> if (hDevice == INVALID_HANDLE_VALUE) // cannot
> open the drive
> {
> error[dr] =
> ERROR_CODE_CANNOT_OPEN_THE_DRIVE;
> AbnormalUnplugDevice=true;
> sprintf(Buf, “ERROR,
> CMTA178_PLUSDlg::Get_geom INVALID_HANDLE_VALUE
> (%s”,driver);
> if((m_CK_OVERNIGHT.GetCheck()!=TRUE))
> AfxMessageBox(Buf);
> else
> PrintMSG_RESULT(“Geometry Test”,Buf);
>
> }else{
>
> sprintf(Buf,“Trying first IOCTRL \r\n”);
> PrintMSG(Buf);
> bResult = DeviceIoControl(hDevice,
> // device to be queried
>
> IOCTL_DISK_SET_DRIVE_LAYOUT_EX, // operation to
> perform
> &pdg, sizeof(pdg),
> //sizeof(pdg), // output buffer
> NULL, 0, // no
> output buffer
> &junk, // #
> bytes returned
> NULL);
>
> //DWORD tmp = GetLastError();
> if (!bResult){
> sprintf(Buf,“RESULT %d\r\n”,
> GetLastError());
> PrintMSG(Buf);
> }
> int ret = Check_write_protection(dr);
> bResult = DeviceIoControl(hDevice,
> // device to be queried
> IOCTL_DISK_CREATE_DISK, //
> operation to perform
> &dsk, sizeof(dsk),
> //sizeof(pdg), // output buffer
> NULL, 0, // no
> output buffer
> &junk, // #
> bytes returned
> NULL);
> // &olap); //
> synchronous I/O
>
> //tmp = GetLastError();
> if (!bResult){
> sprintf(Buf,“RESULT %d\r\n”,
> GetLastError());
> PrintMSG(Buf);
> }}
> bResult = DeviceIoControl(hDevice,
> // device to be queried
>
> IOCTL_DISK_SET_PARTITION_INFO_EX, // operation to
> perform
> &dskinfo, sizeof(dskinfo),
> //sizeof(pdg), // output buffer
> NULL, 0, // no
> output buffer
> &junk, // #
> bytes returned
> //(LPOVERLAPPED)
> NULL);
> if (!bResult){
> sprintf(Buf,“RESULT %d\r\n”,
> GetLastError());
> PrintMSG(Buf);
> }
> }
> CloseHandle(hDevice);
>
> ----------------------------------------------
> can some one tell me about a complete source cod to
> format a drive of a disk.
>
> thanks in advance,
> Regards,
> rakesh
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
>

Don,

you wrote on Friday, February 4, 2005, 14:35:20:

DB> Well you might take a look at
DB> http://www.sysinternals.com/ntw2k/source/fmifs.shtml

Right. BTW, any from Microsoft cares to explain why there’s no
officially documented Win32 API for something as simple as formatting a
volume (I know SHFormatDrive() is documented but that just launches a
dialog)? I mean, all the disk partitioning related stuff is documented
but not the final step necessary to create an usable partition
programmatically. Just curious.


Ralf.