windows - WriteFile Error code : 1 -


task: user-mode programm must send array driver using api writefile

gpdwrite.c(89) error code 1

#include <windows.h> #include <stddef.h> #include <stdio.h> #include <stdlib.h> #include <winioctl.h> #include "gpioctl.h"        // defines ioctl constants. #include <dontuse.h>  void __cdecl main( __in ulong argc, __in_ecount(argc) pchar argv[] ) {  // following returned ioctl.  true if write succeeds. bool    ioctlresult; lpdword writtenbytes; // following parameters used in ioctl call handle              hndfile;        // handle device, obtain createfile genport_write_input inputbuffer;    // input buffer deviceiocontrol long                ioctlcode; ulong               datavalue; ulong               datalength; ulong               returnedlength; // number of bytes returned in output buffer char datastr[20]; int j = 0; (j=0; j<20; j++)     datastr[j] = ' ';   if (argc < 4) {     printf("gpdwrite -b|-w|-d <port#> <value>\n\n");     printf("        byte (-b), word (-w), or doubleword (-d) specified\n");     printf("        value written given port.  ports numbered\n");     printf("        0, 1, ... relative base port assinged driver\n");     printf("        pnp manager according driver preferences\n");     printf("        in inf file. numbers printed in hex.\n");     exit(1); }   hndfile = createfile(                 "\\\\.\\gpddev",           // open device "file"                 generic_write,                 file_share_write,                 null,                 open_existing,                 file_attribute_normal,                 null);  if (hndfile == invalid_handle_value)        // device opened? {     printf("unable open device.\n");     exit(1); }  if (sscanf_s(argv[2], "%x", &inputbuffer.portnumber) == 0) { // port number     printf("sscanf_s failed\n");     exit(1); }  if (sscanf_s(argv[3], "%x", &datavalue) == 0) { // data 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)          { // data written.             printf("sscanf_s failed\n");             exit(1);         }         ioctlcode = ioctl_gpd_write_port_str;         (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 device                     ioctlcode,              // io control code write                     &inputbuffer,           // buffer driver.  holds port & data.                     datalength,             // length of buffer in bytes.                     null,                   // buffer driver.   not used.                     0,                      // length of buffer in bytes.                     &returnedlength,        // bytes placed in outbuf.  should 0.                     null                    // null means wait till i/o completes.                     );   if (ioctlresult)                            // did ioctl succeed? {     printf(         "wrote data %x port %x\n", datavalue,         inputbuffer.portnumber); } else {     printf(         "ioctl failed code %ld\n", getlasterror() ); }  if (!closehandle(hndfile))                  // close device "file". {     printf("failed close device.\n"); }  exit(0); } 

i'm bad in this. can tell me how fix this? driver ready (i think). need make user-mode program using it


Comments