1

Topic: Format Partition to NTFS c++

I am an undergrad student developing a senior design project which encompasses the need to partition and format a hard disk drive. I am developing this in the c++ language and I have been stumped on formatting the partition. Maybe my source code to partition is done so incorrectly? I come to the GParted forums in hopes of assistance from experienced individuals.

The following is the source code that has been developed to partition an external HDD:

//-------------------INITIALIZE AND PARTITION HDD-------------------------------//
#include "stdafx.h"                                                                   //
#include <Windows.h>                                                            //
#include <stdio.h>                                                                  //
#include <iostream>                                                                //
#include <tchar.h>                                                                //
#include <winioctl.h>                                                            //
#include <shellapi.h>                                                            //
//-------------------------------------Main-------------------------------------//
int main()                                                                        //
{                                                                                //
    //char *dsk = "\\\\.\\PhysicalDrive1";                                        //
    HANDLE hDisk;                                                                //
    DWORD junk = 0;                                                                //
    BOOL bResult = false;                                                        //
                                                                                //
    hDisk = CreateFile(TEXT("\\\\.\\PhysicalDrive1"),                            //
        GENERIC_READ | GENERIC_WRITE,                                            //
        FILE_SHARE_READ | FILE_SHARE_WRITE,                                        //
        NULL,                                                                    //
        OPEN_EXISTING,                                                            //
        0,                                                                        //
        NULL);                                                                    //
                                                                                //
    //error handler for driver                                                    //
    if (hDisk == INVALID_HANDLE_VALUE)                                            //    
    {                                                                            //
        CloseHandle(hDisk);                                                        //
        return 1;                                                                //
    }                                                                            //
                                                                                //
                                                                                //
    CREATE_DISK dsk;                                                            //
    dsk.PartitionStyle = PARTITION_STYLE_MBR;                                    //
    dsk.Mbr.Signature = 9999;                                                    //
                                                                                //
//-------------------------------Initialize Disk--------------------------------//
    bResult = DeviceIoControl(hDisk,    //initialize raw disk                    //
        IOCTL_DISK_CREATE_DISK,                                                    //
        &dsk, sizeof(dsk),                                                        //
        NULL, 0,                                                                //
        &junk,                                                                    //
        NULL);                                                                    //
                                                                                //
    if (!bResult)                                                                //
    {                                                                            //
        return GetLastError();                                                    //
    }                                                                            //
                                                                                //
    bResult = DeviceIoControl(hDisk,    //to flush changes                        //
        IOCTL_DISK_UPDATE_PROPERTIES,                                            //
        NULL, 0, NULL, 0, &junk, NULL);                                            //
                                                                                //
    if (!bResult)                                                                //
    {                                                                            //
        return GetLastError();                                                    //
    }                                                                            //
                                                                                //
                                                                                //
    //----------------------------Partition Disk--------------------------------//
    LARGE_INTEGER lgPartSize;                                                    //
                                                                                //
    lgPartSize.QuadPart = (1024 * 1024 * 1024);                                    //
                                                                                //
    DWORD dwDriverLayoutInfoExLen = sizeof(DRIVE_LAYOUT_INFORMATION_EX) + 3 *    //
        sizeof(PARTITION_INFORMATION_EX);                                        //
                                                                                //
    DRIVE_LAYOUT_INFORMATION_EX *pdg = (DRIVE_LAYOUT_INFORMATION_EX *)new        //
        BYTE[dwDriverLayoutInfoExLen];                                            //
                                                                                //
    if (pdg == NULL)                                                            //
    {                                                                            //
        return -1;                                                                //
    }                                                                            //
                                                                                //
    SecureZeroMemory(pdg, dwDriverLayoutInfoExLen);                                //
                                                                                //
    pdg->PartitionStyle = PARTITION_STYLE_MBR;                                    //
    pdg->PartitionCount = 1;                                                    //
    pdg->Mbr.Signature = 99999;                                                    //
                                                                                //
    pdg->PartitionEntry[0].PartitionStyle = PARTITION_STYLE_MBR;                //
    pdg->PartitionEntry[0].StartingOffset.QuadPart = 3225                        //
    pdg->PartitionEntry[0].PartitionLength.QuadPart = lgPartSize.QuadPart * 40; // <-- partition size in GB
    pdg->PartitionEntry[0].PartitionNumber = 1;                                    //
    pdg->PartitionEntry[0].RewritePartition = TRUE;                                //
    pdg->PartitionEntry[0].Mbr.PartitionType = PARTITION_NTFT;                    //    
    pdg->PartitionEntry[0].Mbr.BootIndicator = TRUE;                            //
    pdg->PartitionEntry[0].Mbr.RecognizedPartition = 1;                            //
    pdg->PartitionEntry[0].Mbr.HiddenSectors = 32256 / 512;                        //
                                                                                //
    bResult = DeviceIoControl(hDisk,            //device to be queried            // 
        IOCTL_DISK_SET_DRIVE_LAYOUT_EX,            //operation to perform            //  
        pdg, sizeof DRIVE_LAYOUT_INFORMATION_EX,//sizeof(pdg),                    // 
        NULL, 0,                                // output buffer                //  
        &junk,                                    //# bytes returned                //  
        NULL);                                                                    //
                                                                                //
    if (!bResult)                                                                //
    {                                                                            //
        return GetLastError();                                                    //
    }                                                                            //
                                                                                //
                                                                                //
     CloseHandle(hDisk);
     return 0;                                                                           //            
     }                                                                           //
    //--------------------------------------------------------------------------//

After executing the code the partition of size 40GB is examined using Windows Disk Management:
http://i46.photobucket.com/albums/f115/gcedil/partition_zps64816652.jpg

I figured I would be directed to Windows API:

uint32 Format(
  [in]  string FileSystem = "NTFS",
  [in]  boolean QuickFormat,
  [in]  uint32 ClusterSize = 4096,
  [in]  string Label = "",
  [in]  boolean EnableCompression = false
);

and

DWORD SHFormatDrive(
  _In_  HWND hwnd,
  UINT drive,
  UINT fmtID,
  UINT options
);

but I am not sure how to implement these functions to my existing code. I appreciate any feedback in advanced. Thank you for your time.

2

Re: Format Partition to NTFS c++

GParted is developed under GNU/Linux using the libparted library.  For Windows coding support I recommend you contact a Windows programming forum.