P
Peter-HK
Guest
Hi
I am trying to get the disk size by calling DeviceIoControl, but it return error 87, which mean parameter incorrect. I tried to use windows dd to write some sectors to usb stick, it also got error 87. Am my windows 10 blocked this function call?
Below is my code:
// ConsoleApplication1.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include "pch.h"
#include <iostream>
#include <windows.h>
using namespace std;
short ReadSect(const char *_dsk, char *&_buff, unsigned int _nsect) {
DWORD dwRead;
HANDLE hDisk = CreateFile((LPCWSTR)_dsk, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hDisk == INVALID_HANDLE_VALUE) // this may happen if another program is already reading from disk
{
std::cout << "unable to read harddisk";
CloseHandle(hDisk);
return 1;
}
SetFilePointer(hDisk, _nsect * 512, 0, FILE_BEGIN); // which sector to read
ReadFile(hDisk, _buff, 512, &dwRead, 0); // read sector
CloseHandle(hDisk);
return 0;
}
uint64_t GetDiskLengthIoctl(const char *_dsk) {
//HANDLE hDisk = CreateFile((LPCWSTR)"\\\\.\\PhysicalDrive0", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
HANDLE hDisk = CreateFile((LPCWSTR)_dsk,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_DELETE |
FILE_SHARE_READ |
FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hDisk == INVALID_HANDLE_VALUE)
{
cerr << "Could not open the disk. GetLastError() returned " << GetLastError() << endl;
return 0;
}
GET_LENGTH_INFORMATION gli;
DWORD ret;
DeviceIoControl(hDisk, IOCTL_DISK_GET_LENGTH_INFO, 0, 0, &gli, sizeof(gli), &ret, 0);
CloseHandle(hDisk);
return gli.Length.QuadPart;
}
unsigned long long GetDiskLengthIoctl2(const char *dsk)
{
HANDLE hDisk = CreateFile(LPCWSTR(dsk),
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hDisk == INVALID_HANDLE_VALUE)
{
cerr << "CreateFile INVALID_HANDLE_VALUE = " << GetLastError() << endl;
return 0;
}
/*int nDiskBufferSize = sizeof(DRIVE_LAYOUT_INFORMATION) + sizeof(PARTITION_INFORMATION) * 106;
PDRIVE_LAYOUT_INFORMATION DiskLayout = (PDRIVE_LAYOUT_INFORMATION)malloc(nDiskBufferSize);
DWORD dwRet;
int res = DeviceIoControl(
hDisk,
IOCTL_DISK_GET_DRIVE_LAYOUT,
NULL,
0,
DiskLayout,
nDiskBufferSize,
&dwRet,
NULL);
DWORD error;
error = GetLastError();*/
GET_LENGTH_INFORMATION gli;
DWORD ret;
int r = DeviceIoControl(hDisk,
IOCTL_DISK_GET_LENGTH_INFO,
NULL,
0,
&gli,
sizeof(gli),
&ret,
(LPOVERLAPPED)NULL);
if (r > 0) {
LONGLONG nUseSize = gli.Length.QuadPart;
INT64 sizeGB = nUseSize / 1014 / 1024 / 1024;
//printf("a1=%I64x %I64u\n", sizeGB, sizeGB);
return gli.Length.QuadPart;
}
else {
cerr << "DeviceIoControl GET_LENGTH_INFORMATION = " << GetLastError() << endl;
cout << "Failed to get disk information." << endl;
DWORD error;
error = GetLastError();
HRESULT hRe = HRESULT_FROM_WIN32(error);
char errorData[10];
sprintf_s(errorData, "%x", hRe);
cout << "Error code:" <</*hRe*/errorData << endl;
CloseHandle(hDisk);
return -1;
}
}
int main() {
//wmic diskdrive list brief
//const char * drv = "\\\\.\\C:";
const char *dsk = "\\\\.\\PhysicalDrive1";
int sector = 0;
//uint64_t len = GetDiskLengthIoctl(dsk);
//cout << (len / 1024 / 1024 / 1024) << "GB" << endl;
//char *buff = new char[512];
//ReadSect(dsk, buff, sector);
////for (int x = 0; x < 512; x++) {
//// printf("%02x ", (char)buff[x]);
////}
//cout << endl;
printf("size = %I64d (GB)\n", GetDiskLengthIoctl2(dsk));
//printf("Lba = %x \n", GetDiskLengthIoctl2(dsk) / 512);
//if ((unsigned char)buff[510] == 0x55 && (unsigned char)buff[511] == 0xaa) std::cout << "Disk is bootable!" << std::endl;
}
Peter
Continue reading...
I am trying to get the disk size by calling DeviceIoControl, but it return error 87, which mean parameter incorrect. I tried to use windows dd to write some sectors to usb stick, it also got error 87. Am my windows 10 blocked this function call?
Below is my code:
// ConsoleApplication1.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include "pch.h"
#include <iostream>
#include <windows.h>
using namespace std;
short ReadSect(const char *_dsk, char *&_buff, unsigned int _nsect) {
DWORD dwRead;
HANDLE hDisk = CreateFile((LPCWSTR)_dsk, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hDisk == INVALID_HANDLE_VALUE) // this may happen if another program is already reading from disk
{
std::cout << "unable to read harddisk";
CloseHandle(hDisk);
return 1;
}
SetFilePointer(hDisk, _nsect * 512, 0, FILE_BEGIN); // which sector to read
ReadFile(hDisk, _buff, 512, &dwRead, 0); // read sector
CloseHandle(hDisk);
return 0;
}
uint64_t GetDiskLengthIoctl(const char *_dsk) {
//HANDLE hDisk = CreateFile((LPCWSTR)"\\\\.\\PhysicalDrive0", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
HANDLE hDisk = CreateFile((LPCWSTR)_dsk,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_DELETE |
FILE_SHARE_READ |
FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hDisk == INVALID_HANDLE_VALUE)
{
cerr << "Could not open the disk. GetLastError() returned " << GetLastError() << endl;
return 0;
}
GET_LENGTH_INFORMATION gli;
DWORD ret;
DeviceIoControl(hDisk, IOCTL_DISK_GET_LENGTH_INFO, 0, 0, &gli, sizeof(gli), &ret, 0);
CloseHandle(hDisk);
return gli.Length.QuadPart;
}
unsigned long long GetDiskLengthIoctl2(const char *dsk)
{
HANDLE hDisk = CreateFile(LPCWSTR(dsk),
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hDisk == INVALID_HANDLE_VALUE)
{
cerr << "CreateFile INVALID_HANDLE_VALUE = " << GetLastError() << endl;
return 0;
}
/*int nDiskBufferSize = sizeof(DRIVE_LAYOUT_INFORMATION) + sizeof(PARTITION_INFORMATION) * 106;
PDRIVE_LAYOUT_INFORMATION DiskLayout = (PDRIVE_LAYOUT_INFORMATION)malloc(nDiskBufferSize);
DWORD dwRet;
int res = DeviceIoControl(
hDisk,
IOCTL_DISK_GET_DRIVE_LAYOUT,
NULL,
0,
DiskLayout,
nDiskBufferSize,
&dwRet,
NULL);
DWORD error;
error = GetLastError();*/
GET_LENGTH_INFORMATION gli;
DWORD ret;
int r = DeviceIoControl(hDisk,
IOCTL_DISK_GET_LENGTH_INFO,
NULL,
0,
&gli,
sizeof(gli),
&ret,
(LPOVERLAPPED)NULL);
if (r > 0) {
LONGLONG nUseSize = gli.Length.QuadPart;
INT64 sizeGB = nUseSize / 1014 / 1024 / 1024;
//printf("a1=%I64x %I64u\n", sizeGB, sizeGB);
return gli.Length.QuadPart;
}
else {
cerr << "DeviceIoControl GET_LENGTH_INFORMATION = " << GetLastError() << endl;
cout << "Failed to get disk information." << endl;
DWORD error;
error = GetLastError();
HRESULT hRe = HRESULT_FROM_WIN32(error);
char errorData[10];
sprintf_s(errorData, "%x", hRe);
cout << "Error code:" <</*hRe*/errorData << endl;
CloseHandle(hDisk);
return -1;
}
}
int main() {
//wmic diskdrive list brief
//const char * drv = "\\\\.\\C:";
const char *dsk = "\\\\.\\PhysicalDrive1";
int sector = 0;
//uint64_t len = GetDiskLengthIoctl(dsk);
//cout << (len / 1024 / 1024 / 1024) << "GB" << endl;
//char *buff = new char[512];
//ReadSect(dsk, buff, sector);
////for (int x = 0; x < 512; x++) {
//// printf("%02x ", (char)buff[x]);
////}
//cout << endl;
printf("size = %I64d (GB)\n", GetDiskLengthIoctl2(dsk));
//printf("Lba = %x \n", GetDiskLengthIoctl2(dsk) / 512);
//if ((unsigned char)buff[510] == 0x55 && (unsigned char)buff[511] == 0xaa) std::cout << "Disk is bootable!" << std::endl;
}
Peter
Continue reading...