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

C#

 
GeneralRe: WinForms - Zoom In/Out (MS Word-Style) Pin
Alisaunder13-Nov-11 2:41
Alisaunder13-Nov-11 2:41 
AnswerRe: WinForms - Zoom In/Out (MS Word-Style) Pin
BillWoodruff13-Nov-11 11:00
professionalBillWoodruff13-Nov-11 11:00 
QuestionText Entry / Scrolling Dilema Pin
PDTUM12-Nov-11 6:41
PDTUM12-Nov-11 6:41 
AnswerRe: Text Entry / Scrolling Dilema Pin
BillWoodruff13-Nov-11 11:24
professionalBillWoodruff13-Nov-11 11:24 
GeneralRe: Text Entry / Scrolling Dilema Pin
PDTUM14-Nov-11 7:01
PDTUM14-Nov-11 7:01 
QuestionASP.NET Multi Language Pin
koncuk12-Nov-11 3:35
koncuk12-Nov-11 3:35 
AnswerRe: ASP.NET Multi Language Pin
Abhinav S12-Nov-11 21:35
Abhinav S12-Nov-11 21:35 
QuestionConnect and Read from USB Pin
Jassim Rahma12-Nov-11 3:11
Jassim Rahma12-Nov-11 3:11 
AnswerRe: Connect and Read from USB Pin
OriginalGriff12-Nov-11 4:57
mveOriginalGriff12-Nov-11 4:57 
QuestionCreate image from panel control to include child controls Pin
Danzy8312-Nov-11 2:02
Danzy8312-Nov-11 2:02 
AnswerRe: Create image from panel control to include child controls Pin
OriginalGriff12-Nov-11 2:28
mveOriginalGriff12-Nov-11 2:28 
GeneralRe: Create image from panel control to include child controls Pin
Danzy8312-Nov-11 2:40
Danzy8312-Nov-11 2:40 
GeneralRe: Create image from panel control to include child controls Pin
Matt U.12-Nov-11 2:55
Matt U.12-Nov-11 2:55 
GeneralRe: Create image from panel control to include child controls Pin
Danzy8312-Nov-11 3:36
Danzy8312-Nov-11 3:36 
AnswerRe: Create image from panel control to include child controls Pin
Luc Pattyn12-Nov-11 4:09
sitebuilderLuc Pattyn12-Nov-11 4:09 
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 

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.