locked
Get USB drive serial number not work in Windows 8.1 Metro app

    Question

  • Hi,

    I use the following code to get the serial number of usb drive for TSF input method. It works in Windows 8.1 Desktop app and both Windows 8 Metro and Desktop apps but cannot work Windows 8.1 Metro Style Apps. The TSF will be called in every desktop and metro apps and will get the serial number for license registration. 

    How can I make the code in my TSF works in both Metro and Desktop apps? 

    http://oroboro.com/usb-serial-number/#comment-203

    Thursday, November 21, 2013 10:09 AM

Answers

All replies

  • #include <WinIOCtl.h>
    #include <api/usbioctl.h>
    #include <Setupapi.h>
     
    DEFINE_GUID( GUID_DEVINTERFACE_USB_DISK,   
                 0x53f56307L, 0xb6bf, 0x11d0, 0x94, 0xf2, 
                 0x00, 0xa0, 0xc9, 0x1e, 0xfb, 0x8b );
     
    void getDeviceInfo( int vol )
    {
       UsbDeviceInfo info;
     
       // get the device handle
       char devicePath[7] = "\\\\.\\@:";
       devicePath[4] = (char)( vol + 'A' );
     
       HANDLE deviceHandle = CreateFile( devicePath, 0, 
                                         FILE_SHARE_READ | 
                                         FILE_SHARE_WRITE, NULL, 
                                         OPEN_EXISTING, 0, NULL );
       if ( deviceHandle == INVALID_HANDLE_VALUE )
          return;
     
       // to get the device number
       DWORD volumeDeviceNumber = getDeviceNumber( deviceHandle );
       CloseHandle( deviceHandle );
     
       // Get device interface info set handle
       // for all devices attached to the system
       HDEVINFO hDevInfo = SetupDiGetClassDevs( 
          &amp;GUID_DEVINTERFACE_USB_DISK, NULL, NULL,
          DIGCF_PRESENT | DIGCF_DEVICEINTERFACE );
     
       if ( hDevInfo == INVALID_HANDLE_VALUE )  
            return;
     
       // Get a context structure for the device interface
       // of a device information set.
       BYTE Buf[1024];
       PSP_DEVICE_INTERFACE_DETAIL_DATA pspdidd = 
          (PSP_DEVICE_INTERFACE_DETAIL_DATA)Buf;
       SP_DEVICE_INTERFACE_DATA         spdid;
       SP_DEVINFO_DATA                  spdd;
     
       spdid.cbSize = sizeof( spdid );
     
       DWORD dwIndex = 0;
       while ( true )  
       {
          if ( ! SetupDiEnumDeviceInterfaces( hDevInfo, NULL, 
                                              &amp;GUID_DEVINTERFACE_USB_DISK, 
                                              dwIndex, &amp;spdid ))
             break;
     
          DWORD dwSize = 0;
          SetupDiGetDeviceInterfaceDetail( hDevInfo, &amp;spdid, NULL, 
                                           0, &amp;dwSize, NULL );
     
          if (( dwSize != 0 ) &amp;&amp; ( dwSize &lt;= sizeof( Buf )))
          {
             pspdidd->cbSize = sizeof( *pspdidd ); // 5 Bytes!
     
             ZeroMemory((PVOID)&amp;spdd, sizeof(spdd));
             spdd.cbSize = sizeof(spdd);
     
             long res = SetupDiGetDeviceInterfaceDetail( 
                hDevInfo, &amp;spdid, pspdidd,
                dwSize, &amp;dwSize, &amp;spdd );
             if ( res ) 
             {
                HANDLE hDrive = CreateFile( pspdidd-&gt;DevicePath,0,
                                            FILE_SHARE_READ | FILE_SHARE_WRITE,
                                            NULL, OPEN_EXISTING, 0, NULL );
                if ( hDrive != INVALID_HANDLE_VALUE ) 
                {
                   DWORD usbDeviceNumber = getDeviceNumber( hDrive );
     
                   if ( usbDeviceNumber == volumeDeviceNumber ) 
                   {
                      fprintf( "%s", pspdidd-&gt;DevicePath );
                   }
                }
                CloseHandle( hDrive );
             }
          }
          dwIndex++;
       } 
     
       SetupDiDestroyDeviceInfoList(hDevInfo);
       return;  
    }

    • Edited by QQQn Thursday, November 21, 2013 10:12 AM
    Thursday, November 21, 2013 10:09 AM
  • Hi QQQn,

    Welcome here!

    According to your description, I am not sure if you are using WDK 8.1 and VS2013, if not, please install them on the above link.

    Starting with Windows 8.1, SerCx is superceded by SerCx2. SerCx2 has many improvements over SerCx to reduce the size and complexity of serial controller drivers. Which you could get the change between them on

    Serial Controller Drivers Overview

    Buses and ports: New in the WDK for Windows 8.1

    Using Version 2 of the Serial Framework Extension (SerCx2)

    If you want your app works both on Desktop apps and metro style apps, please modify it as shown above.

    Regards!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, November 22, 2013 8:50 AM
    Moderator
  • Hi XiaoLiang,

    I am using VS 2010 and WDK8. I need to make the code work both on Desktop apps and metro style apps.

    Can I still use VS2010 and WDK8.1 to compile the my previous code to solve the problem?

    Thanks.


    • Edited by QQQn Friday, November 22, 2013 9:28 AM
    Friday, November 22, 2013 9:06 AM
  • Hi QQQn,

    No, you need to upgrade as windows official description for running your app on win8.1.

    Regards!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, November 22, 2013 9:29 AM
    Moderator
  • Hi XiaoLiang,

    Any new sample code of SerCx2 about Getting USB drive serial number?

    Thanks.


    Friday, November 22, 2013 9:34 AM
  • Hi,

    I have searched for some time, but without result.

    I will still find new sample code, and maybe you could write one as well, and shared with us. :)

    Regards!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, November 22, 2013 9:55 AM
    Moderator
  • hi, 

    HAHA, I am not a hardware driver guy. Will try my best!!

    Thanks for your quick response.

    Friday, November 22, 2013 10:03 AM
  • Hi QQQn,

    I hope you could have a nice monday!

    Have you resolved your issue?

    I have to say, even if I tried my best to find sample, the result is disappointing. :(

    Maybe there is no new code for it up to now.

    Please refer 3# which I have listed the link for developing.

    Regards!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, November 25, 2013 2:28 AM
    Moderator
  • Hi XiaoLiang,

    The problem have not solved. I will follow #3 and try to find the solution. Thanks.


    Monday, November 25, 2013 3:04 AM
  • Hi QQQn,

    It is so frustrating that I still couldn't find a sample.

    Have you worked it out?

    I will mark my #3 as answer, if you think it doesn't make sence, please unmark it and we could continue to discuss it. :)

    Thanks for understanding about my work :)

    Regards!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, November 28, 2013 3:15 AM
    Moderator