-
Notifications
You must be signed in to change notification settings - Fork 1
/
disk.cpp
85 lines (73 loc) · 2.16 KB
/
disk.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "stdafx.h"
namespace nsDisk {
LPCSTR drive0 = (LPCSTR) "\\\\.\\PhysicalDrive0";
LPCSTR drive1 = (LPCSTR) "\\\\.\\PhysicalDrive1";
}
bool disk::driveGeometry()
{
//open the specified disk, obtain all disk information,
//this information is later available to the caller
DISK_GEOMETRY * pdg = &geometry;
HANDLE hDevice; // handle to the drive to be examined
// bool bResult; // results flag
HRESULT hResult;
DWORD junk; // discard results
if (driveNumber != NULL)
{
hDevice = CreateFile(driveNumber, // drive to open
0, // no access to the drive
FILE_SHARE_READ | // share mode
FILE_SHARE_WRITE,
NULL, // default security attributes
OPEN_EXISTING, // disposition
0, // file attributes
NULL); // do not copy file attributes
}
else
{
Inform("Disk Number not specified...");
return false;
}
if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
{
Inform("Could Not Open Disk...");
Inform(driveNumber);
return (false);
}
hResult = DeviceIoControl(hDevice, // device to be queried
IOCTL_DISK_GET_DRIVE_GEOMETRY, // operation to perform
NULL, 0, // no input buffer
pdg, sizeof(*pdg), // output buffer
&junk, // # bytes returned
(LPOVERLAPPED) NULL); // synchronous I/O
CloseHandle(hDevice);
return (hResult >= 0/*bResult*/);
}
bool disk::setDrive(int number)
{
//set the disk that the user wants information for
//the window header file uses type LPCSTR, i've had a lot of trouble
//converting this (with user input) to a unified source string (won't accept cast)
//....so for now an ugly switch statement will have to do
switch (number)
{
case 0:
driveNumber = nsDisk::drive0; break;
case 1:
driveNumber = nsDisk::drive1; break;
default:
return false; break;
}
return true;
}
void disk::test()
{
//display number of bytes per sector
DISK_GEOMETRY obj = getGeometry();
cout << obj.BytesPerSector << endl;
}
DISK_GEOMETRY disk::getGeometry() const
{
//standard accessor
return geometry;
}