WriteFile错误代码:1

时间:2016-01-24 16:11:51

标签: 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 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);
}

我在这方面非常糟糕。你能告诉我怎么解决这个问题吗? 司机准备好了(我想)。我需要制作用户模式程序才能使用它

0 个答案:

没有答案