EDN Admin
Well-known member
<p align=left><font face=Arial size=2>I am writing an application which need to get the serial number in USB device. I try to use the API of DeviceIoControl with the <a id="ctl00_rs1_mainContentContainer_ctl01 http://msdn2.microsoft.com/en-us/library/aa363411(VS.85).aspx <b>IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER</b> property. But I can not get it work. I got an error code (50) by calling GetLastError() that means The request is not supported. </font>
<p align=left>
<p align=left>------- code in below----
<p align=left>#include <windows.h>
#include <WinIoCtl.h>
#include <stdio.h>
<p align=left>
<p align=left>BOOL GetUsbInfo(MEDIA_SERIAL_NUMBER_DATA *pmty)
{
HANDLE hDevice; // handle to the drive to be examined
BOOL bResult; // results flag
DWORD junk; // discard results
<p align=left> hDevice = CreateFile(TEXT("<a>\\.\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
<p align=left> if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
{
return (FALSE);
}
<p align=left> 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
<p align=left> CloseHandle(hDevice);
<p align=left> return (bResult);
}
<p align=left>int main(int argc, char *argv[])
{
<p align=left>
MEDIA_SERIAL_NUMBER_DATA pmty;
BOOL bResult; // generic results flag
<p align=left>
bResult = GetUsbInfo(&pmty);
if ( bResult)
{
printf("UsbSerialLength = %ldn", pmty.SerialNumberLength);
}
else
{
printf ("GetDriveType failed. Error %ld.n", GetLastError ());
}
<p align=left>
<p align=left>
return ((int)bResult);
}
<p align=left>
View the full article
<p align=left>
<p align=left>------- code in below----
<p align=left>#include <windows.h>
#include <WinIoCtl.h>
#include <stdio.h>
<p align=left>
<p align=left>BOOL GetUsbInfo(MEDIA_SERIAL_NUMBER_DATA *pmty)
{
HANDLE hDevice; // handle to the drive to be examined
BOOL bResult; // results flag
DWORD junk; // discard results
<p align=left> hDevice = CreateFile(TEXT("<a>\\.\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
<p align=left> if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
{
return (FALSE);
}
<p align=left> 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
<p align=left> CloseHandle(hDevice);
<p align=left> return (bResult);
}
<p align=left>int main(int argc, char *argv[])
{
<p align=left>
MEDIA_SERIAL_NUMBER_DATA pmty;
BOOL bResult; // generic results flag
<p align=left>
bResult = GetUsbInfo(&pmty);
if ( bResult)
{
printf("UsbSerialLength = %ldn", pmty.SerialNumberLength);
}
else
{
printf ("GetDriveType failed. Error %ld.n", GetLastError ());
}
<p align=left>
<p align=left>
return ((int)bResult);
}
<p align=left>
View the full article