#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>
#include <process.h>
#include <WinIoCtl.h>
#include <Winbase.h>
typedef struct _MEDIA_SERIAL_NUMBER_DATA {
ULONG SerialNumberLength;
ULONG Result;
ULONG Retreived;
DWORD SerialNumberData[];
} MEDIA_SERIAL_NUMBER_DATA, *PMEDIA_SERIAL_NUMBER_DATA; // the structure of IOCTL_MEDIA_SERIAL_NUMBER_DATA
int main() {
HANDLE hard;
bool result;
MEDIA_SERIAL_NUMBER_DATA val;
char buf[sizeof(MEDIA_SERIAL_NUMBER_DATA)];
MEDIA_SERIAL_NUMBER_DATA * p = (MEDIA_SERIAL_NUMBER_DATA *) buf;
hard = CreateFile(L"\\\\.\\C:", 0, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
result = DeviceIoControl(hard, IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER, NULL, 0, buf, sizeof buf, 0, NULL);
if (hard == INVALID_HANDLE_VALUE)
printf("Terminal error : invalid handle value \n");
printf("valeur Handle : %c \n", hard);
printf("valeur retour : %d \n", result);
printf("valeur serial : %d \n", p->SerialNumberData);
printf("valeur longeur num serie : %d \n", p->SerialNumberLength);
CloseHandle(hard); // free the Handle
system("pause");
return 0;
}
当我编译并执行这段代码时,我得到以下结果:
valeur Handle : D valeur retour : 0 valeur serial : 15530164 valeur longeur num serie : -858993460
答案 0 :(得分:0)
考虑阅读一些文档。
printf
都使用错误的格式字符串。 DeviceIoControl
为IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER
返回的结构。 lpOverlapped
为NULL
,则lpBytesReturned
不能为NULL
。IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER
检索 USB设备的序列号。DeviceIoControl
的返回值零表示失败。