我试图使用C ++重新分区和格式化USB闪存驱动器,任何帮助都会很棒!
目标是使用单个分区重新划分任意闪存驱动器,占用整个空间并格式化FAT32(后面的选项NTFS和EXFAT)。这将是批量完成的,希望一次有50多个设备,因此驱动器号访问不是一个选项。我能够创建一个分区,但是当我尝试IOCTL_DISK_SET_PARTITION_INFO_EX来设置格式类型时,它失败了0x32,ERROR_NOT_SUPPORTED。但目前尚不清楚究竟是什么不支持。我可以使用诸如diskpart之类的实用程序手动对设备进行分区,因此我知道设备支持分区和文件系统类型。有人可以帮忙吗?我的完整源代码如下,它在使用IOCTL_DISK_SET_PARTITION_INFO_EX进行的DeviceIoControl()调用失败。
#include "stdafx.h"
#include <random>
#include <Windows.h>
#include <atlstr.h>
#include <iostream>
#include <assert.h>
using namespace std;
#define THROW_CSTRING(a, b) { CString csE; csE.Format(a, b); throw csE; }
#define RANDOM_DWORD {DWORD(rand()) | DWORD(rand() << 8) | DWORD(rand() << 16) | DWORD(rand() << 24)}
int main()
{
DRIVE_LAYOUT_INFORMATION_EX* pdg = NULL;
HANDLE hDevice = INVALID_HANDLE_VALUE;
try
{
hDevice = CreateFile(L"\\\\.\\PhysicalDrive2",
GENERIC_READ | GENERIC_WRITE,
0, // Only we can access
NULL, // Default security
OPEN_EXISTING, // For hardware, open existing
0, // File attributes
NULL); //Do not copy attributes
if (hDevice == INVALID_HANDLE_VALUE)
{
THROW_CSTRING(L"ERROR: CreateFile() failed: 0x%x", GetLastError());
}
CREATE_DISK dsk;
memset(&dsk, 0, sizeof(dsk));
CREATE_DISK_MBR dskmbr = { 0 };
dskmbr.Signature = 1;
dsk.PartitionStyle = PARTITION_STYLE_MBR;
dsk.Mbr = dskmbr;
// DRIVE_LAYOUT_INFORMAITON_EX has an array of partition info at the end, need enough for 4 partitions minimum
int iDriveLayoutBytesRequired = sizeof(DRIVE_LAYOUT_INFORMATION_EX) + sizeof(PARTITION_INFORMATION_EX) * 3;
pdg = (DRIVE_LAYOUT_INFORMATION_EX*)new BYTE[iDriveLayoutBytesRequired];
memset(pdg, 0, iDriveLayoutBytesRequired);
DRIVE_LAYOUT_INFORMATION_MBR mbrlayout = { 0 };
mbrlayout.Signature = RANDOM_DWORD;
pdg->PartitionStyle = PARTITION_STYLE_MBR;
pdg->Mbr = mbrlayout;
pdg->PartitionCount = 1;
DWORD dwBytesReturned = 0;
if (!DeviceIoControl(hDevice, IOCTL_DISK_CREATE_DISK, &dsk, sizeof(dsk), NULL, 0, &dwBytesReturned, NULL))
{
THROW_CSTRING(L"ERROR: IOCTL_DISK_CREATE_DISK failed: 0x%x", GetLastError());
}
// Get the drive dimensions, then use that info to create a new partition
// Drive length
GET_LENGTH_INFORMATION sLenInfo = { 0 };
if (!DeviceIoControl(hDevice, IOCTL_DISK_GET_LENGTH_INFO, NULL, 0, &sLenInfo, sizeof(sLenInfo), &dwBytesReturned, NULL))
{
THROW_CSTRING(L"ERROR: IOCTL_DISK_GET_LENGTH_INFO failed: 0x%x", GetLastError());
}
assert(sizeof(sLenInfo.Length.QuadPart) == sizeof(__int64));
__int64 iDiskLengthBytes = sLenInfo.Length.QuadPart;
pdg->PartitionStyle = PARTITION_STYLE_MBR;
pdg->PartitionCount = 4;
pdg->Mbr.Signature = 1;
pdg->PartitionEntry[0].PartitionStyle = PARTITION_STYLE_MBR;
pdg->PartitionEntry[0].StartingOffset.QuadPart = 0;
pdg->PartitionEntry[0].PartitionLength.QuadPart = iDiskLengthBytes;
pdg->PartitionEntry[0].PartitionNumber = 1;
pdg->PartitionEntry[0].RewritePartition = TRUE;
//pdg->PartitionEntry[0].Mbr.PartitionType = PARTITION_IFS; // NTFS
pdg->PartitionEntry[0].Mbr.PartitionType = PARTITION_FAT32;
pdg->PartitionEntry[0].Mbr.BootIndicator = TRUE;
pdg->PartitionEntry[0].Mbr.RecognizedPartition = 1;
pdg->PartitionEntry[0].Mbr.HiddenSectors = 0;
// Partition device
if (!DeviceIoControl(hDevice, IOCTL_DISK_SET_DRIVE_LAYOUT_EX, pdg, iDriveLayoutBytesRequired, NULL, 0, &dwBytesReturned, NULL))
{
THROW_CSTRING(L"ERROR: IOCTL_DISK_SEt_DRIVE_LAYOUT_EX failed: 0x%x", GetLastError());
}
// Tell the driver to flush its cache
if (!DeviceIoControl(hDevice, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &dwBytesReturned, NULL))
{
THROW_CSTRING(L"ERROR: IOCTL_DISK_UPDATE_PROPERTIES failed: 0x%x", GetLastError());
}
SET_PARTITION_INFORMATION_EX dskinfo;
memset(&dskinfo, 0, sizeof(dskinfo));
dskinfo.PartitionStyle = PARTITION_STYLE_MBR;
dskinfo.Mbr.PartitionType = PARTITION_FAT32;
if (!DeviceIoControl(hDevice, IOCTL_DISK_SET_PARTITION_INFO_EX, &dskinfo, sizeof(dskinfo), NULL, 0, &dwBytesReturned, NULL))
{
THROW_CSTRING(L"ERROR: IOCTL_DISK_SET_PARTITION_INFO_EX failed: 0x%x", GetLastError());
}
}
catch (CString csErr)
{
// Error lookup: https://msdn.microsoft.com/en-us/library/w indows/desktop/ms681382(v=vs.85).aspx
// 0x7a - ERROR_INSUFFICIENT_BUFFER
// 0x57 - ERROR_INVALID_PARAMETER
// 0x32 - ERROR_NOT_SUPPORTED
// 0x18 - ERROR_BAD_LENGTH
// 0x05 - ERROR_ACCESS_DENIED
wcout << csErr.GetString();
}
CloseHandle(hDevice);
delete pdg;
return 0;
}
答案 0 :(得分:1)
我有一个解决方案,但它有点令人费解。我使用上面的DeviceIoControl()来分区磁盘。然后我使用VDS和IID_IVdsVolumeMF接口来创建文件系统,但是到那里有一些工作。目标是对系统上的所有闪存驱动器(USB记忆棒)进行分区和格式化。 VDS将通过IID_IVdsVolumeMF接口执行格式,但它不会告诉您(至少我还没弄清楚)哪些设备是可移动的。但是WMI会告诉你哪些设备是可移动的,但没有格式化功能。所以...
首先使用WMI获取系统上所有 可移动 卷路径的列表,例如:
CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *)&pLoc)
pLoc->ConnectServer(CComBSTR(L"ROOT\\CIMV2"), nullptr, nullptr, nullptr, 0, nullptr, nullptr, pWbemSvc)
CoSetProxyBlanket(
*pWbemSvc, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
NULL, // client identity
EOAC_NONE // proxy capabilities
pWbemSvc->ExecQuery(CComBSTR(L"WQL"), CComBSTR(L"SELECT * FROM Win32_Volume WHERE DriveType=2"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator)
为您提供以下路径:
L"\\\\?\\Volume{3899cb7b-7c3f-11e6-bf82-005056c00008}\\"
然后使用VDS获取计算机上所有VDS卷的列表。基本上你加载VDS,然后获得所有软件提供商。为简洁起见,这个来源缺少部分,但我想我已经足够解释发生了什么:
pSvc->QueryProviders(VDS_QUERY_SOFTWARE_PROVIDERS, &pEnumProviders)
现在遍历提供商列表,从每个提供商处获取包:
pEnumProviders->Next(1, &pUnk, &cFetched)
pProv = pUnk;
pProv->QueryPacks(&pEnumpacks)
vPacks.push_back(pEnumpacks);
现在遍历包并获取每个包中的所有卷:
iterator iPacks = vPacks.begin();
(*iPacks)->Next(1, &pUnk, &cFetched)
pPack = pUnk;
pPack->QueryVolumes(&pEnumvolumes)
pvpEnumvolumes->push_back(pEnumvolumes)
现在您有一个可移动设备的路径列表,并且您有一个系统上所有卷的列表。是时候比较它们并找出哪些 卷 可移动 。
iVolEnum = pvpEnumOfVDSVolumes->begin()
(*iVolEnum)->Next(1, &pUnk, &cFetched)
pVMF3 = pUnk;
CComHeapPtr<LPWSTR> pVDSVolumePaths;
pVMF3->QueryVolumeGuidPathnames(&pVDSVolumePaths, &nPaths)
iterator iWMIVolPath = pvWMIRemovableVols->begin();
loop..
if (wcscmp(iWMIVolPath->data(), pVDSVolumePaths[i]) == 0)
{ // VDS Vol is removable! }
现在使用此VDS卷对象格式化卷:
foreach( vol in vRemovableVDSVols )
{
CComQIPtr<IVdsVolume> pVolume = *(vol);
IVdsVolumeMF *pVolumeMF;
pVolume->QueryInterface(IID_IVdsVolumeMF, (void **)&pVolumeMF);
pVolumeMF->Format( VDS_FST_FAT32,
L"MyFob",
512, // alloc size
true, // force
false, // quick
false, // compression
&pAsync); // async
}
然后你的USB记忆棒被格式化了!哇..但似乎工作。
微软真的没有让这更轻松吗?