-2
#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;
}

When I compile and execute this piece of code, I get following result:

valeur Handle : D
valeur retour : 0
valeur serial : 15530164     
valeur longeur num serie : -858993460
Cœur
  • 37,241
  • 25
  • 195
  • 267
JuJi
  • 13
  • 4
  • 4
    1) Indent your code correctly. Currently it is - unreadable. 2) Copy the output that you get, into the question, rather than providing images to it. 3) We can't understand your output anyway, provide it in English. 4) Didn't you look at the question preview? It is there, for a reason. – Algirdas Preidžius Dec 19 '16 at 13:21
  • 2
    If you use C++ stream I/O you'll have fewer problems with types. – molbdnilo Dec 19 '16 at 13:28
  • @molbdnilo: You cannot send device IO control codes using C++ I/O streams. Often a good suggestion, but certainly not in this case. – IInspectable Dec 19 '16 at 13:32
  • @IInspectable I was referring to the `printf`s. – molbdnilo Dec 19 '16 at 14:07
  • @IInspectable I think the problem is in the pointer ,the serial number is returned but the size of serial not – JuJi Dec 19 '16 at 21:33
  • Not at all, the problem is that not a single one of your `printf` calls is correct. – IInspectable Dec 19 '16 at 21:38
  • @IInspectable but if it was then i will get no result on terminal no??? – JuJi Dec 19 '16 at 21:44
  • I'll try with i\o stream and see – JuJi Dec 19 '16 at 21:45
  • Undefined behavior makes no promises. Anything can happen, including to work as intended. It doesn't here, and you're lucky to be on an architecture, that has insanely relaxed alignment restrictions. That type of bug explodes in your face on many architectures. – IInspectable Dec 19 '16 at 21:47

1 Answers1

0

Consider reading some documentation.

  1. Almost all your printfs use the wrong format string.
  2. Your structure is not the structure that DeviceIoControl returns for IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER.
  3. If lpOverlapped is NULL, lpBytesReturned cannot be NULL.
  4. IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER retrieves the serial number of a USB device.
  5. The return value zero from DeviceIoControl indicates failure.
molbdnilo
  • 64,751
  • 3
  • 43
  • 82