partitioning USB Falsh drive
I have written some code to make two partitions in USB flash drive. When I ran it I am not able to make partitions on usb. What'll be the problem in this code.
Code:
/-------------------INITIALIZE AND PARTITION-------------------------------//
#include "stdafx.h"
#include <Windows.h>
#include <stdio.h>
#include <iostream>
#include <tchar.h>
#include <winioctl.h>
#include <shellapi.h>
//-------------------------------------Main-------------------------------------//
int _tmain(int argc, _TCHAR* argv[])
{
//char *dsk = "\\\\.\\PhysicalDrive1";
HANDLE hDisk;
DWORD junk = 0;
BOOL bResult = false;
unsigned int SectorSize = 512;
//
//
hDisk = CreateFile(TEXT("\\\\.\\E:"),
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 DiskSize;
DiskSize.QuadPart = 15784004812.8;
LARGE_INTEGER Part_1_size;
Part_1_size.QuadPart = 7892002406.4;
LARGE_INTEGER Part_2_size;
Part_2_size.QuadPart = 7892002406.4;
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);
//Create the PHTSYS partition
pdg->PartitionEntry[0].PartitionStyle = PARTITION_STYLE_MBR;
pdg->PartitionEntry[0].StartingOffset.QuadPart = 32768;
pdg->PartitionEntry[0].PartitionLength = Part_1_size;
pdg->PartitionEntry[0].PartitionNumber = 1;
pdg->PartitionEntry[0].RewritePartition = 1;
pdg->PartitionEntry[0].Mbr.PartitionType = PARTITION_FAT32;
pdg->PartitionEntry[0].Mbr.BootIndicator = TRUE;
pdg->PartitionEntry[0].Mbr.RecognizedPartition = 1;
pdg->PartitionEntry[0].Mbr.HiddenSectors = (pdg->PartitionEntry[0].StartingOffset.QuadPart - 1) / SectorSize;
//Create the extended entry for the PHTDATA partition
pdg->PartitionEntry[1].PartitionStyle = PARTITION_STYLE_MBR;
pdg->PartitionEntry[1].StartingOffset.QuadPart = Part_1_size.QuadPart + pdg->PartitionEntry[0].StartingOffset.QuadPart;
pdg->PartitionEntry[1].PartitionLength = Part_2_size;
pdg->PartitionEntry[1].PartitionNumber = 2;
pdg->PartitionEntry[1].RewritePartition = 1;
pdg->PartitionEntry[1].Mbr.PartitionType = PARTITION_EXTENDED;
pdg->PartitionEntry[1].Mbr.BootIndicator = 0;
pdg->PartitionEntry[1].Mbr.RecognizedPartition = 1;
pdg->PartitionEntry[1].Mbr.HiddenSectors = (pdg->PartitionEntry[0].StartingOffset.QuadPart + Part_1_size.QuadPart) / SectorSize;
//Create the PHTDATA partition
pdg->PartitionEntry[4].PartitionStyle = PARTITION_STYLE_MBR;
pdg->PartitionEntry[4].StartingOffset.QuadPart = Part_1_size.QuadPart + pdg->PartitionEntry[0].StartingOffset.QuadPart + SectorSize;
LARGE_INTEGER LogicalPartition = Part_2_size;
LogicalPartition.QuadPart = LogicalPartition.QuadPart - SectorSize;
pdg->PartitionEntry[4].PartitionNumber = 3;
pdg->PartitionEntry[4].RewritePartition = 1;
pdg->PartitionEntry[4].Mbr.PartitionType = PARTITION_FAT32;
pdg->PartitionEntry[4].Mbr.BootIndicator = 0;
pdg->PartitionEntry[4].Mbr.RecognizedPartition = 1;
pdg->PartitionEntry[4].Mbr.HiddenSectors = (pdg->PartitionEntry[0].StartingOffset.QuadPart + Part_1_size.QuadPart) / SectorSize;
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;
}
Re: partitioning USB Falsh drive
If you mean for a usb memory stick, I refer to post #6 of your previous thread http://forums.codeguru.com/showthrea...t=#post2169443
What error code are you getting for which function()?
Re: partitioning USB Falsh drive
Thank you for your reply. I want to make partitions using c++ or vc++. for that I wrote that code. And I am not getting any error and also it is not partitioning the USB falsh drive. Actually I am new to both these languages.
Re: partitioning USB Falsh drive
As I already indicated in your other post. Does your flash drive behave as an actual harddisk/SSD or does it behave as a memory stick or as a memory stick (a block device)
memorysticks can't be partitioned, they can (usually but not necessarily) be formatted. There are memory sticks with an internal file system, these can only store files, and can't be formatted.
Also... I doubt a USB connected device would be identified as "physicaldisk1"
you are here opening the (partitioned, formatted and assigned) "disk E:", and are trying to partition that E: drive. This makes no sense at all. Only devices can be partitioned, logical drives cannnot.