devnode.c
上传用户:gzccxsp
上传日期:2015-07-14
资源大小:182k
文件大小:4k
源码类别:

MacOS编程

开发平台:

Visual C++

  1. /*++
  2. Copyright (c) 1998-1998 Microsoft Corporation
  3. Module Name:
  4.     DEVNODE.C
  5. --*/
  6. //*****************************************************************************
  7. // I N C L U D E S
  8. //*****************************************************************************
  9. #include <windows.h>
  10. #include <basetyps.h>
  11. //typedef DWORD LOG_CONF; // Logical configuration.
  12. #include "cfgmgr32.h"
  13. #include <string.h>
  14. //*****************************************************************************
  15. // G L O B A L S
  16. //*****************************************************************************
  17. CHAR buf[512];  // XXXXX How big does this have to be? Dynamically size it?
  18. //*****************************************************************************
  19. //
  20. // DriverNameToDeviceDesc()
  21. //
  22. // Returns the Device Description of the DevNode with the matching DriverName.
  23. // Returns NULL if the matching DevNode is not found.
  24. //
  25. // The caller should copy the returned string buffer instead of just saving
  26. // the pointer value.  XXXXX Dynamically allocate return buffer?
  27. //
  28. //*****************************************************************************
  29. PCHAR DriverNameToDeviceDesc (PCHAR DriverName)
  30. {
  31.     DEVINST     devInst;
  32.     DEVINST     devInstNext;
  33.     CONFIGRET   cr;
  34.     ULONG       walkDone = 0;
  35.     ULONG       len;
  36.     // Get Root DevNode
  37.     //
  38.     cr = CM_Locate_DevNode(&devInst,
  39.                            NULL,
  40.                            0);
  41.     if (cr != CR_SUCCESS)
  42.     {
  43.         return NULL;
  44.     }
  45.     // Do a depth first search for the DevNode with a matching
  46.     // DriverName value
  47.     //
  48.     while (!walkDone)
  49.     {
  50.         // Get the DriverName value
  51.         //
  52.         len = sizeof(buf);
  53.         cr = CM_Get_DevNode_Registry_Property(devInst,
  54.                                               CM_DRP_DRIVER,
  55.                                               NULL,
  56.                                               buf,
  57.                                               &len,
  58.                                               0);
  59.         // If the DriverName value matches, return the DeviceDescription
  60.         //
  61.         if (cr == CR_SUCCESS && strcmp(DriverName, buf) == 0)
  62.         {
  63.             len = sizeof(buf);
  64.             cr = CM_Get_DevNode_Registry_Property(devInst,
  65.                                                   CM_DRP_DEVICEDESC,
  66.                                                   NULL,
  67.                                                   buf,
  68.                                                   &len,
  69.                                                   0);
  70.             if (cr == CR_SUCCESS)
  71.             {
  72.                 return buf;
  73.             }
  74.             else
  75.             {
  76.                 return NULL;
  77.             }
  78.         }
  79.         // This DevNode didn't match, go down a level to the first child.
  80.         //
  81.         cr = CM_Get_Child(&devInstNext,
  82.                           devInst,
  83.                           0);
  84.         if (cr == CR_SUCCESS)
  85.         {
  86.             devInst = devInstNext;
  87.             continue;
  88.         }
  89.         // Can't go down any further, go across to the next sibling.  If
  90.         // there are no more siblings, go back up until there is a sibling.
  91.         // If we can't go up any further, we're back at the root and we're
  92.         // done.
  93.         //
  94.         for (;;)
  95.         {
  96.             cr = CM_Get_Sibling(&devInstNext,
  97.                                 devInst,
  98.                                 0);
  99.             if (cr == CR_SUCCESS)
  100.             {
  101.                 devInst = devInstNext;
  102.                 break;
  103.             }
  104.             cr = CM_Get_Parent(&devInstNext,
  105.                                devInst,
  106.                                0);
  107.             if (cr == CR_SUCCESS)
  108.             {
  109.                 devInst = devInstNext;
  110.             }
  111.             else
  112.             {
  113.                 walkDone = 1;
  114.                 break;
  115.             }
  116.         }
  117.     }
  118.     return NULL;
  119. }