Annotation of q_a/samples/ddk/instdrv/instdrv.c, revision 1.1

1.1     ! root        1: /*++
        !             2: 
        !             3: Copyright (c) 1993  Microsoft Corporation
        !             4: 
        !             5: Module Name:
        !             6: 
        !             7:     Instdrv.c
        !             8: 
        !             9: Abstract:
        !            10: 
        !            11:     A simple Win32 app that installs a device driver
        !            12: 
        !            13: Environment:
        !            14: 
        !            15:     user mode only
        !            16: 
        !            17: Notes:
        !            18: 
        !            19:     See readme.txt
        !            20: 
        !            21: Revision History:
        !            22: 
        !            23:     06-25-93 : created
        !            24: --*/
        !            25: 
        !            26: 
        !            27: 
        !            28: #include <windows.h>
        !            29: #include <stdio.h>
        !            30: #include <stdlib.h>
        !            31: #include <string.h>
        !            32: 
        !            33: 
        !            34: 
        !            35: BOOL
        !            36: InstallDriver(
        !            37:     IN SC_HANDLE  SchSCManager,
        !            38:     IN LPCTSTR    DriverName,
        !            39:     IN LPCTSTR    ServiceExe
        !            40:     );
        !            41: 
        !            42: BOOL
        !            43: RemoveDriver(
        !            44:     IN SC_HANDLE  SchSCManager,
        !            45:     IN LPCTSTR    DriverName
        !            46:     );
        !            47: 
        !            48: BOOL
        !            49: StartDriver(
        !            50:     IN SC_HANDLE  SchSCManager,
        !            51:     IN LPCTSTR    DriverName
        !            52:     );
        !            53: 
        !            54: BOOL
        !            55: StopDriver(
        !            56:     IN SC_HANDLE  SchSCManager,
        !            57:     IN LPCTSTR    DriverName
        !            58:     );
        !            59: 
        !            60: BOOL
        !            61: OpenDevice(
        !            62:     IN LPCTSTR    DriverName
        !            63:     );
        !            64: 
        !            65: 
        !            66: 
        !            67: VOID
        !            68: main(
        !            69:     IN int   argc,
        !            70:     IN char *argv[]
        !            71:     )
        !            72: /*++
        !            73: 
        !            74: Routine Description:
        !            75: 
        !            76: Arguments:
        !            77: 
        !            78: Return Value:
        !            79: 
        !            80: --*/
        !            81: {
        !            82:     SC_HANDLE   schSCManager;
        !            83: 
        !            84:     if (argc != 3)
        !            85:     {
        !            86:         char currentDirectory[128];
        !            87: 
        !            88:         printf ("usage: instdrv <driver name> <.sys location>\n");
        !            89:         printf ("           to install a kernel-mode device driver, or:\n");
        !            90:         printf ("       instdrv <driver name> remove\n");
        !            91:         printf ("           to remove a kernel-mode device driver\n\n");
        !            92: 
        !            93:         GetCurrentDirectory (128,
        !            94:                              currentDirectory
        !            95:                              );
        !            96: 
        !            97:         printf ("       Example: instdrv simpldrv %s\\obj\\i386\\simpldrv.sys\n",
        !            98:                 currentDirectory
        !            99:                 );
        !           100: 
        !           101:         exit (1);
        !           102:     }
        !           103: 
        !           104:     schSCManager = OpenSCManager (NULL,                 // machine (NULL == local)
        !           105:                                   NULL,                 // database (NULL == default)
        !           106:                                   SC_MANAGER_ALL_ACCESS // access required
        !           107:                                   );
        !           108: 
        !           109:     if (!stricmp (argv[2],
        !           110:                   "remove"
        !           111:                   ))
        !           112:     {
        !           113:         StopDriver (schSCManager,
        !           114:                     argv[1]
        !           115:                     );
        !           116: 
        !           117:         RemoveDriver (schSCManager,
        !           118:                       argv[1]
        !           119:                       );
        !           120:     }
        !           121:     else
        !           122:     {
        !           123:         InstallDriver (schSCManager,
        !           124:                        argv[1],
        !           125:                        argv[2]
        !           126:                        );
        !           127: 
        !           128:         StartDriver (schSCManager,
        !           129:                      argv[1]
        !           130:                      );
        !           131: 
        !           132:         OpenDevice (argv[1]);
        !           133: 
        !           134:     }
        !           135: 
        !           136:     CloseServiceHandle (schSCManager);
        !           137: }
        !           138: 
        !           139: 
        !           140: 
        !           141: BOOL
        !           142: InstallDriver(
        !           143:     IN SC_HANDLE  SchSCManager,
        !           144:     IN LPCTSTR    DriverName,
        !           145:     IN LPCTSTR    ServiceExe
        !           146:     )
        !           147: /*++
        !           148: 
        !           149: Routine Description:
        !           150: 
        !           151: Arguments:
        !           152: 
        !           153: Return Value:
        !           154: 
        !           155: --*/
        !           156: {
        !           157:     SC_HANDLE  schService;
        !           158:     DWORD      err;
        !           159: 
        !           160: 
        !           161: 
        !           162:     //
        !           163:     // NOTE: This creates an entry for a standalone driver. If this
        !           164:     //       is modified for use with a driver that requires a Tag,
        !           165:     //       Group, and/or Dependencies, it may be necessary to
        !           166:     //       query the registry for existing driver information
        !           167:     //       (in order to determine a unique Tag, etc.).
        !           168:     //
        !           169: 
        !           170:     schService = CreateService (SchSCManager,          // SCManager database
        !           171:                                 DriverName,           // name of service
        !           172:                                 DriverName,           // name to display
        !           173:                                 SERVICE_ALL_ACCESS,    // desired access
        !           174:                                 SERVICE_KERNEL_DRIVER, // service type
        !           175:                                 SERVICE_DEMAND_START,  // start type
        !           176:                                 SERVICE_ERROR_NORMAL,  // error control type
        !           177:                                 ServiceExe,            // service's binary
        !           178:                                 NULL,                  // no load ordering group
        !           179:                                 NULL,                  // no tag identifier
        !           180:                                 NULL,                  // no dependencies
        !           181:                                 NULL,                  // LocalSystem account
        !           182:                                 NULL                   // no password
        !           183:                                 );
        !           184: 
        !           185:     if (schService == NULL)
        !           186:     {
        !           187:         err = GetLastError();
        !           188: 
        !           189:         if (err == ERROR_SERVICE_EXISTS)
        !           190:         {
        !           191:             //
        !           192:             // A common cause of failure (easier to read than an error code)
        !           193:             //
        !           194: 
        !           195:             printf ("failure: CreateService, ERROR_SERVICE_EXISTS\n");
        !           196:         }
        !           197:         else
        !           198:         {
        !           199:             printf ("failure: CreateService (0x%02x)\n",
        !           200:                     err
        !           201:                     );
        !           202:         }
        !           203: 
        !           204:         return FALSE;
        !           205:     }
        !           206:     else
        !           207:     {
        !           208:         printf ("CreateService SUCCESS\n");
        !           209:     }
        !           210: 
        !           211:     CloseServiceHandle (schService);
        !           212: 
        !           213:     return TRUE;
        !           214: }
        !           215: 
        !           216: 
        !           217: 
        !           218: BOOL
        !           219: RemoveDriver(
        !           220:     IN SC_HANDLE  SchSCManager,
        !           221:     IN LPCTSTR    DriverName
        !           222:     )
        !           223: /*++
        !           224: 
        !           225: Routine Description:
        !           226: 
        !           227: Arguments:
        !           228: 
        !           229: Return Value:
        !           230: 
        !           231: --*/
        !           232: {
        !           233:     SC_HANDLE  schService;
        !           234:     BOOL       ret;
        !           235: 
        !           236:     schService = OpenService (SchSCManager,
        !           237:                               DriverName,
        !           238:                               SERVICE_ALL_ACCESS
        !           239:                               );
        !           240: 
        !           241:     if (schService == NULL)
        !           242:     {
        !           243:         printf ("failure: OpenService (0x%02x)\n", GetLastError());
        !           244:         return FALSE;
        !           245:     }
        !           246: 
        !           247:     ret = DeleteService (schService);
        !           248: 
        !           249:     if (ret)
        !           250:     {
        !           251:         printf ("DeleteService SUCCESS\n");
        !           252:     }
        !           253:     else
        !           254:     {
        !           255:         printf ("failure: DeleteService (0x%02x)\n",
        !           256:                 GetLastError()
        !           257:                 );
        !           258:     }
        !           259: 
        !           260:     CloseServiceHandle (schService);
        !           261: 
        !           262:     return ret;
        !           263: }
        !           264: 
        !           265: 
        !           266: 
        !           267: BOOL
        !           268: StartDriver(
        !           269:     IN SC_HANDLE  SchSCManager,
        !           270:     IN LPCTSTR    DriverName
        !           271:     )
        !           272: {
        !           273:     SC_HANDLE  schService;
        !           274:     BOOL       ret;
        !           275:     DWORD      err;
        !           276: 
        !           277:     schService = OpenService (SchSCManager,
        !           278:                               DriverName,
        !           279:                               SERVICE_ALL_ACCESS
        !           280:                               );
        !           281: 
        !           282:     if (schService == NULL)
        !           283:     {
        !           284:         printf ("failure: OpenService (0x%02x)\n", GetLastError());
        !           285:         return FALSE;
        !           286:     }
        !           287: 
        !           288:     ret = StartService (schService,    // service identifier
        !           289:                         0,             // number of arguments
        !           290:                         NULL           // pointer to arguments
        !           291:                         );
        !           292:     if (ret)
        !           293:     {
        !           294:         printf ("StartService SUCCESS\n");
        !           295:     }
        !           296:     else
        !           297:     {
        !           298:         err = GetLastError();
        !           299: 
        !           300:         if (err == ERROR_SERVICE_ALREADY_RUNNING)
        !           301:         {
        !           302:             //
        !           303:             // A common cause of failure (easier to read than an error code)
        !           304:             //
        !           305: 
        !           306:             printf ("failure: StartService, ERROR_SERVICE_ALREADY_RUNNING\n");
        !           307:         }
        !           308:         else
        !           309:         {
        !           310:             printf ("failure: StartService (0x%02x)\n",
        !           311:                     err
        !           312:                     );
        !           313:         }
        !           314:     }
        !           315: 
        !           316:     CloseServiceHandle (schService);
        !           317: 
        !           318:     return ret;
        !           319: }
        !           320: 
        !           321: 
        !           322: 
        !           323: BOOL
        !           324: StopDriver(
        !           325:     IN SC_HANDLE  SchSCManager,
        !           326:     IN LPCTSTR    DriverName
        !           327:     )
        !           328: {
        !           329:     SC_HANDLE       schService;
        !           330:     BOOL            ret;
        !           331:     SERVICE_STATUS  serviceStatus;
        !           332: 
        !           333:     schService = OpenService (SchSCManager,
        !           334:                               DriverName,
        !           335:                               SERVICE_ALL_ACCESS
        !           336:                               );
        !           337: 
        !           338:     if (schService == NULL)
        !           339:     {
        !           340:         printf ("failure: OpenService (0x%02x)\n", GetLastError());
        !           341:         return FALSE;
        !           342:     }
        !           343: 
        !           344:     ret = ControlService (schService,
        !           345:                           SERVICE_CONTROL_STOP,
        !           346:                           &serviceStatus
        !           347:                           );
        !           348:     if (ret)
        !           349:     {
        !           350:         printf ("ControlService SUCCESS\n");
        !           351:     }
        !           352:     else
        !           353:     {
        !           354:         printf ("failure: ControlService (0x%02x)\n",
        !           355:                 GetLastError()
        !           356:                 );
        !           357:     }
        !           358: 
        !           359:     CloseServiceHandle (schService);
        !           360: 
        !           361:     return ret;
        !           362: }
        !           363: 
        !           364: 
        !           365: 
        !           366: BOOL
        !           367: OpenDevice(
        !           368:     IN LPCTSTR    DriverName
        !           369:     )
        !           370: {
        !           371:     char     completeDeviceName[64] = "";
        !           372:     LPCTSTR  dosDeviceName = DriverName;
        !           373:     HANDLE   hDevice;
        !           374:     BOOL     ret;
        !           375: 
        !           376: 
        !           377: 
        !           378:     //
        !           379:     // Create a \\.\XXX device name that CreateFile can use
        !           380:     //
        !           381:     // NOTE: We're making an assumption here that the driver
        !           382:     //       has created a symbolic link using it's own name
        !           383:     //       (i.e. if the driver has the name "XXX" we assume
        !           384:     //       that it used IoCreateSymbolicLink to create a
        !           385:     //       symbolic link "\DosDevices\XXX". Usually, there
        !           386:     //       is this understanding between related apps/drivers.
        !           387:     //
        !           388:     //       An application might also peruse the DEVICEMAP
        !           389:     //       section of the registry, or use the QueryDosDevice
        !           390:     //       API to enumerate the existing symbolic links in the
        !           391:     //       system.
        !           392:     //
        !           393: 
        !           394:     strcat (completeDeviceName,
        !           395:             "\\\\.\\"
        !           396:             );
        !           397: 
        !           398:     strcat (completeDeviceName,
        !           399:             dosDeviceName
        !           400:             );
        !           401: 
        !           402:     hDevice = CreateFile (completeDeviceName,
        !           403:                           GENERIC_READ | GENERIC_WRITE,
        !           404:                           0,
        !           405:                           NULL,
        !           406:                           OPEN_EXISTING,
        !           407:                           FILE_ATTRIBUTE_NORMAL,
        !           408:                           NULL
        !           409:                           );
        !           410: 
        !           411:     if (hDevice == ((HANDLE)-1))
        !           412:     {
        !           413:         printf ("Can't get a handle to %s\n",
        !           414:                 completeDeviceName
        !           415:                 );
        !           416: 
        !           417:         ret = FALSE;
        !           418:     }
        !           419:     else
        !           420:     {
        !           421:         printf ("CreateFile SUCCESS\n");
        !           422: 
        !           423:         CloseHandle (hDevice);
        !           424: 
        !           425:         ret = TRUE;
        !           426:     }
        !           427: 
        !           428:     return ret;
        !           429: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.