Skip to content
This repository was archived by the owner on Jul 26, 2023. It is now read-only.

Commit 171b717

Browse files
authored
Merge pull request #503 from qmfrederik/fixes/device-io-control-unit-test
Add unit test for DeviceIOControl
2 parents 440808b + 4cc5ccf commit 171b717

File tree

4 files changed

+262
-0
lines changed

4 files changed

+262
-0
lines changed

src/Kernel32.Tests/storebanned/Kernel32Facts.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,43 @@ unsafe uint GetProcessIdThreadProc(void* data)
11301130
GC.KeepAlive(threadStartRoutine); // Make sure that the delegate stays alive until the test is done
11311131
}
11321132

1133+
[Fact]
1134+
public unsafe void DeviceIOControl_Works()
1135+
{
1136+
const uint IOCTL_DISK_GET_DRIVE_GEOMETRY = 0x070000;
1137+
const string drive = @"\\.\PhysicalDrive0";
1138+
1139+
using (var device = CreateFile(
1140+
filename: drive,
1141+
access: 0,
1142+
share: Kernel32.FileShare.FILE_SHARE_READ | Kernel32.FileShare.FILE_SHARE_WRITE,
1143+
securityAttributes: (SECURITY_ATTRIBUTES*)null,
1144+
creationDisposition: CreationDisposition.OPEN_EXISTING,
1145+
flagsAndAttributes: 0,
1146+
SafeObjectHandle.Null))
1147+
{
1148+
Assert.False(device.IsInvalid);
1149+
1150+
DISK_GEOMETRY pdg = default;
1151+
1152+
Assert.True(DeviceIoControl(
1153+
device,
1154+
(int)IOCTL_DISK_GET_DRIVE_GEOMETRY,
1155+
null,
1156+
0,
1157+
&pdg,
1158+
sizeof(DISK_GEOMETRY),
1159+
out int pBytesReturned,
1160+
(OVERLAPPED*)null));
1161+
1162+
Assert.NotEqual(0u, pdg.BytesPerSector);
1163+
Assert.NotEqual(0, pdg.Cylinders);
1164+
Assert.Equal(MEDIA_TYPE.FixedMedia, pdg.MediaType);
1165+
Assert.NotEqual(0u, pdg.SectorsPerTrack);
1166+
Assert.NotEqual(0u, pdg.TracksPerCylinder);
1167+
}
1168+
}
1169+
11331170
/// <summary>
11341171
/// Helper for <see cref="CreateThread_Test"/>, <see cref="CreateRemoteThread_PseudoTest"/> and
11351172
/// <see cref="CreateRemoteThreadEx_PseudoTest"/> tests.

src/Kernel32/PublicAPI.Unshipped.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ PInvoke.Kernel32.CreateThreadFlags
1111
PInvoke.Kernel32.CreateThreadFlags.CREATE_SUSPENDED = 4 -> PInvoke.Kernel32.CreateThreadFlags
1212
PInvoke.Kernel32.CreateThreadFlags.None = 0 -> PInvoke.Kernel32.CreateThreadFlags
1313
PInvoke.Kernel32.CreateThreadFlags.STACK_SIZE_PARAM_IS_A_RESERVATION = 65536 -> PInvoke.Kernel32.CreateThreadFlags
14+
PInvoke.Kernel32.DISK_GEOMETRY
15+
PInvoke.Kernel32.DISK_GEOMETRY.BytesPerSector -> uint
16+
PInvoke.Kernel32.DISK_GEOMETRY.Cylinders -> long
17+
PInvoke.Kernel32.DISK_GEOMETRY.DISK_GEOMETRY() -> void
18+
PInvoke.Kernel32.DISK_GEOMETRY.MediaType -> PInvoke.Kernel32.MEDIA_TYPE
19+
PInvoke.Kernel32.DISK_GEOMETRY.SectorsPerTrack -> uint
20+
PInvoke.Kernel32.DISK_GEOMETRY.TracksPerCylinder -> uint
1421
PInvoke.Kernel32.FileSystemFlags
1522
PInvoke.Kernel32.FileSystemFlags.FILE_CASE_PRESERVED_NAMES = 2 -> PInvoke.Kernel32.FileSystemFlags
1623
PInvoke.Kernel32.FileSystemFlags.FILE_CASE_SENSITIVE_SEARCH = 1 -> PInvoke.Kernel32.FileSystemFlags
@@ -32,6 +39,33 @@ PInvoke.Kernel32.FileSystemFlags.FILE_SUPPORTS_USN_JOURNAL = 33554432 -> PInvoke
3239
PInvoke.Kernel32.FileSystemFlags.FILE_UNICODE_ON_DISK = 4 -> PInvoke.Kernel32.FileSystemFlags
3340
PInvoke.Kernel32.FileSystemFlags.FILE_VOLUME_IS_COMPRESSED = 32768 -> PInvoke.Kernel32.FileSystemFlags
3441
PInvoke.Kernel32.FileSystemFlags.FILE_VOLUME_QUOTAS = 32 -> PInvoke.Kernel32.FileSystemFlags
42+
PInvoke.Kernel32.MEDIA_TYPE
43+
PInvoke.Kernel32.MEDIA_TYPE.F3_120M_512 = 13 -> PInvoke.Kernel32.MEDIA_TYPE
44+
PInvoke.Kernel32.MEDIA_TYPE.F3_128Mb_512 = 20 -> PInvoke.Kernel32.MEDIA_TYPE
45+
PInvoke.Kernel32.MEDIA_TYPE.F3_1Pt23_1024 = 18 -> PInvoke.Kernel32.MEDIA_TYPE
46+
PInvoke.Kernel32.MEDIA_TYPE.F3_1Pt2_512 = 17 -> PInvoke.Kernel32.MEDIA_TYPE
47+
PInvoke.Kernel32.MEDIA_TYPE.F3_1Pt44_512 = 2 -> PInvoke.Kernel32.MEDIA_TYPE
48+
PInvoke.Kernel32.MEDIA_TYPE.F3_200Mb_512 = 23 -> PInvoke.Kernel32.MEDIA_TYPE
49+
PInvoke.Kernel32.MEDIA_TYPE.F3_20Pt8_512 = 4 -> PInvoke.Kernel32.MEDIA_TYPE
50+
PInvoke.Kernel32.MEDIA_TYPE.F3_230Mb_512 = 21 -> PInvoke.Kernel32.MEDIA_TYPE
51+
PInvoke.Kernel32.MEDIA_TYPE.F3_240M_512 = 24 -> PInvoke.Kernel32.MEDIA_TYPE
52+
PInvoke.Kernel32.MEDIA_TYPE.F3_2Pt88_512 = 3 -> PInvoke.Kernel32.MEDIA_TYPE
53+
PInvoke.Kernel32.MEDIA_TYPE.F3_32M_512 = 25 -> PInvoke.Kernel32.MEDIA_TYPE
54+
PInvoke.Kernel32.MEDIA_TYPE.F3_640_512 = 14 -> PInvoke.Kernel32.MEDIA_TYPE
55+
PInvoke.Kernel32.MEDIA_TYPE.F3_720_512 = 5 -> PInvoke.Kernel32.MEDIA_TYPE
56+
PInvoke.Kernel32.MEDIA_TYPE.F5_160_512 = 10 -> PInvoke.Kernel32.MEDIA_TYPE
57+
PInvoke.Kernel32.MEDIA_TYPE.F5_180_512 = 9 -> PInvoke.Kernel32.MEDIA_TYPE
58+
PInvoke.Kernel32.MEDIA_TYPE.F5_1Pt23_1024 = 19 -> PInvoke.Kernel32.MEDIA_TYPE
59+
PInvoke.Kernel32.MEDIA_TYPE.F5_1Pt2_512 = 1 -> PInvoke.Kernel32.MEDIA_TYPE
60+
PInvoke.Kernel32.MEDIA_TYPE.F5_320_1024 = 8 -> PInvoke.Kernel32.MEDIA_TYPE
61+
PInvoke.Kernel32.MEDIA_TYPE.F5_320_512 = 7 -> PInvoke.Kernel32.MEDIA_TYPE
62+
PInvoke.Kernel32.MEDIA_TYPE.F5_360_512 = 6 -> PInvoke.Kernel32.MEDIA_TYPE
63+
PInvoke.Kernel32.MEDIA_TYPE.F5_640_512 = 15 -> PInvoke.Kernel32.MEDIA_TYPE
64+
PInvoke.Kernel32.MEDIA_TYPE.F5_720_512 = 16 -> PInvoke.Kernel32.MEDIA_TYPE
65+
PInvoke.Kernel32.MEDIA_TYPE.F8_256_128 = 22 -> PInvoke.Kernel32.MEDIA_TYPE
66+
PInvoke.Kernel32.MEDIA_TYPE.FixedMedia = 12 -> PInvoke.Kernel32.MEDIA_TYPE
67+
PInvoke.Kernel32.MEDIA_TYPE.RemovableMedia = 11 -> PInvoke.Kernel32.MEDIA_TYPE
68+
PInvoke.Kernel32.MEDIA_TYPE.Unknown = 0 -> PInvoke.Kernel32.MEDIA_TYPE
3569
PInvoke.Kernel32.MEMORY_PRIORITY_INFORMATION
3670
PInvoke.Kernel32.MEMORY_PRIORITY_INFORMATION.MEMORY_PRIORITY_INFORMATION() -> void
3771
PInvoke.Kernel32.MEMORY_PRIORITY_INFORMATION.MemoryPriority -> PInvoke.Kernel32.MemoryPriority
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright © .NET Foundation and Contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
namespace PInvoke
5+
{
6+
/// <content>
7+
/// Contains the <see cref="DISK_GEOMETRY"/> nested type.
8+
/// </content>
9+
public partial class Kernel32
10+
{
11+
/// <summary>
12+
/// Describes the geometry of disk devices and media.
13+
/// </summary>
14+
/// <seealso href="https://docs.microsoft.com/en-us/windows/win32/api/winioctl/ns-winioctl-disk_geometry"/>
15+
public struct DISK_GEOMETRY
16+
{
17+
/// <summary>
18+
/// The number of cylinders.
19+
/// </summary>
20+
public long Cylinders;
21+
22+
/// <summary>
23+
/// The type of media.
24+
/// </summary>
25+
public MEDIA_TYPE MediaType;
26+
27+
/// <summary>
28+
/// The number of tracks per cylinder.
29+
/// </summary>
30+
public uint TracksPerCylinder;
31+
32+
/// <summary>
33+
/// The number of sectors per track.
34+
/// </summary>
35+
public uint SectorsPerTrack;
36+
37+
/// <summary>
38+
/// The number of bytes per sector.
39+
/// </summary>
40+
public uint BytesPerSector;
41+
}
42+
}
43+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
// Copyright © .NET Foundation and Contributors. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
namespace PInvoke
5+
{
6+
/// <content>
7+
/// Contains the <see cref="MEDIA_TYPE"/> nested type.
8+
/// </content>
9+
public partial class Kernel32
10+
{
11+
/// <summary>
12+
/// Represents the various forms of device media.
13+
/// </summary>
14+
/// <seealso href="https://docs.microsoft.com/en-us/windows/win32/api/winioctl/ne-winioctl-media_type"/>
15+
public enum MEDIA_TYPE
16+
{
17+
/// <summary>
18+
/// Format is unknown
19+
/// </summary>
20+
Unknown,
21+
22+
/// <summary>
23+
/// A 5.25" floppy, with 1.2MB and 512 bytes/sector.
24+
/// </summary>
25+
F5_1Pt2_512,
26+
27+
/// <summary>
28+
/// A 3.5" floppy, with 1.44MB and 512 bytes/sector.
29+
/// </summary>
30+
F3_1Pt44_512,
31+
32+
/// <summary>
33+
/// A 3.5" floppy, with 2.88MB and 512 bytes/sector.
34+
/// </summary>
35+
F3_2Pt88_512,
36+
37+
/// <summary>
38+
/// A 3.5" floppy, with 20.8MB and 512 bytes/sector.
39+
/// </summary>
40+
F3_20Pt8_512,
41+
42+
/// <summary>
43+
/// A 3.5" floppy, with 720KB and 512 bytes/sector.
44+
/// </summary>
45+
F3_720_512,
46+
47+
/// <summary>
48+
/// A 5.25" floppy, with 360KB and 512 bytes/sector.
49+
/// </summary>
50+
F5_360_512,
51+
52+
/// <summary>
53+
/// A 5.25" floppy, with 320KB and 512 bytes/sector.
54+
/// </summary>
55+
F5_320_512,
56+
57+
/// <summary>
58+
/// A 5.25" floppy, with 320KB and 1024 bytes/sector.
59+
/// </summary>
60+
F5_320_1024,
61+
62+
/// <summary>
63+
/// A 5.25" floppy, with 180KB and 512 bytes/sector.
64+
/// </summary>
65+
F5_180_512,
66+
67+
/// <summary>
68+
/// A 5.25" floppy, with 160KB and 512 bytes/sector.
69+
/// </summary>
70+
F5_160_512,
71+
72+
/// <summary>
73+
/// Removable media other than floppy.
74+
/// </summary>
75+
RemovableMedia,
76+
77+
/// <summary>
78+
/// Fixed hard disk media.
79+
/// </summary>
80+
FixedMedia,
81+
82+
/// <summary>
83+
/// A 3.5" floppy, with 120MB and 512 bytes/sector.
84+
/// </summary>
85+
F3_120M_512,
86+
87+
/// <summary>
88+
/// A 3.5" floppy, with 640KB and 512 bytes/sector.
89+
/// </summary>
90+
F3_640_512,
91+
92+
/// <summary>
93+
/// A 5.25" floppy, with 640KB and 512 bytes/sector.
94+
/// </summary>
95+
F5_640_512,
96+
97+
/// <summary>
98+
/// A 5.25" floppy, with 720KB and 512 bytes/sector.
99+
/// </summary>
100+
F5_720_512,
101+
102+
/// <summary>
103+
/// A 3.5" floppy, with 1.2MB and 512 bytes/sector.
104+
/// </summary>
105+
F3_1Pt2_512,
106+
107+
/// <summary>
108+
/// A 3.5" floppy, with 1.23MB and 1024 bytes/sector.
109+
/// </summary>
110+
F3_1Pt23_1024,
111+
112+
/// <summary>
113+
/// A 5.25" floppy, with 1.23MB and 1024 bytes/sector.
114+
/// </summary>
115+
F5_1Pt23_1024,
116+
117+
/// <summary>
118+
/// A 3.5" floppy, with 128MB and 512 bytes/sector.
119+
/// </summary>
120+
F3_128Mb_512,
121+
122+
/// <summary>
123+
/// A 3.5" floppy, with 230MB and 512 bytes/sector.
124+
/// </summary>
125+
F3_230Mb_512,
126+
127+
/// <summary>
128+
/// An 8" floppy, with 256KB and 128 bytes/sector.
129+
/// </summary>
130+
F8_256_128,
131+
132+
/// <summary>
133+
/// A 3.5" floppy, with 200MB and 512 bytes/sector. (HiFD).
134+
/// </summary>
135+
F3_200Mb_512,
136+
137+
/// <summary>
138+
/// A 3.5" floppy, with 240MB and 512 bytes/sector. (HiFD).
139+
/// </summary>
140+
F3_240M_512,
141+
142+
/// <summary>
143+
/// A 3.5" floppy, with 32MB and 512 bytes/sector.
144+
/// </summary>
145+
F3_32M_512,
146+
}
147+
}
148+
}

0 commit comments

Comments
 (0)