gpt4 book ai didi

ZeroMemory 的 C# 等效代码

转载 作者:行者123 更新时间:2023-11-30 00:49:26 24 4
gpt4 key购买 nike

我正在使用以下代码在 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 的默认值,这似乎与此处间接相关)。

关于ZeroMemory 的 C# 等效代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28174397/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com