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

📄 talkcallkerneldll.cpp

📁 windows驱动程序
💻 CPP
字号:
//****************************************************************************//
//*                                                                           //
//* Copyright (C) 2003, James Antognini, antognini@mindspring.com.            //
//*                                                                           //
//****************************************************************************//

/**************************************************************************************************/      
/*                                                                                                */      
/* Notes:                                                                                         */      
/*                                                                                                */      
/**************************************************************************************************/      

#define JARtnName         "TalkCallKernelDll"
#define JARtnVer          "1.00"

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

#include "KernelDLL.h"

#define rcOK      0
#define rcError   8

static struct
  {
   char                  * pInStr;
   CallKernelDLLOperType   OperType;
  }
   InStrArr[] =
     {                                                // Except that CmdUnknown has to be first, the following can be in any order.
      {"",       CallKernelDLLUnknown},
      {".",      CallKernelDLLTest}
     };

/**************************************************************************************************/      
/*                                                                                                */      
/* Prototypes (forward defines).                                                                  */      
/*                                                                                                */      
/**************************************************************************************************/      

BOOL
  OpenDevice(IN LPCTSTR, HANDLE *);

BOOL
  CallDriver(HANDLE, int, PBYTE, int, PBYTE, int);

/**************************************************************************************************/      
/*                                                                                                */      
/**************************************************************************************************/      
int main(
         IN int    nbrArgs,
         IN char * pArgv[]
        )
{
 #define rcOK          0
 #define rcError       8

 #define CmdUnknown    0
 #define CmdStart      1
 #define CmdStop       2
 #define CmdTest       3
 #define CmdTest2      4
 #define CmdTest3      5
 #define CmdTest4      6
 #define CmdTest5      7
 #define JADrvNm       "CallKernelDLL"                // Name of driver whose device object will be opened.

 char             static DevicePath[] = "\\DosDevices\\",
                         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
                           },
                         DriverName[] = JADrvNm,
                         FullDeviceName[sizeof(DevicePath)+sizeof(DriverName)];
 HANDLE                  hDevice = INVALID_HANDLE_VALUE;
 int                     rc = rcOK,
                         flagOpenDevice,
                         CmdNbr;
 KernelDLLTestStr        lclKernelDLLTestStr;

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

 strcat(PgmCompileInfo, TimeCompiledBase);

 printf(JARtnName " v" JARtnVer " (compiled %s)\n", PgmCompileInfo);                                                                                   

 CmdNbr = InStrArr[1].OperType;

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

 flagOpenDevice = OpenDevice(
                             DriverName,
                             &hDevice
                            );

 if (TRUE==flagOpenDevice)                            // Success?
   {
    BOOL bFlag;

    switch(CmdNbr)
      {
       case CallKernelDLLTest:
  
         bFlag = CallDriver(
                            hDevice,
                            KernelDLL_TEST,
                            (PBYTE)&lclKernelDLLTestStr,
                            sizeof(KernelDLLTestStr),
                            (PBYTE)&lclKernelDLLTestStr,
                            sizeof(KernelDLLTestStr)
                           );

         if (TRUE==bFlag)
           {
            if (0==lclKernelDLLTestStr.rc)
              printf("  Success\n");
            else
              printf("  " JARtnName ".main():  IOCTL rc = 0x%08x\n", lclKernelDLLTestStr.rc);
           }

         break;

       default:
         printf("  " JARtnName ".main():  Unsupported command\n");

         break;
      }                                               // End switch(CmdNbr).

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

 return rc;
}                                                     // End main().

/**************************************************************************************************/      
/*                                                                                                */      
/**************************************************************************************************/      
BOOL
OpenDevice(
           IN LPCTSTR   DriverName,
           PHANDLE      phDevice
          )
{
 TCHAR    completeDeviceName[64] = "\\\\.\\";
 HANDLE   hDevice;
 DWORD    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 (hDevice==INVALID_HANDLE_VALUE)                 // hDevice = -1 if failure.
   {
    lclError = GetLastError();                      // Get more information.
    return FALSE;
   }

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

 if ( phDevice )
   *phDevice = hDevice;
 else
   CloseHandle( hDevice );

 return TRUE;
}                                                   // End OpenDevice().

/**************************************************************************************************/      
/*                                                                                                */      
/* Sends a DeviceIoControl to the driver along with some data.                                    */      
/*                                                                                                */      
/**************************************************************************************************/      
BOOL
CallDriver(
           HANDLE inHandle,
           int    Msg,
           PBYTE  pInData,
           int    InDataLen,
           PBYTE  pOutData,
           int    OutDataLen
          )
{
 DWORD   ErrorCode,
         nb;
 BOOL    bResult,
         bFlag;
                                                                     
 if (
     KernelDLL_TEST==Msg
    )
   {
    bResult = DeviceIoControl(
                              inHandle,
                              Msg,
                              pInData,
                              InDataLen,
                              pOutData,
                              OutDataLen,
                              &nb,
                              NULL
                             );
   }
 else
   {
    printf("  " JARtnName "().CallDriver():  Unknown code = %08xx\n", Msg);
    bFlag = FALSE;                                                                
        
    goto done;  
   }
                                                                     
 if (TRUE==bResult)                                                                     
   bFlag = TRUE;                                                                 
 else                                                                     
   {
    ErrorCode = GetLastError();
    printf("  " JARtnName "().CallDriver():  ErrorCode = %08xx\n", ErrorCode);
     
    bFlag = FALSE;                                                                
   }
                                                                     
done:
 return bFlag;                                                      
}                                                     // End CallDriver().
 

⌨️ 快捷键说明

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