Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's a simple desktop application that reads a <code>REG_SZ</code> value. It's crude and simple. It will read the value that you want. You may have to adapt it to Silverlight. I cannot help you there!</p> <p>I hope this is useful:</p> <pre><code>using System; using System.ComponentModel; using System.Runtime.InteropServices; using System.Text; namespace ConsoleApplication1 { internal static class NativeMethods { public const int ERROR_SUCCESS = 0; public const uint HKEY_LOCAL_MACHINE = 0x80000002; public const int KEY_READ = 0x20019; [DllImport("advapi32.dll", CharSet = CharSet.Unicode)] public static extern int RegOpenKeyEx( UIntPtr hKey, string subKey, int ulOptions, int samDesired, out UIntPtr hkResult ); [DllImport("advapi32.dll")] public static extern int RegCloseKey( UIntPtr hKey ); [DllImport("advapi32.dll", CharSet = CharSet.Unicode)] public static extern int RegQueryValueEx( UIntPtr hKey, string lpValueName, int lpReserved, IntPtr type, IntPtr lpData, ref int lpcbData ); } internal static class RegistryWrapper { private static void checkErrorCode(int errorCode) { if (errorCode != NativeMethods.ERROR_SUCCESS) throw new Win32Exception(errorCode); } public static string ReadRegString(UIntPtr rootKey, string subKey, string name) { UIntPtr hkey; checkErrorCode(NativeMethods.RegOpenKeyEx(rootKey, subKey, 0, NativeMethods.KEY_READ, out hkey)); try { int cbData = 0; checkErrorCode(NativeMethods.RegQueryValueEx(hkey, name, 0, IntPtr.Zero, IntPtr.Zero, ref cbData)); IntPtr ptr = Marshal.AllocHGlobal(cbData); try { checkErrorCode(NativeMethods.RegQueryValueEx(hkey, name, 0, IntPtr.Zero, ptr, ref cbData)); return Marshal.PtrToStringUni(ptr, cbData / sizeof(char)).TrimEnd('\0'); } finally { Marshal.FreeHGlobal(ptr); } } finally { checkErrorCode(NativeMethods.RegCloseKey(hkey)); } } } class Program { static void Main(string[] args) { Console.WriteLine(RegistryWrapper.ReadRegString((UIntPtr)NativeMethods.HKEY_LOCAL_MACHINE, @"HARDWARE\DEVICEMAP\SERIALCOMM", @"\Device\Serial0")); } } } </code></pre> <p><strong>Update</strong></p> <p>It seems that <code>AllocHGlobal</code> and <code>FreeHGlobal</code> are not available on Silverlight. You can p/invoke to <code>LocalAlloc</code> and <code>LocalFree</code> instead. Or you could use <code>CoTaskMemAlloc</code> and <code>CoTaskMemFree</code>. Here's what the former looks like:</p> <pre><code>[DllImport("kernel32.dll", SetLastError=true)] static extern IntPtr LocalAlloc(uint uFlags, UIntPtr uBytes); [DllImport("kernel32.dll", SetLastError=true)] static extern IntPtr LocalFree(IntPtr hMem); </code></pre> <p>Define <code>LMEM_FIXED</code> like this:</p> <pre><code>const uint LMEM_FIXED = 0x0000; </code></pre> <p>Then replace the call to <code>AllocHGlobal</code> with this:</p> <pre><code>IntPtr ptr = LocalAlloc(LMEM_FIXED, cbData); </code></pre> <p>And replace the call to <code>FreeHGlobal</code> with this:</p> <pre><code>LocalFree(ptr); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload