Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: Maurice_Lupin en 17 Noviembre 2011, 17:27 pm



Título: Seriales PenDrive DevC++ (SRC)
Publicado por: Maurice_Lupin en 17 Noviembre 2011, 17:27 pm
Buscando código para VB.NET encontré esto, hice pequeñas modificaciones para que funcione en Dev C++ y googleando encontré la función Split, comparto el resultado

Hay que linkear: -lsetupapi
Código
  1.  
  2. #include <windows.h>
  3. #include <Setupapi.h>
  4. #include <stdio.h>
  5.  
  6. static /*const*/ GUID hidGUID = { 0xA5DCBF10L, 0x6530, 0x11D2,
  7. { 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED } };
  8.  
  9. char **split ( char *string, const char sep) {
  10.  
  11.    char       **lista;
  12.    char       *p = string;
  13.    int         i = 0;
  14.  
  15.    int         pos;
  16.    const int   len = strlen (string);
  17.  
  18.    lista = (char **) malloc (sizeof (char *));
  19.    if (lista == NULL) {                      /* Cannot allocate memory */
  20.        return NULL;
  21.    }
  22.  
  23.    lista[pos=0] = NULL;
  24.  
  25.    while (i <len) {
  26.  
  27.        while ((p[i] == sep) && (i <len))
  28.            i++;
  29.  
  30.        if (i <len) {
  31.  
  32.            char **tmp = (char **) realloc (lista , (pos + 2) * sizeof (char *));
  33.            if (tmp == NULL) {       /* Cannot allocate memory */
  34.                free (lista);
  35.                return NULL;
  36.            }
  37.            lista = tmp;
  38.            tmp = NULL;
  39.  
  40.            lista[pos + 1] = NULL;
  41.            lista[pos] = (char *) malloc (sizeof (char));
  42.            if (lista[pos] == NULL) {         /* Cannot allocate memory */
  43.                for (i = 0; i <pos; i++)
  44.                    free (lista[i]);
  45.                free (lista);
  46.                return NULL;
  47.            }
  48.  
  49.            int j = 0;
  50.            for (i; ((p[i] != sep) && (i <len)); i++) {
  51.                lista[pos][j] = p[i];
  52.                j++;
  53.  
  54.                char *tmp2 = (char *) realloc (lista[pos],(j + 1) * sizeof (char));
  55.                if (lista[pos] == NULL) {     /* Cannot allocate memory */
  56.                    for (i = 0; i <pos; i++)
  57.                        free (lista[i]);
  58.                    free (lista);
  59.                    return NULL;
  60.                }
  61.                lista[pos] = tmp2;
  62.                tmp2 = NULL;
  63.            }
  64.            lista[pos][j] = '\0';
  65.            pos++;
  66.        }
  67.    }
  68.  
  69.    return lista;
  70. }
  71.  
  72. HANDLE connectDeviceNumber(DWORD deviceIndex)
  73. {
  74.    //GUID hidGUID;  
  75.  
  76.  
  77.    HDEVINFO hardwareDeviceInfoSet;
  78.    SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
  79.    PSP_INTERFACE_DEVICE_DETAIL_DATA deviceDetail;
  80.    ULONG requiredSize;
  81.    HANDLE deviceHandle = INVALID_HANDLE_VALUE;
  82.    DWORD result;
  83.  
  84.    //Get the HID GUID value - used as mask to get list of devices
  85.    //HidD_GetHidGuid (&hidGUID);
  86.    //hidGUID = new GUID(GUID GUID_DEVINTERFACE_USB_DEVICE);
  87.  
  88.    //Get a list of devices matching the criteria (hid interface, present)
  89.    hardwareDeviceInfoSet = SetupDiGetClassDevs (&hidGUID,
  90.                                                 NULL, // Define no enumerator (global)
  91.                                                 NULL, // Define no
  92.                                                 (DIGCF_PRESENT | // Only Devices present
  93.                                                 DIGCF_DEVICEINTERFACE)); // Function class devices.
  94.  
  95.    deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
  96.  
  97.    //Go through the list and get the interface data
  98.    result = SetupDiEnumDeviceInterfaces (hardwareDeviceInfoSet,
  99.                                          NULL, //infoData,
  100.                                          &hidGUID, //interfaceClassGuid,
  101.                                          deviceIndex,
  102.                                          &deviceInterfaceData);
  103.  
  104.    /* Failed to get a device - possibly the index is larger than the number of devices */
  105.    if (result == FALSE)
  106.    {
  107.        SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet);
  108.        printf("hidin: -- failed to get specified device number");
  109.        return INVALID_HANDLE_VALUE;
  110.    }
  111.  
  112.    //Get the details with null values to get the required size of the buffer
  113.    SetupDiGetDeviceInterfaceDetail (hardwareDeviceInfoSet,
  114.                                     &deviceInterfaceData,
  115.                                     NULL, //interfaceDetail,
  116.                                     0, //interfaceDetailSize,
  117.                                     &requiredSize,
  118.                                     0); //infoData))
  119.  
  120.    //Allocate the buffer
  121.    deviceDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)malloc(requiredSize);
  122.    deviceDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
  123.  
  124.    //Fill the buffer with the device details
  125.    if (!SetupDiGetDeviceInterfaceDetail (hardwareDeviceInfoSet,
  126.                                          &deviceInterfaceData,
  127.                                          deviceDetail,
  128.                                          requiredSize,
  129.                                          &requiredSize,
  130.                                          NULL))
  131.    {
  132.        SetupDiDestroyDeviceInfoList (hardwareDeviceInfoSet);
  133.        free (deviceDetail);
  134.        printf("hidin: -- failed to get device info");
  135.        return INVALID_HANDLE_VALUE;
  136.    }
  137.  
  138.    char  **listSplit;
  139.  
  140.    listSplit = split(deviceDetail->DevicePath,'#');
  141.  
  142.    //printf("Opening device with path: %s", deviceDetail->DevicePath);
  143.    printf("Serial: %s\n",listSplit[2] );
  144. }
  145.  
  146. int main(int argc, char* argv[]) {
  147.    connectDeviceNumber(0);
  148.  
  149.    getchar();
  150.   return 0;
  151. }
  152.