I have this P/Invoke RegSetValueExW that sets the value to the registry key. in C#
[DllImport('coredll.dll', EntryPoint = 'RegSetValueExW')] public static extern int RegSetValueExW(uint hKey, string lpValueName, uint lpReserved, uint lpType, byte[] lpData, uint lpcbData);
I’m having a problem with the 4th param byte[] lpdata. I need to pass a DWORD with a value of 5 (int) in it. Everything is OK if I pass a string (REG_SZ), just need to convert using the GetBytes function.
If I call GetBytes(‘5’) it converts it to ascii value of 53 so it writes 53 on the registry value instead of 5
I’ve got to start by asking why you are using PInvoke here when there is already a method for setting registry values in the Microsoft.Win32.RegistryKey class? Or are you stuck using an old version of the Compact Framework?
Assuming you have a good reason for the PInvoke, the easiest answer is just to overload the PInvoke declaration for integer values. i.e.: