Click here to Skip to main content
15,881,248 members
Home / Discussions / C#
   

C#

 
AnswerRe: filter interface Pin
Abhinav S30-Jul-13 19:53
Abhinav S30-Jul-13 19:53 
GeneralRe: filter interface Pin
Atul Kumar Singh21-Aug-13 6:35
Atul Kumar Singh21-Aug-13 6:35 
AnswerRe: filter interface Pin
Richard Andrew x641-Aug-13 11:22
professionalRichard Andrew x641-Aug-13 11:22 
QuestionGeneric Report Running Pin
eddieangel30-Jul-13 9:43
eddieangel30-Jul-13 9:43 
AnswerRe: Generic Report Running Pin
Mycroft Holmes30-Jul-13 12:40
professionalMycroft Holmes30-Jul-13 12:40 
GeneralRe: Generic Report Running Pin
eddieangel30-Jul-13 12:49
eddieangel30-Jul-13 12:49 
GeneralRe: Generic Report Running Pin
Mycroft Holmes30-Jul-13 14:20
professionalMycroft Holmes30-Jul-13 14:20 
QuestionHow to monitor and block a registry key change? Pin
turbosupramk330-Jul-13 8:07
turbosupramk330-Jul-13 8:07 
I'm looking for direction on how to do this. I've looked at RegistryMonitor, but I'm having a hard time following how the code works.

I was expecting it to be like a normal event where I could use e.cancel and stop it if needed, but it appears that .net doesn't have that for the registry.

This code example monitors changes on "SYSTEM\CurrentControlSet\Control\Test" in HKLM, but how can I catch it and stop the change from completing?


C#
<pre>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{


    public class RegistryTester
    {
       

        const int INFINITE = -1;
        const int WAIT_OBJECT_0 = 0;
        const long HKEY_LOCAL_MACHINE = 0x80000002L;
        const long REG_NOTIFY_CHANGE_LAST_SET = 0x00000004L;

        [DllImport("advapi32.dll", EntryPoint = "RegNotifyChangeKeyValue")]
        static extern long RegNotifyChangeKeyValue(IntPtr key,
            bool watchSubTree, int notifyFilter, IntPtr regEvent, bool
            async);

        [DllImport("advapi32.dll", EntryPoint = "RegOpenKey")]
        static extern IntPtr RegOpenKey(IntPtr key, String subKey,
            out IntPtr resultSubKey);

        [DllImport("kernel32.dll", EntryPoint = "CreateEvent")]
        static extern IntPtr CreateEvent(IntPtr eventAttributes,
            bool manualReset, bool initialState, String name);

        [DllImport("kernel32.dll", EntryPoint = "WaitForMultipleObjects")]
        static extern unsafe int WaitForMultipleObjects(int numHandles,
            IntPtr* handleArrays, bool waitAll, int timeOut);

        [DllImport("kernel32.dll", EntryPoint = "CloseHandle")]
        static extern IntPtr CloseHandle(IntPtr handle);

        public unsafe static void Main(String[] args)
        {
            args = new string[] { "1", @"SYSTEM\CurrentControlSet\Control\Test" };
            

            int ret = 0;

            if (args.Length < 2)
            {
                Console.WriteLine("Usage: RegistryTester NUMKEYS KEY1 KEY2...");
                return;
            }

            int numKeys = 0;

            try
            {
                numKeys = int.Parse(args[0]);
            }
            catch (Exception)
            {
                Console.WriteLine("Invalid argument for NUMKEYS");
                return;
            }

            if (numKeys != args.Length - 1)
            {
                Console.WriteLine("Did not provide correct number of key arguments.");
                return;
            }

            String[] keys = new String[numKeys];

            for (int i = 0; i < numKeys; i++)
            {
                keys[i] = args[i + 1]; // Start at the 3rd command line arg
            }

            IntPtr[] eventHandles = new IntPtr[numKeys];

            for (int i = 0; i < numKeys; i++)
            {
                eventHandles[i] = CreateEvent((IntPtr)null, false, false,
                    null);

                IntPtr myKey;

                unchecked
                {
                    RegOpenKey(new IntPtr((int)HKEY_LOCAL_MACHINE),
                        keys[i], out myKey);
                }

                RegNotifyChangeKeyValue(myKey, true,
                    (int)REG_NOTIFY_CHANGE_LAST_SET, eventHandles[i],
                     true);
            }

            Console.WriteLine("Waiting on " + numKeys + " keys.");

            fixed (IntPtr* handlePtr = eventHandles)
            {
                ret = WaitForMultipleObjects(numKeys, handlePtr, false,
                    INFINITE);
            }

            Console.WriteLine(keys[ret - WAIT_OBJECT_0] + " was changed.");
            Console.ReadLine();
        }
    }
}




AnswerRe: How to monitor and block a registry key change? Pin
Dave Kreskowiak30-Jul-13 9:27
mveDave Kreskowiak30-Jul-13 9:27 
GeneralRe: How to monitor and block a registry key change? Pin
turbosupramk331-Jul-13 4:13
turbosupramk331-Jul-13 4:13 
GeneralRe: How to monitor and block a registry key change? Pin
Dave Kreskowiak31-Jul-13 5:58
mveDave Kreskowiak31-Jul-13 5:58 
GeneralRe: How to monitor and block a registry key change? Pin
Eddy Vluggen31-Jul-13 8:39
professionalEddy Vluggen31-Jul-13 8:39 
AnswerRe: How to monitor and block a registry key change? Pin
FIorian Schneidereit30-Jul-13 15:54
FIorian Schneidereit30-Jul-13 15:54 
GeneralRe: How to monitor and block a registry key change? Pin
turbosupramk331-Jul-13 2:41
turbosupramk331-Jul-13 2:41 
QuestionProblem in creating Report Pin
Arun kumar Gautam30-Jul-13 3:32
Arun kumar Gautam30-Jul-13 3:32 
AnswerRe: Problem in creating Report Pin
Mycroft Holmes30-Jul-13 12:42
professionalMycroft Holmes30-Jul-13 12:42 
JokeRe: Problem in creating Report Pin
UL UL ALBAB30-Jul-13 23:32
UL UL ALBAB30-Jul-13 23:32 
QuestionSystem,Timers.Timer to slow in C# Pin
Member 994007329-Jul-13 23:28
Member 994007329-Jul-13 23:28 
AnswerRe: System,Timers.Timer to slow in C# Pin
Pete O'Hanlon30-Jul-13 0:23
mvePete O'Hanlon30-Jul-13 0:23 
GeneralRe: System,Timers.Timer to slow in C# Pin
Member 994007330-Jul-13 1:49
Member 994007330-Jul-13 1:49 
GeneralRe: System,Timers.Timer to slow in C# Pin
AmitGajjar30-Jul-13 2:41
professionalAmitGajjar30-Jul-13 2:41 
GeneralRe: System,Timers.Timer to slow in C# Pin
Member 994007330-Jul-13 2:56
Member 994007330-Jul-13 2:56 
GeneralRe: System,Timers.Timer to slow in C# Pin
AmitGajjar30-Jul-13 19:29
professionalAmitGajjar30-Jul-13 19:29 
AnswerRe: System,Timers.Timer to slow in C# Pin
Eddy Vluggen30-Jul-13 0:26
professionalEddy Vluggen30-Jul-13 0:26 
AnswerRe: System,Timers.Timer to slow in C# Pin
Alan N30-Jul-13 1:26
Alan N30-Jul-13 1: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.