Search code examples
c#pinvokewindows-ce

Write to registry using P/Invoke


I need to change a registery value in windowsCE using c# and P/Invoke (RapiDll isn't on there)

I know how to read the key:

private static string ReadRegKey(UIntPtr rootKey, string keyPath, string valueName,string value)
    {
        IntPtr hKey = IntPtr.Zero;
        if (RegOpenKeyEx(rootKey, keyPath, 0, KEY_READ, out hKey) == 0)
        {
            uint size = 1024;
            uint type = 0;
            string keyValue = null;
            StringBuilder keyBuffer = new StringBuilder();
            keyBuffer.Append(value);

            if (RegQueryValueEx(hKey, valueName, IntPtr.Zero, ref type, keyBuffer, ref size) == 0)
                keyValue = keyBuffer.ToString();

            RegCloseKey(hKey);

            return (keyValue);
        }

        return (null);  // Return null if the value could not be read
    }

Can anyone help me with this? (It's for changing the Device name btw)


Solution

  • RegistryKey reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\WJST\WLAN", true); 
    
    // set value of "CDInsert" to 1
    reg.SetValue("CDInsert", 1, RegistryValueKind.DWord);
    
    // get value of "CDInsert"; return 0 if value not found
    int value = (int)reg.GetValue("CDInsert", 0);