Click here to Skip to main content
15,889,876 members
Home / Discussions / C#
   

C#

 
GeneralRe: Create image from panel control to include child controls Pin
Danzy8312-Nov-11 4:29
Danzy8312-Nov-11 4:29 
AnswerRe: Create image from panel control to include child controls Pin
Luc Pattyn12-Nov-11 4:38
sitebuilderLuc Pattyn12-Nov-11 4:38 
GeneralRe: Create image from panel control to include child controls Pin
Danzy8312-Nov-11 4:53
Danzy8312-Nov-11 4:53 
AnswerRe: Create image from panel control to include child controls Pin
Luc Pattyn12-Nov-11 5:25
sitebuilderLuc Pattyn12-Nov-11 5:25 
AnswerRe: Create image from panel control to include child controls Pin
OriginalGriff12-Nov-11 4:53
mveOriginalGriff12-Nov-11 4:53 
GeneralRe: Create image from panel control to include child controls Pin
Danzy8312-Nov-11 4:56
Danzy8312-Nov-11 4:56 
AnswerRe: Create image from panel control to include child controls Pin
OriginalGriff12-Nov-11 5:11
mveOriginalGriff12-Nov-11 5:11 
QuestionPInvoke registry value - Help please. Pin
CCodeNewbie11-Nov-11 23:26
CCodeNewbie11-Nov-11 23:26 
Greeting Gurus,

I have been going around in circles trying to get a value from HKLM\Security\Policy\PolEdtEv and would like to ask for your help please. The key has a "Default" name and a REG_NONE type.

The Goal:
Retrieve the value (I'm pretty sure it's hex) which has a default of
00 fa 07 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 09 00 00 00

If the above value is retrieved, change it to
01 fa 07 00 03 00 00 00 03 00 00 00 03 00 00 00 03 00 00 00 03 00 00 00 03 00 00 00 03 00 00 00 03 00 00 00 03 00 00 00 09 00 00 00

This effectively enables Success & Failure audits under Local Policies\Audit Policy

The Problem:
According to MSoft…"GetValue does not support reading values of type REG_NONE or REG_LINK. In both cases, the default value (Nothing) is returned instead of the actual value."

this means you can't use:
RegistryKey RegKey = Registry.LocalMachine;
RegKeyIN = RegKey.OpenSubKey("SECURITY\\Policy\\PolEdtEv");
Object Value = RegKey.GetValue("SOMEVALUE");

…so you need to use P/Invoke, and that’s where I’m stuck

The Solution:
I understand you need to use
C#
[DllImport("advapi32.dll", EntryPoint = "RegOpenKeyEx")]
        public static extern int RegOpenKeyEx(UIntPtr hKey, string lpSubKey, int ulOptions, int samDesired, out string phkResult);
to open the key and then
C#
[DllImport("advapi32.dll",EntryPoint = "RegQueryValueEx")]
        public static extern int RegQueryValueEx(UIntPtr hKey,string lpValueName,int lpReserved,out uint lpType,StringBuilder lpData,ref int lpcbData);
to get the value.

Despite my best efforts and dozens of hours going through many forum examples I just can't get my code to work. I know the following code is a mess but having been over and over and over so many suggested methods and examples I am now at a complete loss. My code (please don't laugh)...
C#
using Microsoft.Win32;
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace ConsoleApplication6
{
    class Program
    {
        public static readonly UIntPtr HKEY_LOCAL_MACHINE = new UIntPtr(0x80000002u);

        public const string lpSubKey = "SECURITY\\Policy\\PolEdtEv";
        
        public const int KEY_READ = 0x20019;
      

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

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

        private static int ReadRegKey(UIntPtr HKEY_LOCAL_MACHINE, string lpSubKey, string valueName)
        {
            if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpSubKey, 0, KEY_READ, out valueName ) == 0)
            {
                Console.WriteLine(valueName);

                int size = 1024;
                uint type;
                string keyValue = null;
                StringBuilder keyBuffer = new StringBuilder();
                
                if (RegQueryValueEx(HKEY_LOCAL_MACHINE, valueName, 0, out type, keyBuffer, ref size) == 0)
                    keyValue = keyBuffer.ToString();

                Console.WriteLine(keyValue);
                return (keyValue);
            }
        }
    }
}


The above returns "not all code paths return a value" which is the fewest errors I have got so far.

Could you help please?
AnswerRe: PInvoke registry value - Help please. Pin
Richard MacCutchan12-Nov-11 0:11
mveRichard MacCutchan12-Nov-11 0:11 
AnswerRe: PInvoke registry value - Help please. Pin
Luc Pattyn12-Nov-11 1:50
sitebuilderLuc Pattyn12-Nov-11 1:50 
AnswerRe: PInvoke registry value - Help please. Pin
CCodeNewbie12-Nov-11 2:42
CCodeNewbie12-Nov-11 2:42 
GeneralRe: PInvoke registry value - Help please. Pin
Richard MacCutchan12-Nov-11 3:20
mveRichard MacCutchan12-Nov-11 3:20 
AnswerRe: PInvoke registry value - Help please. Pin
Luc Pattyn12-Nov-11 4:20
sitebuilderLuc Pattyn12-Nov-11 4:20 
GeneralRe: PInvoke registry value - Help please. Pin
Richard MacCutchan12-Nov-11 5:38
mveRichard MacCutchan12-Nov-11 5:38 
GeneralRe: PInvoke registry value - Help please. Pin
CCodeNewbie12-Nov-11 4:22
CCodeNewbie12-Nov-11 4:22 
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 
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 

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.