dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: partitioning USB Falsh drive

  1. #1
    Join Date
    Dec 2014
    Posts
    8

    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;                                                                                    
    }
    Last edited by sateeshn2507; December 9th, 2014 at 08:12 AM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,001

    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()?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.5)

  3. #3
    Join Date
    Dec 2014
    Posts
    8

    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.
    Last edited by sateeshn2507; December 9th, 2014 at 09:11 AM.

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)