ZeroMemory的C#等价代码

时间:2015-01-27 15:45:43

标签: c# c++ memory zero

我使用以下代码在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));

1 个答案:

答案 0 :(得分:6)

您不需要在C#中使用ZeroMemory;你分配的所有字节数组都被隐式归零,因为零是所有整数类型(以及Guid的默认值),这似乎是间接涉及的。)