DeviceIoControl
returns error code 50 when the requested operation is not supported by the device:
System Error Codes (0-499) (WinError.h) - Win32 apps | Microsoft Learn[
^]
IIRC not all hard drives support serial numbers, so that may be your problem.
And BTW if you are planning on using this to identify your system as part of an anti-piracy policy, I'd suggest it's a pretty poor idea: not all "drives" in a system are physical (mapped to network shares, logical drives within a physical drive, etc.) and it is very, very simple update a system by fitting a larger drive and cloning the old one. Use the processor and / or motherboard instead.