Advertisement
Guest User

Erroroem

a guest
Jul 24th, 2012
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.30 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include <setupapi.h>
  4. #include <devguid.h>
  5. #include <regstr.h>
  6.  
  7. #define TOTALBYTES    8192
  8. #define BYTEINCREMENT 4096
  9. #define MODEMFOUND 1
  10. #define MODEMNOTFOUND 0
  11.  
  12.  
  13. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  14. {
  15.    
  16.    
  17.        typedef BOOL ( *fptr1 ) ( PCWSTR , DWORD, PVOID );
  18.            fptr1 SetupUninstallOEMInf;
  19.        
  20.        HDEVINFO hDevInfo;
  21.        SP_DEVINFO_DATA DeviceInfoData;
  22.        DWORD i;
  23.        int DeviceFound_Flag = 0;
  24.        
  25.         // initialisations for RegQuery
  26.        DWORD BufferSize = TOTALBYTES;
  27.        DWORD cbData;
  28.        DWORD dwRet;
  29.        BOOL Modem_Flag = MODEMNOTFOUND;
  30.        
  31.        PPERF_DATA_BLOCK PerfData = (PPERF_DATA_BLOCK) malloc( BufferSize );
  32.        cbData = BufferSize;
  33.        
  34.        
  35.        HINSTANCE hinstLib = LoadLibrary( "Setupapi.dll" );
  36.  
  37.        if ( hinstLib == NULL ) {
  38.         printf ("Load library failed\n");
  39.        }
  40.  
  41.        SetupUninstallOEMInf = (fptr1)GetProcAddress(hinstLib, "SetupUninstallOEMInfA");
  42.  
  43.                      
  44.        hDevInfo = SetupDiGetClassDevs ( NULL,
  45.            NULL, // Enumerator
  46.            0,
  47.            DIGCF_ALLCLASSES );
  48.  
  49.        if (hDevInfo == INVALID_HANDLE_VALUE)
  50.        {
  51.            // Insert error handling here.
  52.            //MessageBox(NULL, "Device not found!!", TEXT ( "Device Status!!" ),
  53.                                  //0 );
  54.  
  55.            return 1;
  56.        }
  57.  
  58.        // Enumerate through all devices in Set.
  59.  
  60.        DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
  61.        if ( (SetupDiEnumDeviceInfo(hDevInfo,0,
  62.            &DeviceInfoData ) == 0 ) ) {
  63.            MessageBox(NULL, "Device not found!!", TEXT ( "Device Status!!" ),
  64.                                  0 );
  65.  
  66.            }
  67.  
  68.        
  69.        
  70.        
  71.        for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,
  72.            &DeviceInfoData);i++)
  73.        {
  74.             DWORD DataT;
  75.             LPTSTR buffer = NULL;
  76.             DWORD buffersize = 0;
  77.             LPTSTR temp_array = NULL;
  78.            //
  79.            // Call function with null to begin with,
  80.            // then use the returned buffer size (doubled)
  81.            // to Alloc the buffer. Keep calling until
  82.            // success or an unknown failure.
  83.            //
  84.            //  Double the returned buffersize to correct
  85.            //  for underlying legacy CM functions that
  86.            //  return an incorrect buffersize value on
  87.            //  DBCS/MBCS systems.
  88.            //
  89.            while (!SetupDiGetDeviceRegistryProperty(
  90.                hDevInfo,
  91.                &DeviceInfoData,
  92.                SPDRP_HARDWAREID,
  93.                &DataT,
  94.                (PBYTE)buffer,
  95.                buffersize,
  96.                &buffersize))
  97.            {
  98.  
  99.             if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
  100.                {
  101.                    // Change the buffer size.
  102.                    if (buffer) LocalFree(buffer);
  103.                    // Double the size to avoid problems on
  104.                    // W2k MBCS systems per KB 888609.
  105.                    buffer = LocalAlloc(LPTR,buffersize * 2);
  106.                }
  107.                else
  108.                {
  109.                    // Insert error handling here.
  110.                    break;
  111.                }
  112.            }
  113.              
  114.              if ( buffer != NULL ) {
  115.              
  116.             if ( ( strnicmp ( buffer , "USB\\VID_16DE&PID_0010", strlen ("USB\\VID_16DE&PID_0010") ) == 0 ) ||
  117.                 ( strnicmp ( buffer , "USB\\VID_16DE&PID_001", strlen ("USB\\VID_16DE&PID_001") ) == 0 ) ||
  118.                 ( strnicmp ( buffer , "USB\\VID_16DE&PID_002", strlen ("USB\\VID_16DE&PID_002") ) == 0 ) ||
  119.                 ( strnicmp ( buffer , "USB\\VID_16DE&PID_003", strlen ("USB\\VID_16DE&PID_003") ) == 0 )   ) {
  120.                 DeviceFound_Flag = 1;
  121.                
  122.                 HKEY hDeviceKey = SetupDiOpenDevRegKey ( hDevInfo, &DeviceInfoData,
  123.                 DICS_FLAG_GLOBAL,
  124.                 0,
  125.                 DIREG_DRV,
  126.                 KEY_READ);
  127.                
  128.                
  129.                 dwRet = RegQueryValueEx (hDeviceKey, "InfPath", NULL, NULL , (LPBYTE) PerfData, &cbData );
  130.                 //printf ("%s\n", PerfData);
  131.                 ////////////////////////
  132.                 while( dwRet == ERROR_MORE_DATA )
  133.                 {
  134.                 // Get a buffer that is big enough.
  135.                     printf ("inside while loop\n");
  136.  
  137.                     BufferSize += BYTEINCREMENT;
  138.                     PerfData = (PPERF_DATA_BLOCK) realloc( PerfData, BufferSize );
  139.                     cbData = BufferSize;
  140.  
  141.                 //printf(".");
  142.                 dwRet = RegQueryValueEx ( hDeviceKey, "InfPath", NULL, NULL, (LPBYTE) PerfData, &cbData );
  143.                 }
  144.                
  145.                 //printf("\n\nFinal buffer size is %d %s\n", BufferSize, PerfData);
  146.                 if( dwRet == ERROR_SUCCESS ) {
  147.                 }
  148.                 else printf ( "\nRegQueryValueEx failed (%d)\n", dwRet );
  149.                
  150.                 printf ( "%s   %s\n",buffer,PerfData );
  151.              
  152.             SetupUninstallOEMInf ( (PCWSTR)PerfData, 0, NULL );
  153.  
  154.              } else {
  155.                     continue;
  156.                }
  157.             }
  158.  
  159.            if (buffer)
  160.            LocalFree(buffer);
  161.  
  162.        }
  163.  
  164.  
  165.        if ( GetLastError()!=NO_ERROR &&
  166.             GetLastError()!=ERROR_NO_MORE_ITEMS )
  167.        {
  168.            // Insert error handling here.
  169.            return 1;
  170.        }
  171.  
  172.        //  Cleanup
  173.        SetupDiDestroyDeviceInfoList(hDevInfo);
  174.      
  175.        
  176.  
  177.  
  178.        return 0;
  179.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement