https://docs.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-getserviceregistrystatekey is the recommended way to read and write to the registry for a Windows service, as it properly isolates the registry state and is Windows OS agnostic.
This issue tracks using that API to open registry keys instead of opening up registry keys manually.