Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to initialize a disk, partition and format it in NTFS or FAT32 format, but I encountered some problems. The code can initialize a disk and partition it, but after code execution the disk start sector data was wrong. Here is the code.
C++
int InitDisk(void)
{
	BOOL bResult = FALSE;     // generic results flag   
	CAutoFile hDevice;               // handle to the drive to be examined  
	DWORD junk = 0;                   // discard results 

	hDevice = CreateFile(TEXT("\\\\.\\PhysicalDrive0"), //"\\\\.\\PhysicalDrive1",  // drive to open 
		GENERIC_READ | GENERIC_WRITE,                // 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 
	{ 
		return GetLastError();
	}

	CREATE_DISK dsk; 
	dsk.PartitionStyle = PARTITION_STYLE_MBR;  
	dsk.Mbr.Signature = 9999;

         // Initialize disk
	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); 
	if (!bResult)
	{ 
		return GetLastError(); 
	}
	bResult = DeviceIoControl(hDevice, 
		IOCTL_DISK_UPDATE_PROPERTIES, 
		NULL, 0, NULL, 0, &junk, NULL);
	if (! bResult)
	{ 
		return GetLastError(); 
	}

	LARGE_INTEGER lgPartitionSize;
	lgPartitionSize.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);
	// set RewritePartition=true in every partition to force rewrite.   
	// 	for (int item = 0; item < 4; item++){
	// 		pdg->PartitionEntry[item].RewritePartition = 1;
	// 		pdg->PartitionEntry[item].PartitionNumber = 0;
	// 	}

	pdg->PartitionStyle = PARTITION_STYLE_MBR; 
	pdg->PartitionCount = 1;
	pdg->Mbr.Signature = 99999;

	pdg->PartitionEntry[0].PartitionStyle = PARTITION_STYLE_MBR;   
	pdg->PartitionEntry[0].StartingOffset.QuadPart = 32256;   // 63扇区
	pdg->PartitionEntry[0].PartitionLength.QuadPart = lgPartitionSize.QuadPart * 36;   
	pdg->PartitionEntry[0].PartitionNumber = 1;   
	pdg->PartitionEntry[0].RewritePartition = TRUE;   
	pdg->PartitionEntry[0].Mbr.PartitionType = PARTITION_NTFT; // PARTITION_IFS (NTFS partition or logical drive)   
	pdg->PartitionEntry[0].Mbr.BootIndicator = TRUE;
	pdg->PartitionEntry[0].Mbr.RecognizedPartition = 1;   
	pdg->PartitionEntry[0].Mbr.HiddenSectors = 32256 / 512;   

         // partition a disk
	bResult = DeviceIoControl(hDevice,        // device to be queried 
		IOCTL_DISK_SET_DRIVE_LAYOUT_EX,  // operation to perform 
		pdg, sizeof DRIVE_LAYOUT_INFORMATION_EX,        //sizeof(pdg),     // output buffer 
		NULL, 0,                // no output buffer 
		&junk,                    // # bytes returned 
		NULL); 
	if (!bResult)
	{ 
		return GetLastError(); 
	}
	bResult = DeviceIoControl(hDevice, 
		IOCTL_DISK_UPDATE_PROPERTIES, 
		NULL, 0, NULL, 0, &junk, NULL);
	if (!bResult)
	{ 
		return GetLastError(); 
	}

	PARTITION_INFORMATION_EX dskinfo; 
	PARTITION_INFORMATION_MBR mbrinfo; 
	mbrinfo.PartitionType = PARTITION_NTFT; 
	mbrinfo.HiddenSectors = (32256 / 512);
	mbrinfo.BootIndicator = 1;
	mbrinfo.RecognizedPartition = 1;

	dskinfo.PartitionStyle = PARTITION_STYLE_MBR; 
	dskinfo.StartingOffset.QuadPart = 32256;  
	dskinfo.PartitionLength.QuadPart = lgPartitionSize.QuadPart * 36; 
	dskinfo.PartitionNumber = 1; 
	dskinfo.RewritePartition = TRUE; 
	dskinfo.Mbr = mbrinfo; 
        // IOCTL_DISK_SET_PARTITION_INFO_EX can not perform successful
	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)
	{ 
		return GetLastError();
	} 

	return 0;
}

Hope that friends who have some experience can guide me!
Format the partition function does not call SHFormatDrive Shell API!
Posted
Updated 22-Feb-11 21:24pm
v3
Comments
Sandeep Mewara 23-Feb-11 2:43am    
Always format the code part using PRE tags. It makes the question readable.
Andrew Brock 23-Feb-11 3:24am    
A friendly tip:
\\.\PhysicalDrive0 is usually C:\. You probably don't want to format your C drive :D

1 solution

Have a look at the Win32_Volume class[^]. It has the Format[^] method.

Best regards
Espen Harlinn
 
Share this answer
 
Comments
fjdiewornncalwe 13-Mar-13 9:43am    
Espen.. Oops... Feb 23, 2011...
Espen Harlinn 13-Mar-13 11:56am    
?? I assume that date has a meaning in this context that I'm as of yet unaware of - ah, I see.
I didn't check the date, and it showed up on the first page of 'quick answers' today ...
Sergey Alexandrovich Kryukov 25-Apr-13 19:03pm    
That's right. Can be used anywhere where WMI can be used. My 5.
—SA
Espen Harlinn 25-Apr-13 19:05pm    
Thank you, Sergey :-D
swdev.bali 24-Apr-14 3:02am    
But, as I try to enumerate Win32_Volume(), it didn't display the newly created partition. It just display all volume with existing drive letter on it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900