Click here to Skip to main content
14,698,734 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my code:

HANDLE HandelUsb= CreateFile(L"\\\\.\\G:", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
    if (HandelUsb == INVALID_HANDLE_VALUE)
    {
        printf("Terminal failure: Unable to open usb ERROR CODE:0x%x\n", GetLastError());
        return 1;
    }
 
    DISK_GEOMETRY d = { 0 };
    DWORD dwReturned = 0;
   
    int gs =DeviceIoControl(HandelUsb, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &d, sizeof(DISK_GEOMETRY),&dwReturned, NULL);
    printf("%d\n", gs);
   
    if (d.MediaType == RemovableMedia)
    {
        DWORD dwReturned2 = 0;
        MEDIA_SERIAL_NUMBER_DATA data={ 0 };
        if (DeviceIoControl(HandelUsb, IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER, NULL, 0, &data,sizeof(MEDIA_SERIAL_NUMBER_DATA), &dwReturned2, NULL)) {
                printf("SerialNumberLength %d\nResult %d\nReserved[2] %s\nSerialNumberData[1] %s ", data.SerialNumberLength, data.Result, data.Reserved, data.SerialNumberData);
        }
        else {
                printf("faild to get serial number ERROR CODE:0x%x\n", GetLastError());
           
        }
    }
 
    CloseHandle(HandelUsb);
    return 0;

Everything works well except for the function
DeviceIoControl(HandelUsb, IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER, NULL, 0, &data,sizeof(MEDIA_SERIAL_NUMBER_DATA), &dwReturned2, NULL)

It always fails (returns a value of 0), GetLastError returns the value 0X1 (problem function).

Maybe someone here can show me what I'm missing here?

* I ran Windus 7

What I have tried:

Change the permissions function CreateFile (Add GENERIC_EXECUTE , replace to GENERIC_ALL add FILE_ATTRIBUTE_NORMAL ), play with the parameters of the DeviceIoControl.
Posted
Comments
Duncan Edwards Jones 5-May-16 5:53am
   
I think you might need FILE_SHARE_READ access to read the device serial number?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900