I was writing a test program that will perform some actions when a USB Memory Stick is inserted.
When this happens Windows send a Broadcast a WM_DEVICECHANGE message.
The wParam member of this Message contains a (pointer to) a DEV_BROADCAST_HDR structure.
if the dbch_devicetype member of this structure is of type DBT_DEVTYP_VOLUME then we can cast the structure to DEV_BROADCAST_VOLUME.
And finally the dbcv_unitmask member of that structure returns a Bitmask containing the Drive Letter.
A fast and convenient method to convert this Bitmask to a Drive Letter (the first found) is the function below:
function BitMaskToDriveLetter(const Bitmask: Cardinal): Char;
asm
bsr eax, eax; // Find first Bit Set
add eax, 65; // 65 = ASCII value of A
end;