本文介绍了分区原始usb磁盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我的目标是为原始usb磁盘创建完整磁盘大小的分区。代码片段如下:



// usbpartition.cpp:定义控制台应用程序的入口点。

//



#includestdafx.h

#includewindows.h





int _tmain(int argc,_TCHAR * argv [])

{

bool bResult = false; //通用结果标志

HANDLE hDevice; //处理要检查的驱动器

DWORD垃圾= 0; //丢弃结果



hDevice = CreateFile(TEXT(\\\\\\\\PhysicalDrive1),//\\\ \\\\\PhysicalDrive1,//开车开启

GENERIC_READ | GENERIC_WRITE,//无法访问驱动器

FILE_SHARE_READ | //分享模式

FILE_SHARE_WRITE,

NULL,//默认安全属性

OPEN_EXISTING,//处置

0,/ / file attributes

NULL); //不要复制文件属性

if(hDevice == INVALID_HANDLE_VALUE)//无法打开驱动器

{

返回GetLastError();

}



CREATE_DISK dsk;

dsk.PartitionStyle = PARTITION_STYLE_MBR;

dsk.Mbr.Signature = 9999;



//初始化磁盘

bResult = DeviceIoControl(hDevice, //要查询的设备

IOCTL_DISK_CREATE_DISK,//执行的操作

&dsk,sizeof(dsk),// sizeof(pdg),//输出缓冲区

NULL,0,//无输出缓冲区

&junk,//返回#字节

NULL);

if(!bResult)

{

返回GetLastError();

}

bResult = DeviceIoControl(hDevice,

IOCTL_DISK_UPDATE_PROPERTIES,

NULL,0,NULL,0,&junk ,NULL);

if(!bResult)

{

返回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)

{

返回-1;

}

SecureZeroMemory(pdg, dwDriverLayoutInfoExLen);

//在每个分区中设置RewritePartition = true以强制重写。

// 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分区或逻辑驱动器)

pdg-> PartitionEntry [0] .Mbr.BootIndicator = false;

pdg-> PartitionEntry [0]。 Mbr.RecognizedPartition = 1;

pdg-> PartitionEntry [0] .Mbr.HiddenSectors = 0; // 32256/512;



//分区磁盘

bResult = DeviceIoControl(hDevice,//要查询的设备

IOCTL_DISK_SET_DRIVE_LAYOUT_EX ,//执行操作

pdg,sizeof DRIVE_LAYOUT_INFORMATION_EX,// sizeof(pdg),//输出缓冲区

NULL,0,//无输出缓冲区

&junk,//返回#字节

NULL);

if(!bResult)

{

返回GetLastError();

}



bResult = DeviceIoControl(hDevice,

IOCTL_DISK_UPDATE_PROPERTIES,

NULL ,0,NULL,0,&junk,NULL);

if(!bResult)

{

返回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无法成功

bResult = DeviceIoControl(hDevice,//要查询的设备

IOCTL_DISK_SET_PARTITION_INFO_EX,//要执行的操作

&dskinfo,sizeof(dskinfo),// sizeof(pdg),//输出缓冲区

NULL,0,//无输出缓冲区

&junk,//#bytes返回

//(LPOVERLAPPED)

NULL);

if(!bResult)

{

int out = GetLastError();

printf(output = %d \ n,out);

// OutputDebugString(Loutput,GetLastError());

退出;

}



返回0;

}



但是IOCTL IOCTL_DISK_SET_PARTITION_INFO_EX失败,错误代码为31.请帮我判断我的代码是否正确..

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..

推荐答案


这篇关于分区原始usb磁盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 02:29