Click to Rate and Give Feedback
MSDN
MSDN Library
System Services
Device Services
Device Management
 IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMB...

  Switch on low bandwidth view
IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER Control Code

Retrieves the serial number of a USB device.

To perform this operation, call the DeviceIoControl function with the following parameters.

BOOL DeviceIoControl(
  (HANDLE) hDevice,                      // handle to device
  IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER, // dwIoControlCode
  NULL,                                  // lpInBuffer
  0,                                     // nInBufferSize
  (LPVOID) lpOutBuffer,                  // output buffer
  (DWORD) nOutBufferSize,                // size of output buffer
  (LPDWORD) lpBytesReturned,             // number of bytes returned
  (LPOVERLAPPED) lpOverlapped            // OVERLAPPED structure
);

Parameters

hDevice

A handle to the device. To obtain a device handle, call the CreateFile function.

dwIoControlCode

The control code for the operation. Use IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER for this operation.

lpInBuffer

Not used with this operation; set to NULL.

nInBufferSize

Not used with this operation; set to zero.

lpOutBuffer

A pointer to a MEDIA_SERIAL_NUMBER_DATA structure that receives the serial number.

nOutBufferSize

The size of the output buffer, in bytes.

lpBytesReturned

A pointer to a variable that receives the size of the data stored in the output buffer, in bytes.

If the output buffer is too small, the call fails, GetLastError returns ERROR_INSUFFICIENT_BUFFER, and lpBytesReturned is zero.

If lpOverlapped is NULL, lpBytesReturned cannot be NULL. Even when an operation returns no output data and lpOutBuffer is NULL, DeviceIoControl makes use of lpBytesReturned. After such an operation, the value of lpBytesReturned is meaningless.

If lpOverlapped is not NULL, lpBytesReturned can be NULL. If this parameter is not NULL and the operation returns data, lpBytesReturned is meaningless until the overlapped operation has completed. To retrieve the number of bytes returned, call GetOverlappedResult. If hDevice is associated with an I/O completion port, you can retrieve the number of bytes returned by calling GetQueuedCompletionStatus.

lpOverlapped

A pointer to an OVERLAPPED structure.

If hDevice was opened without specifying FILE_FLAG_OVERLAPPED, lpOverlapped is ignored.

If hDevice was opened with the FILE_FLAG_OVERLAPPED flag, the operation is performed as an overlapped (asynchronous) operation. In this case, lpOverlapped must point to a valid OVERLAPPED structure that contains a handle to an event object. Otherwise, the function fails in unpredictable ways.

For overlapped operations, DeviceIoControl returns immediately, and the event object is signaled when the operation has been completed. Otherwise, the function does not return until the operation has been completed or an error occurs.

Return Value

If the operation completes successfully, DeviceIoControl returns a nonzero value.

If the operation fails or is pending, DeviceIoControl returns zero. To get extended error information, call GetLastError.

Requirements

Minimum supported clientWindows XP
Minimum supported serverWindows Server 2003
HeaderWinioctl.h

See Also

Device Management Control Codes
DeviceIoControl
MEDIA_SERIAL_NUMBER_DATA

Send comments about this topic to Microsoft

Build date: 5/28/2009

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Please help me !!      Jimmy Hsueh ... CarolBuchmiller - MSFT   |   Edit   |   Show History

[Carol Buchmiller - MSFT] Please post questions to the MSDN Forums at http://forums.microsoft.com/msdn. You will likely get a quicker response through the forum than through the Community Content.


I try to use this but come out with an error code (50) which means The request is not supported.

Why? I am using VC++ 6.0

------------below is my code------

#include <windows.h>
#include <WinIoCtl.h>
#include <stdio.h>

BOOL GetUsbInfo(MEDIA_SERIAL_NUMBER_DATA *pmty)
{
HANDLE hDevice; // handle to the drive to be examined
BOOL bResult; // results flag
DWORD junk; // discard results

hDevice = CreateFile(TEXT("\\\\.\\F:"), // drive
0, // no access to the drive
FILE_SHARE_READ | // share mode
FILE_SHARE_WRITE,
NULL, // default security attributes
OPEN_EXISTING, // disposition
0, // file attributes
NULL); // do not copy file attributes

if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
{
return (FALSE);
}

bResult = DeviceIoControl(hDevice, // device to be queried
IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER, // operation to perform
NULL, 0, // no input buffer
pmty, sizeof(*pmty), // output buffer
&junk, // # bytes returned
(LPOVERLAPPED) NULL); // synchronous I/O

CloseHandle(hDevice);

return (bResult);
}

int main(int argc, char *argv[])
{

MEDIA_SERIAL_NUMBER_DATA pmty;
BOOL bResult; // generic results flag


bResult = GetUsbInfo(&pmty);
if ( bResult)
{
printf("NumLength = %ld\n", pmty.SerialNumberLength);
}
else
{
printf ("GetUsbInfo failed. Error %ld.\n", GetLastError ());
}


return ((int)bResult);
}

Tags What's this?: Add a tag
Flag as ContentBug
reply      shenhui ... Thomas Lee   |   Edit   |   Show History
This truely is strange, but some USB devices never have serial numbers when they are produced by manufacturer, so they can't reply such request from windows system
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker