Je suis coincé en train de tenter de repartitionner et de formater un lecteur flash USB en C++, toute aide serait super!Erreur lors du partitionnement et du formatage Clé USB en C++
L'objectif est de repartitionner n'importe quel lecteur flash arbitraire avec une seule partition prenant tout l'espace et formaté FAT32 (options ultérieures NTFS et EXFAT). Cela se fera en batch, avec plus de 50 appareils en même temps, avec un peu de chance, donc l'accès aux lettres n'est pas une option. Je suis capable de créer une partition, mais lorsque j'essaie IOCTL_DISK_SET_PARTITION_INFO_EX pour définir le type de format, il échoue avec 0x32, ERROR_NOT_SUPPORTED. Mais on ne sait pas exactement ce qui n'est pas supporté. Je peux partitionner manuellement le périphérique en utilisant des utilitaires tels que diskpart, donc je sais que les types de partition et de système de fichiers sont pris en charge par le périphérique. Quelqu'un peut-il aider? Mon code source complet est ci-dessous, il échoue sur l'appel DeviceIoControl() avec IOCTL_DISK_SET_PARTITION_INFO_EX.
#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;
}
Avez-vous essayé d'exécuter votre programme en tant qu'administrateur (via "Exécuter en tant qu'administrateur")? – 1201ProgramAlarm
Quel est l'objectif de 'IOCTL_DISK_SET_PARTITION_INFO_EX'? Vous avez déjà défini le type de partition dans le fichier 'IOCTL_DISK_SET_DRIVE_LAYOUT_EX' ci-dessus. –
Merci pour la suggestion. Oui, j'ai essayé le clic droit et "exécuter en tant qu'administrateur" et le résultat est le même, erreur 0x32 ERROR_NOT_SUPPORTED. – Matt