Click here to Skip to main content
15,922,015 members
Home / Discussions / C#
   

C#

 
GeneralRe: PInvoke registry value - Help please. Pin
Richard MacCutchan12-Nov-11 6:30
mveRichard MacCutchan12-Nov-11 6:30 
GeneralRe: PInvoke registry value - Help please. Pin
Luc Pattyn12-Nov-11 6:59
sitebuilderLuc Pattyn12-Nov-11 6:59 
GeneralRe: PInvoke registry value - Help please. Pin
Richard MacCutchan12-Nov-11 7:08
mveRichard MacCutchan12-Nov-11 7:08 
AnswerRe: PInvoke registry value - Help please. Pin
Luc Pattyn12-Nov-11 7:28
sitebuilderLuc Pattyn12-Nov-11 7:28 
GeneralRe: PInvoke registry value - Help please. Pin
Richard MacCutchan12-Nov-11 7:35
mveRichard MacCutchan12-Nov-11 7:35 
GeneralRe: PInvoke registry value - Help please. Pin
Luc Pattyn12-Nov-11 7:44
sitebuilderLuc Pattyn12-Nov-11 7:44 
GeneralRe: PInvoke registry value - Help please. Pin
Richard MacCutchan12-Nov-11 8:10
mveRichard MacCutchan12-Nov-11 8:10 
GeneralRe: PInvoke registry value - Help please. Pin
CCodeNewbie12-Nov-11 22:58
CCodeNewbie12-Nov-11 22:58 
Hi Richard,

I tried your code but it errors "does not contain a static Main method". I changed it to "private static int ReadRegKey(UIntPtr HKEY_LOCAL_MACHINE, string lpSubKey, string valueName)" which gave me the error "Cannot covert 'string' to 'int' on "return keyValue".
I explicity declared "string keyValue = keyBuffer.ToString();" and got the "Embedded statement cannot be a declaration or labeled statement" so I removed 'string' from "string keyValue = keyBuffer.ToString();", bracketed the snippet & changed "private static int" back to "private static string" and am now back where I started "ReadKey...: not all code paths return a value" Aaargh!!!

On another note, and trying a different approach, I found that 'Default' key Names can be referred to as "" (open quotes,close quotes with no spaces). So using Luc's IntPtr suggestion I am trying
C#
public static readonly UIntPtr HKEY_LOCAL_MACHINE = new UIntPtr(0x80000002u);
        public const string lpSubKey = "SECURITY\\Policy\\PolEdtEv";
        public const int KEY_READ = 0x20019;
        public const uint REG_NONE = 0x00000001;
        public const string valueName = "";

        [DllImport("advapi32.dll", EntryPoint = "RegOpenKeyEx")]
        public static extern int RegOpenKeyEx(UIntPtr hKey, string lpSubKey, int ulOptions, int samDesired, out UIntPtr phkResult);

        [DllImport("advapi32.dll", EntryPoint = "RegQueryValueEx")]
        public static extern int RegQueryValueEx(UIntPtr hKey, string pSubKey, int reserved, out uint lpType, IntPtr lpData, ref int lpcbData);

        private static int ReadRegKey(UIntPtr HKEY_LOCAL_MACHINE, string lpSubKey, string valueName)
        {
            UIntPtr hKey;
            //int keyValue = 0;
            // final parameter is the returned key from RegOpen

            if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpSubKey, 0, KEY_READ, out hKey) == 0)
            {
                int size = 1024;
                uint type = 0;
                // I could not get StringBuffer to work here
                IntPtr [] keyBuffer = new IntPtr [1024];

                if (RegQueryValueEx(hKey, valueName, 0, out type, keyBuffer, ref size) == 0)
                {
                    int keyValue = keyBuffer;
                    Console.WriteLine(keyBuffer);

                    return keyValue;
                }
            }
            
        }

but I am getting "cannot convert from 'System.IntPtr[]' to 'System.IntPtr'" on the line "if (RegQueryValueEx(hKey, valueName, 0, out type, keyBuffer, ref size) == 0)" and "Cannot implicitly convert type 'System.IntPtr[]' to 'int'" on the line "int keyValue = keyBuffer;"

I have tried replacing the 'IntPtrs' with 'ints' and with 'bytes []' but it makes no difference.Mad | :mad: Confused | :confused: Frown | :(
GeneralRe: PInvoke registry value - Help please. Pin
Richard MacCutchan13-Nov-11 1:23
mveRichard MacCutchan13-Nov-11 1:23 
GeneralRe: PInvoke registry value - Help please. Pin
CCodeNewbie13-Nov-11 2:26
CCodeNewbie13-Nov-11 2:26 
GeneralRe: PInvoke registry value - Help please. Pin
Richard MacCutchan13-Nov-11 3:37
mveRichard MacCutchan13-Nov-11 3:37 
GeneralRe: PInvoke registry value - Help please. Pin
CCodeNewbie13-Nov-11 4:13
CCodeNewbie13-Nov-11 4:13 
GeneralRe: PInvoke registry value - Help please. Pin
Richard MacCutchan13-Nov-11 4:30
mveRichard MacCutchan13-Nov-11 4:30 
AnswerRe: PInvoke registry value - Help please. Pin
Luc Pattyn12-Nov-11 4:35
sitebuilderLuc Pattyn12-Nov-11 4:35 
AnswerRe: PInvoke registry value - Help please. Pin
biop.codeproject13-Nov-11 15:05
biop.codeproject13-Nov-11 15:05 
GeneralRe: PInvoke registry value - Help please. Pin
CCodeNewbie13-Nov-11 22:14
CCodeNewbie13-Nov-11 22:14 
QuestionForm.Show() in a backgroundworker doesn't work Pin
teknolog12311-Nov-11 22:41
teknolog12311-Nov-11 22:41 
AnswerRe: Form.Show() in a backgroundworker doesn't work Pin
Richard MacCutchan11-Nov-11 22:53
mveRichard MacCutchan11-Nov-11 22:53 
GeneralRe: Form.Show() in a backgroundworker doesn't work Pin
teknolog12311-Nov-11 23:48
teknolog12311-Nov-11 23:48 
AnswerRe: Form.Show() in a backgroundworker doesn't work Pin
Luc Pattyn12-Nov-11 1:56
sitebuilderLuc Pattyn12-Nov-11 1:56 
AnswerRe: Form.Show() in a backgroundworker doesn't work Pin
BobJanova13-Nov-11 22:45
BobJanova13-Nov-11 22:45 
QuestionWinForms - Scroll Panel On Mouse Wheel Pin
Matt U.11-Nov-11 14:27
Matt U.11-Nov-11 14:27 
AnswerRe: WinForms - Scroll Panel On Mouse Wheel Pin
BillWoodruff11-Nov-11 15:54
professionalBillWoodruff11-Nov-11 15:54 
GeneralRe: WinForms - Scroll Panel On Mouse Wheel Pin
Matt U.11-Nov-11 16:10
Matt U.11-Nov-11 16:10 
AnswerRe: WinForms - Scroll Panel On Mouse Wheel Pin
OriginalGriff11-Nov-11 21:51
mveOriginalGriff11-Nov-11 21:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.