Click here to Skip to main content
15,896,912 members

partition a raw usb disk

Member 9872030 asked:

Open original thread
Hi,

My aim is to create partition to the full disk size for a raw usb disk. The code snippet is as follows:

// usbpartition.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "windows.h"


int _tmain(int argc, _TCHAR* argv[])
{
bool bResult = false; // generic results flag
HANDLE hDevice; // handle to the drive to be examined
DWORD junk = 0; // discard results

hDevice = CreateFile(TEXT("\\\\.\\PhysicalDrive1"), //"\\\\.\\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; // lgPartitionSize.QuadPart * 36;
pdg->PartitionEntry[0].PartitionNumber = 1;
pdg->PartitionEntry[0].RewritePartition = TRUE;
pdg->PartitionEntry[0].Mbr.PartitionType = PARTITION_FAT32; // PARTITION_IFS (NTFS partition or logical drive)
pdg->PartitionEntry[0].Mbr.BootIndicator = false;
pdg->PartitionEntry[0].Mbr.RecognizedPartition = 1;
pdg->PartitionEntry[0].Mbr.HiddenSectors = 0; // 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_FAT32; //PARTITION_NTFT;
mbrinfo.HiddenSectors = 0; //(32256 / 512);
mbrinfo.BootIndicator = 0; //1;
mbrinfo.RecognizedPartition = 1;

dskinfo.PartitionStyle = PARTITION_STYLE_MBR;
dskinfo.StartingOffset.QuadPart = 32256;
dskinfo.PartitionLength.QuadPart = lgPartitionSize.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)
{
int out = GetLastError();
printf("output=%d\n",out);
// OutputDebugString(L"output",GetLastError());
return out;
}

return 0;
}

But the IOCTL IOCTL_DISK_SET_PARTITION_INFO_EX fails with error code 31. Please help me out whether my code is right..
Tags: VC++

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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