簡體   English   中英

ZeroMemory的C#等效代碼

[英]C# equivalent code for ZeroMemory

我正在使用以下代碼在USB驅動器中進行分區。

char str[128];
    wsprintf(str,"\\\\.\\PhysicalDrive%d", i);
hDevice = CreateFile( ,"\\\\.\\PhysicalDrive1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL );

LARGE_INTEGER dimDiscoD;
LARGE_INTEGER dimDiscoE;
LARGE_INTEGER dimDiscoF;
LARGE_INTEGER dimDiscoG;
DISK_GEOMETRY_EX pDiskGeometryEx;

DWORD dwBytesReturned;
BOOL r = DeviceIoControl(hDevice, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, NULL, 0, &pDiskGeometryEx, sizeof(pDiskGeometryEx), &dwBytesReturned, NULL );

dimDiscoG.QuadPart = DIM_DISCOG;
dimDiscoD.QuadPart = (( pDiskGeometryEx.DiskSize.QuadPart - dimDiscoG.QuadPart )/6) * 4;
dimDiscoE.QuadPart = dimDiscoD.QuadPart/4;
dimDiscoF.QuadPart = dimDiscoE.QuadPart;

CREATE_DISK pCreateDisk;
ZeroMemory(&pCreateDisk,sizeof(CREATE_DISK));

DWORD dwBytesReturned;
pCreateDisk.PartitionStyle =  PARTITION_STYLE_MBR;
pCreateDisk.Mbr.Signature = 0xA4B57310;

DeviceIoControl(hDevice, IOCTL_DISK_CREATE_DISK, (LPVOID)&pCreateDisk, sizeof(pCreateDisk), NULL, 0, &dwBytesReturned, (LPOVERLAPPED) NULL)

DWORD junk1;

DeviceIoControl(hDevice,IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &junk1, NULL);

int SectorSize=pDiskGeometryEx.Geometry.BytesPerSector;

DWORD szNewLayout = sizeof(DRIVE_LAYOUT_INFORMATION_EX)+4*sizeof (PARTITION_INFORMATION_EX);
DRIVE_LAYOUT_INFORMATION_EX *dl = (DRIVE_LAYOUT_INFORMATION_EX*) new BYTE[szNewLayout];
ZeroMemory(dl,szNewLayout);

...................

dl->PartitionStyle = PARTITION_STYLE_MBR;
dl->PartitionCount = 4;// specify AT LEAST 4 partitions!!!
dl->Mbr.Signature = 0xA4B57310; //

DeviceIoControl(hDevice, IOCTL_DISK_SET_DRIVE_LAYOUT_EX, dl, szNewLayout, NULL, 0, &junk, (LPOVERLAPPED) NULL) )

DWORD junk;
DeviceIoControl(hDevice, IOCTL_DISK_UPDATE_PROPERTIES, NULL, 0, NULL, 0, &junk, NULL);

CloseHandle(hDevice);
delete dl;

我將上面的代碼轉換為C#。 如何將以下行轉換為C#? ZeroMemory(&pCreateDisk,sizeof(CREATE_DISK));

您無需在C#中使用ZeroMemory 您分配的所有字節數組都將隱式清零,因為所有整數類型(以及Guid的默認值)都是默認值零。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM