Windows 写入文件错误代码:1
任务:用户模式程序必须使用API WriteFile将数组发送给驱动程序 gpdwrite.c(89)错误代码1Windows 写入文件错误代码:1,windows,writefile,wdk,Windows,Writefile,Wdk,任务:用户模式程序必须使用API WriteFile将数组发送给驱动程序 gpdwrite.c(89)错误代码1 #include <windows.h> #include <stddef.h> #include <stdio.h> #include <stdlib.h> #include <winioctl.h> #include "gpioctl.h" // This defines the IOCTL constan
#include <windows.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <winioctl.h>
#include "gpioctl.h" // This defines the IOCTL constants.
#include <dontuse.h>
VOID __cdecl main(
__in ULONG argc,
__in_ecount(argc) PCHAR argv[]
)
{
// The following is returned by IOCTL. It is true if the write succeeds.
BOOL IoctlResult;
LPDWORD writtenBytes;
// The following parameters are used in the IOCTL call
HANDLE hndFile; // Handle to device, obtain from CreateFile
GENPORT_WRITE_INPUT InputBuffer; // Input buffer for DeviceIoControl
LONG IoctlCode;
ULONG DataValue;
ULONG DataLength;
ULONG ReturnedLength; // Number of bytes returned in output buffer
char DataStr[20];
int j = 0;
for (j=0; j<20; j++)
DataStr[j] = ' ';
if (argc < 4)
{
printf("GpdWrite -b|-w|-d <Port#> <Value>\n\n");
printf(" The byte (-b), word (-w), or doubleword (-d) specified\n");
printf(" as value is written to the given port. Ports are numbered\n");
printf(" as 0, 1, ... relative to the base port assinged to the driver\n");
printf(" by the PNP manager according to the driver preferences\n");
printf(" in the INF file. All numbers are printed in hex.\n");
exit(1);
}
hndFile = CreateFile(
"\\\\.\\GpdDev", // Open the Device "file"
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hndFile == INVALID_HANDLE_VALUE) // Was the device opened?
{
printf("Unable to open the device.\n");
exit(1);
}
if (sscanf_s(argv[2], "%x", &InputBuffer.PortNumber) == 0) { // Get the port number
printf("sscanf_s failed\n");
exit(1);
}
if (sscanf_s(argv[3], "%x", &DataValue) == 0) { // Get the data to be written.
printf("sscanf_s failed scan hex\n");
//exit(1);
}
//***********************************************************************************************
if (!WriteFile(hndFile,DataStr,sizeof(DataStr), 0, NULL))
{
printf("WriteFile failed!!! %ld\n",GetLastError());
}
//***********************************************************************************************
switch (argv[1][1])
{
/*case 'b':
IoctlCode = IOCTL_GPD_WRITE_PORT_UCHAR;
InputBuffer.CharData = (UCHAR)DataValue;
DataLength = offsetof(GENPORT_WRITE_INPUT, CharData) +
sizeof(InputBuffer.CharData);
printf("DataValue = %d (UCHAR)DataValue = %d\n",DataValue, (UCHAR)DataValue);
break;
case 'w':
IoctlCode = IOCTL_GPD_WRITE_PORT_USHORT;
InputBuffer.ShortData = (USHORT)DataValue;
DataLength = offsetof(GENPORT_WRITE_INPUT, ShortData) +
sizeof(InputBuffer.ShortData);
break;
case 'd':
IoctlCode = IOCTL_GPD_WRITE_PORT_ULONG;
InputBuffer.LongData = (ULONG)DataValue;
DataLength = offsetof(GENPORT_WRITE_INPUT, LongData) +
sizeof(InputBuffer.LongData);
break;
*/
case 'c':
if (sscanf_s(argv[3], "%19s", DataStr,20) == 0)
{ // Get the data to be written.
printf("sscanf_s failed\n");
exit(1);
}
IoctlCode = IOCTL_GPD_WRITE_PORT_STR;
for (j=0;j < 20;j++)
{
InputBuffer.MyStr[j] = DataStr[j];
}
DataLength = offsetof(GENPORT_WRITE_INPUT, MyStr) + sizeof(InputBuffer.MyStr);
//WriteFile(hndFile,(void *)DataValue,sizeof(DataValue),NULL,NULL);
break;
default:
printf("Wrong command line argument %c\n", argv[1][1]);
exit(1);
}
IoctlResult = DeviceIoControl(
hndFile, // Handle to device
IoctlCode, // IO Control code for Write
&InputBuffer, // Buffer to driver. Holds port & data.
DataLength, // Length of buffer in bytes.
NULL, // Buffer from driver. Not used.
0, // Length of buffer in bytes.
&ReturnedLength, // Bytes placed in outbuf. Should be 0.
NULL // NULL means wait till I/O completes.
);
if (IoctlResult) // Did the IOCTL succeed?
{
printf(
"Wrote data %x to port %x\n", DataValue,
InputBuffer.PortNumber);
}
else
{
printf(
"Ioctl failed with code %ld\n", GetLastError() );
}
if (!CloseHandle(hndFile)) // Close the Device "file".
{
printf("Failed to close device.\n");
}
exit(0);
}
#包括
#包括
#包括
#包括
#包括
#包括“gpioctl.h”//这定义了IOCTL常量。
#包括
VOID\uu cdecl干管(
__在ULONG argc,
__输入帐户(argc)PCHAR argv[]
)
{
//IOCTL返回以下内容。如果写入成功,则为true。
BOOL-ioctl结果;
LPDWORD写十字节;
//以下参数用于IOCTL调用
HANDLE hndFile;//设备句柄,从CreateFile获取
GENPORT\u WRITE\u INPUT InputBuffer;//设备控制的输入缓冲区
长ioctl码;
乌龙数据值;
乌龙数据长度;
ULONG ReturnedLength;//输出缓冲区中返回的字节数
char-DataStr[20];
int j=0;
对于(j=0;jIMO,问题最有可能出现在驱动程序中。(这或者您正在编写的数据的大小或内容无效。)您是否设法解决了问题?我收到了与您相同的错误,我不知道为什么。谢谢