473,387 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Registry Access, easy question hopefully

I am attempting to access the following registry
HKEY_LOCAL_MACHINE\Security\Policy\PolyAdtEv using the following code. This
registry has a binary representation of the settings of the audit policies on
the remote machine. If I can read it then I can interpret it and determine
what the policies are set to. I know I have proper permissions to access
this registry. The error I am getting is that the "Object reference is not
set to an instance of the object" Please let me know what i am leaving out.
Thanks.

RegistryKey environmentKey;
string remotename;

remotename = "3dboxx-7106";

environmentKey =
RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMa chine,remotename)
.OpenSubKey("Security").OpenSubKey("Policy").OpenS ubKey("PolyAdtEv");

// Print the values.
listBox1.Items.Add("There are " + environmentKey.ValueCount.ToString() +
" values for " + environmentKey.Name.ToString() + ".");

foreach(string valueName in environmentKey.GetValueNames())
{
listBox2.Items.Add(environmentKey.GetValue(valueNa me).ToString());
}
environmentKey.Close();
Nov 17 '05 #1
1 1105
Justin wrote:
The error I am getting is that the "Object reference is not
set to an instance of the object"
[ . . . ]
environmentKey =
RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMa chine,remotename)
.OpenSubKey("Security").OpenSubKey("Policy").OpenS ubKey("PolyAdtEv");


Chances are that your problem is here. OpenSubKey returns null if it fails.
Either test each result for this condition before using it or wrap a try
around this one statement and catch the NullReferenceException.
Alternatively, you can use this untested wrapper class to do the checking
for you (you can't use inheritance because RegistryKey is sealed):

class RegistryKeyNotFoundException { /* Implement me */ }

class RegistryKeyWrapper
{
RegistryKey _key;
RegistryKeyWrapper(RegistryKey key) { _key = key; }
public RegistryKeyWrapper OpenSubKey(string name)
{
RegistryKey result = _key.OpenSubKey(name);
if (result == null) throw new RegistryKeyNotFoundException();
return new RegistryKeyWrapper(result);
}
public Key { get { return _key; } }
}

Now you can rewrite your above line like this and not worry about null
checking:

environmentKey =
new RegistryKeyWrapper(
RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMa chine,remotename))
.OpenSubKey("Security").OpenSubKey("Policy").OpenS ubKey("PolyAdtEv").Key;

I hope this helps. When writing about questions like this in the future,
try reproducing the problem in a Debug build and extracting the StackTrace
property of the exception to see exactly what line it occurred on.
--
Derrick Coetzee, MCP, MSFT (Speech Server)
This posting is provided "AS IS" with no warranties, and confers no
rights. Use of included code samples are subject to the terms
specified at http://www.microsoft.com/info/cpyright.htm
Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: RWC | last post by:
Hi! I'm having a problem in A2K2. This should be simple, but I can't really figure it out. First problem, I'm trying to set the property of a table, via Vb to hidden, and I aslo would like to...
21
by: Kevin Swanson | last post by:
I'm attempting some remote registry manipulation via C#. I've written a test app to simply grab a specified key from a specified hive on a specified machine. The call to OpenSubKey is throwing...
9
by: Andrew | last post by:
Apologies for the double-post.. I'm new, just getting used to this.. and should have posted this way in the first place.. How does one go about taking ownership of a registry key using C# & .NET...
4
by: akhare1 | last post by:
OK, before I start, let me clarify a few things here. This is not the run of the mill failure to read a registry key while trying to write to the Event Log. Here's our setup: a) IIS 6.0...
11
by: Josh Flanagan | last post by:
I am trying to write to the event log from ASP.NET, on Windows XP SP1. As soon as I try to write an event (or even query the source with EventLog.SourceExists() or EventLog.LogNameFromSourceName())...
8
by: Al Kaufman | last post by:
I have a simple console app that uses: regSubKey = <some registry key> Dim reg As RegistryKey = Registry.ClassesRoot.OpenSubKey(regSubKey) Dim path As String path = CStr(reg.GetValue(""))
6
by: ewolfman | last post by:
Hi, Is there any way in which I can monitor / hook the Registry, and upon a call to a specific key from a specific application - swap the returned value? I was thinking of using this method...
1
by: mikeeeeeeey | last post by:
Hi all, I've been having this mega-problem for a while now. I'm trying to access the Registry; I've tried using PHP (server-side, so won't work), so I've gone client side, and seeking for the...
3
by: thephatp | last post by:
This is incredibly strange. I used regedit and went to HKEY_LOCAL_MACHINE and create a new subkey under "Software". I added a few more, then went into code to try to read them. I kept getting...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.