Calling of GetVolumeInformation returns empty serial number

Thomas Jollans tjol at tjol.eu
Tue Nov 7 05:36:00 EST 2017


On 2017-11-07 08:58, Durumdara wrote:
> Hi!
> 
> Windows 10, Python 3.6.
> 
> I want to get the serial number of the drives (without external modules
> like Win32 or WMI).
> It is needed for identification of removable devices (like USB external
> drives).
> 
> Somewhere I saw this code:
> 
> def GetVolumeID(Drive):
>     import ctypes
>     kernel32 = ctypes.windll.kernel32
>     volumeNameBuffer = ctypes.create_unicode_buffer(1024)
>     fileSystemNameBuffer = ctypes.create_unicode_buffer(1024)
>     serial_number = None
>     max_component_length = None
>     file_system_flags = None
> 
>     rc = kernel32.GetVolumeInformationW(
>         ctypes.c_wchar_p(Drive),
>         volumeNameBuffer,
>         ctypes.sizeof(volumeNameBuffer),
>         serial_number,
>         max_component_length,
>         file_system_flags,
>         fileSystemNameBuffer,
>         ctypes.sizeof(fileSystemNameBuffer)
>     )
>     return serial_number;
> 
> print(GetVolumeID('c:\\'))
> 
> 
> This function is working with other values (volumeNameBuffer), but for
> serial  it returns None.
> The serial number is empty.
> How to I pass this parameter to I get the value?

Do you get a value for max_component_length? (I wouldn't expect so, but
that is what you imply).

Anyway, the ctypes docs are pretty clear:
https://docs.python.org/3/library/ctypes.html#passing-pointers-or-passing-parameters-by-reference


> 
> The doc said it's LPDWORD (pointer to DWORD):
> 
> https://msdn.microsoft.com/en-us/library/windows/desktop/aa364993(v=vs.85).aspx
> 
> _Out_opt_ LPDWORD lpVolumeSerialNumber,
> 
> 
> Thank you for any advance in this theme!
> 
> Best wishes
>    dd
> 


-- 
Thomas Jollans



More information about the Python-list mailing list