|
|
1.1 root 1: // Write a byte to an port using the wkd driver.
2: // Robert R. Howell January 6, 1993
3: // Robert B. Nelson (Microsoft) January 12, 1993
4: // Robert B. Nelson (Microsoft) April 5, 1993
5: // Robert B. Nelson (Microsoft) May 6, 1993
6:
7: #include <windows.h>
8: #include <stddef.h>
9: #include <stdio.h>
10: #include <stdlib.h>
11: #include <winioctl.h>
12: #include "gpioctl.h" // This defines the IOCTL constants.
13:
14: void main(int argc, char **argv)
15: {
16:
17: // The following is returned by IOCTL. It is true if the write succeeds.
18: BOOL IoctlResult;
19:
20: // The following parameters are used in the IOCTL call
21: HANDLE hndFile; // Handle to device, obtain from CreateFile
22: GENPORT_WRITE_INPUT InputBuffer; // Input buffer for DeviceIoControl
23: LONG IoctlCode;
24: ULONG DataValue;
25: ULONG DataLength;
26: ULONG ReturnedLength; // Number of bytes returned in output buffer
27:
28:
29: if (argc < 4)
30: {
31: printf("GpdWrite -b|-w|-d <Port#> <Value>\n\n");
32: printf(" The byte (-b), word (-w), or doubleword (-d) specified\n");
33: printf(" as value is written to the given port. Ports are numbered\n");
34: printf(" as 0, 1, ... relative to the base set in the driver or\n");
35: printf(" the registry. The default driver uses ports 300h through\n");
36: printf(" 303h. All numbers are read in hex.\n");
37: exit(1);
38: }
39:
40:
41: hndFile = CreateFile(
42: "\\\\.\\GpdDev", // Open the Device "file"
43: GENERIC_WRITE,
44: FILE_SHARE_WRITE,
45: NULL,
46: OPEN_EXISTING,
47: 0,
48: NULL);
49:
50: if (hndFile == INVALID_HANDLE_VALUE) // Was the device opened?
51: {
52: printf("Unable to open the device.\n");
53: exit(1);
54: }
55:
56: sscanf(argv[2], "%x", &InputBuffer.PortNumber); // Get the port number
57: sscanf(argv[3], "%x", &DataValue); // Get the data to be written.
58:
59: switch (argv[1][1])
60: {
61: case 'b':
62: IoctlCode = IOCTL_GPD_WRITE_PORT_UCHAR;
63: InputBuffer.CharData = (UCHAR)DataValue;
64: DataLength = offsetof(GENPORT_WRITE_INPUT, CharData) +
65: sizeof(InputBuffer.CharData);
66: break;
67:
68: case 'w':
69: IoctlCode = IOCTL_GPD_WRITE_PORT_USHORT;
70: InputBuffer.ShortData = (USHORT)DataValue;
71: DataLength = offsetof(GENPORT_WRITE_INPUT, ShortData) +
72: sizeof(InputBuffer.ShortData);
73: break;
74:
75: case 'd':
76: IoctlCode = IOCTL_GPD_WRITE_PORT_ULONG;
77: InputBuffer.LongData = (ULONG)DataValue;
78: DataLength = offsetof(GENPORT_WRITE_INPUT, LongData) +
79: sizeof(InputBuffer.LongData);
80: break;
81: }
82:
83: IoctlResult = DeviceIoControl(
84: hndFile, // Handle to device
85: IoctlCode, // IO Control code for Write
86: &InputBuffer, // Buffer to driver. Holds port & data.
87: DataLength, // Length of buffer in bytes.
88: NULL, // Buffer from driver. Not used.
89: 0, // Length of buffer in bytes.
90: &ReturnedLength, // Bytes placed in outbuf. Should be 0.
91: NULL // NULL means wait till I/O completes.
92: );
93:
94:
95: if (IoctlResult) // Did the IOCTL succeed?
96: {
97: printf(
98: "Wrote to port %x the data %x\n",
99: InputBuffer.PortNumber, DataValue);
100: }
101: else
102: {
103: printf(
104: "Ioctl failed with code %ld\n", GetLastError() );
105: }
106:
107:
108: if (!CloseHandle(hndFile)) // Close the Device "file".
109: {
110: printf("Failed to close device.\n");
111: }
112:
113: exit(0);
114: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.