⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 talkdriver.c

📁 撰写windows驱动须知(Essentials Of Building Windows Drivers)
💻 C
字号:
//**************************************************************************************************//
//*                                                                                                *//
//* Copyright (C) 2003, James Antognini, antognini@mindspring.com.                                 *//
//*                                                                                                *//
//**************************************************************************************************//

#define TalkDriverName    "TalkDriver"
#define TalkDriverVer     "1.02"

#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <process.h>
#include <commctrl.h>
#include <winioctl.h>
#include "DemoDriver.h"

#define rcOK      0
#define rcError   8

/**************************************************************************************************/      
/*                                                                                                */      
/**************************************************************************************************/      

BOOLEAN
OpenDevice(
           IN LPCTSTR,
           HANDLE *
          );

BOOLEAN
CallDriver(
           HANDLE,
           ULONG,
           PBYTE,
           ULONG,
           PBYTE,
           ULONG
          );

/**************************************************************************************************/      
/*                                                                                                */      
/**************************************************************************************************/      
int
_cdecl
main(
     IN int    argc,
     IN char * pArgv[]
    )
  {
   #define CmdUnknown    0
   #define CmdStart      1
   #define CmdStop       2
   #define CmdTest       3
    
   char             const  DevicePath[] = "\\DosDevices\\",
                           DriverName[] = JADrvNm,
                           DateCompiledBase[] = __DATE__,
                           TimeCompiledBase[] = " "__TIME__;
    
   #define CompDateTimeStr "dd mmm yyyy hh:mm:ss"
    
   char                    PgmCompileInfo[sizeof(CompDateTimeStr)+1],
                           DateCompiled[] =           // Build date in preferred (dd mmm yyyy) format.
                             {DateCompiledBase[4], DateCompiledBase[5], DateCompiledBase[6],
                              DateCompiledBase[0], DateCompiledBase[1], DateCompiledBase[2], DateCompiledBase[3],
                              DateCompiledBase[7], DateCompiledBase[8], DateCompiledBase[9], DateCompiledBase[10],
                              0x0
                             },
                           FullDeviceName[sizeof(DevicePath)+sizeof(DriverName)];
                                                                                        
   HANDLE                  hDevice = INVALID_HANDLE_VALUE;                              
   BOOL                    flag    = FALSE;                                             
   ULONG                   rc = rcOK,                                                     
                           flagOpenDevice,                                             
                           CmdNbr = CmdUnknown;                                                  

   DemoDrvTestStr          lclDemoDrvTestStr;
   pDemoDrvTestStr         plclDemoDrvTestStr =       // Point to parm structure for IOCTL.
                             &lclDemoDrvTestStr;

   if (' '==DateCompiled[0])                          // Is first day a blank?
    strcpy(PgmCompileInfo, DateCompiled+1);
   else
    strcpy(PgmCompileInfo, DateCompiled+0);

   strcat(PgmCompileInfo, TimeCompiledBase);

   printf(TalkDriverName " v" TalkDriverVer " (compiled %s)\n", PgmCompileInfo);

   if (argc!=2)                                       // Wrong number of arguments?
     ;                                                // Do nothing yet
   else
     {
      flag = strcmp(pArgv[1],"start");                // 'start' given?
      if (0==flag)
        CmdNbr = CmdStart;
      else
        {
         flag = strcmp(pArgv[1],"stop");              // 'stop' given?
         if (0==flag)
           CmdNbr = CmdStop;
         else
           {
            flag = strcmp(pArgv[1],"test");           // 'test' given?
            if (0==flag)
              CmdNbr = CmdTest;
            else
              {
              }
           }
        }
     }

   if (CmdUnknown==CmdNbr)                            // A problem?
     {
      printf("Usage:  talkDriver <Start | Stop | Test>\n");

      rc = 1;
      goto ExitPoint;
     }

   strcpy(FullDeviceName, DevicePath);
   strcat(FullDeviceName, DriverName);

   flagOpenDevice = OpenDevice(
                               DriverName,
                               &hDevice
                              );

   if (flagOpenDevice)                                // Success?
     {
      memset(&lclDemoDrvTestStr,                      // Clear parameter structure.
             0,
             sizeof(lclDemoDrvTestStr)
            );

      switch(CmdNbr)
        {
         case CmdTest:
           CallDriver(
                      hDevice,
                      DemoDrv_TEST,
                      (PVOID)&lclDemoDrvTestStr,
                      sizeof(lclDemoDrvTestStr),
                      (PVOID)&lclDemoDrvTestStr,
                      sizeof(lclDemoDrvTestStr)
                     );

           break;

         default:
           printf("Unsupported command\n");

           break;
        }                                             // End switch(CmdNbr).

      CloseHandle(hDevice);
     }                                                // End if(flag).
   else
     {
      printf("OpenDevice result = not OK\n");
      rc = rcError;
     }

ExitPoint:
   return rc;
  }

/**************************************************************************************************/      
/*                                                                                                */      
/**************************************************************************************************/      
BOOLEAN
OpenDevice(
           IN LPCTSTR   DriverName,
           HANDLE     * lphDevice
          )
{
    TCHAR    completeDeviceName[64] = "\\\\.\\";
    HANDLE   hDevice;
    ULONG    lclError;

    //
    // Create a \\.\XXX device name that CreateFile can use
    //

    strcat(completeDeviceName,
           DriverName
          );

    hDevice = CreateFile(
                         completeDeviceName,
                         GENERIC_READ | GENERIC_WRITE,
                         0,
                         NULL,
                         OPEN_EXISTING,
                         FILE_ATTRIBUTE_NORMAL,
                         NULL
                        );

    if (INVALID_HANDLE_VALUE==hDevice)              // hDevice = -1 if failure.
      {
       lclError = GetLastError();                   // Get more information.
       return FALSE;
      }

    // If user wants the handle, give it back.  Otherwise, just close it.

    if (0!=lphDevice)
      *lphDevice = hDevice;
    else
      CloseHandle(hDevice);

    return TRUE;
}

/**************************************************************************************************/      
/*                                                                                                */      
/**************************************************************************************************/      
BOOLEAN
CallDriver(
           HANDLE inHandle,
           ULONG  Msg,
           PBYTE  InData,
           ULONG  InDataLen,
           PBYTE  OutData,
           ULONG  OutDataLen
          )
{
 ULONG   nb,                                         
         ErrorCode;                                                  
 BOOL    resultFlag;
 BOOLEAN rcFlag;
                                                                     
 resultFlag = DeviceIoControl(                                                                                       
                              inHandle,                                                                
                              Msg,                                                                     
                              InData,                                                                  
                              InDataLen,                                                               
                              OutData,                                                                 
                              OutDataLen,                                                              
                              &nb,                                                                     
                              NULL                                                                     
                             );                                                                         
                                                                     
 if (0!=resultFlag)                                                                     
   rcFlag = TRUE;                                                                 
 else                                                                     
   {
    ErrorCode = GetLastError();
    printf("ErrorCode = %08xx\n", ErrorCode);
     
    rcFlag = FALSE;                                                                
   }
                                                                     
 return rcFlag;                                                      
}
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -