Reading remote machine registry key throws error 'Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))'
-
Thursday, January 13, 2011 9:48 AM
Hi,
For my application i need to read and update registry keys to supply values for the dependant applications running on remote machines in the same network.
My computers are not running under domain but they are under same workgroup. But i have administrator access for all the machines. The code i have used to access the remote machine is given below. It throws "Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))" error when i connect to the remote machine. Windows firewall is disabled on both the machines.
Code
string regKeyToGet = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\";
string keyToRead = "DisplayName";
ConnectionOptions oConn = new ConnectionOptions();
oConn.Username ="Administrator";
oConn.Password = "testAdmin123";
ManagementScope scope = new ManagementScope(@"\\SK-CM0005\root\default", oConn);
scope.Options.EnablePrivileges = true;
scope.Connect();
ManagementClass registry = new ManagementClass(scope, new ManagementPath("StdRegProv"), null);
ManagementBaseObject inParams = registry.GetMethodParameters("GetStringValue");
inParams["sSubKeyName"] = regKeyToGet;
inParams["sValueName"] = keyToRead;
ManagementBaseObject outParams = registry.InvokeMethod("GetStringValue", inParams, null);
MessageBox.Show(outParams["sValue"].ToString());
Error is on line
scope.Connect();
Error
Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"
The stack trace
at System.Management.ThreadDispatch.Start()
at System.Management.ManagementScope.Initialize()
at System.Management.ManagementScope.Connect()
at SupplyData.Form1.button5_Click(Object sender, EventArgs e) in E:\Sk\Sample Applications\SupplyData\Form1.cs:line 318
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at USBStorageController.Program.Main() in E:\Sk\Sample Applications\SupplyData\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()Please help me in resolving this issue. It will be great if you can provide some code samples.
Thanks & Regards
Karthikeyan S
- Edited by karthi_keyan_s Thursday, January 13, 2011 9:55 AM re aligned
- Changed Type karthi_keyan_s Thursday, January 13, 2011 10:01 AM
All Replies
-
Friday, January 14, 2011 10:30 AMModerator
Hi karthi_keyan_s,
Thank you for posting.
Your code looks well. It might be related with the configuration of network.
Based on your description, I suggest you to refer the following link.
Try the following command please:
Netsh firewall set service RemoteAdmin
Please see the following article for more detailed information. http://www.aspdeveloper.net/tiki-index.php?page=FirewallWMIandRPC
Best Regards,
Larcolais
Larcolais Gong[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

-
Friday, January 14, 2011 12:20 PM
Hi Larcolais,
I tried this. but still unresolved.
This command works if my connection gets blocked by firewall. But i have disabled the firewall in both the machine. And my issue with the access level. I am not able to gain access to registry even with administrative user of the computer.
Pls. let me know if there are any other idea.
Thanks & Regards
Karthikeyan S
-
Monday, January 17, 2011 5:45 AMModerator
Hi karthi_keyan_s,
Thank you for your feedback.
What is the OS version of remote machine?
Your current status is:
You want to allow a remote user or a remote administrator to connect to the PC or server system registry remotely and view or modify it.
If yes, please check the following link about How to Disable/Restrict Remote Registry Access in Windows.
Best Regards,
Larcolais
Larcolais Gong[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

-
Monday, January 17, 2011 6:47 AM
Hi Larcolais,
The server i am using is windows 2003 and the remote machines are of windows xp and windows 7.
I have already set the remote machine administrator and the administrators group policy to have edit and right permission over the registry. After this also i am getting the same Access denied error in my application.
My requirement: I need to edit the remote machine registry with the rights of remote machine administrator. I know the administrator credentials of the remote machines that i manage, with this credentials i am trying the gain access to the remote registry.
The above given link was helpful but i was not able to add the servermachine role in remote registry permissions.
Thanks and Regards
Karthikeyan S
-
Tuesday, January 18, 2011 3:03 AMModerator
Hi karthi_keyan_s,
From your latest reply, your concern is: how to access the remote machine’s registry?
I suggest you can refer the following links, hope this can help you.
http://support.microsoft.com/kb/555335
http://support.microsoft.com/kb/314837
Furthermore, if you don’t have rights enough to perform some tasks, you can use the impersonation.
Please check the following link: http://csharptuning.blogspot.com/2007/06/impersonation-in-c.html
Best Regards,
Larcolais
Larcolais Gong[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

- Marked As Answer by Larcolais GongModerator Tuesday, January 25, 2011 9:06 AM
-
Sunday, January 23, 2011 3:19 PMModerator
Hi karthi_keyan_s,
Any update?
Best Regards.
Larcolais
Larcolais Gong[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

-
Tuesday, January 25, 2011 9:11 AM
Hi Larcolais,
Sorry for the delayed response...It is still now working. I tried all the procedures stated in the above link but ended in vein.
I have a question at this stage. Do i have to be connected to a domain? Since my machines are in the same network 192.168.2.x and within the same work group "WORKGROUP" and i have the administrator users access of the remote machine and the server machine but even then i get the access restriction error.
Or is there anything that the remote registry cannot be accessed over the network?
Thanks & Regards
Karthikeyan S

