Click to See Complete Forum and Search --> : Using WMI in ASP.NET web app to update remote Win registry


strangedub@yaho
December 10th, 2002, 12:22 PM
We are in the process of converting some specialized ASP web applications from .asp (using VBScript / Javascript) to ASP.NET (using C#). One of the functions of our applications is to update the Windows registry on a remote host (either the host where the web exists, or another host). Today the server-side VBScript uses WMI to access the registry via the StdRegProv class as follows:
==
Set objRegistry = GetObject("winmgmts:root\default:StdRegProv")
lAccess = KEY_NOTIFY + KEY_QUERY_VALUE + KEY_SET_VALUE
Err.Clear ' Initialize error status
lRC = objRegistry.CheckAccess(HKEY_LOCAL_MACHINE, shortPath, lAccess,bGranted)
==

I would appreciate any help (code samples, links, other docs) in coding this logic in C#. We picked up the string "winmgmts:root\default:StdRegProv" used on the GetObject call from an example at the online MSDN site -- and there seemed to be no real explanation of how this string gets us to a Namespace.

I know we could simply use the Microsoft.Win32 Namespace if our registry access was limited to the localhost (i.e., the web server host), but we really do need to do remote updates as well.

Thanks in advance for any help on this.

-Michael G. Rose
Unisys Corp.
michael.rose@unisys.com

strangedub@yaho
January 14th, 2003, 11:31 AM
Well, after doing a bit of searching and tinkering yesterday, I did get OpenRemoteBaseKey to work for me. As mentioned before, our existing .asp pages use WMI (StdRegProv), so I figured we needed to code up analogous logic in C#... Now, OpenRemoteBaseKey doesn't appear to use WMI, so I'm not exactly sure how it works. (I know WMI uses DCOM under the covers - so perhaps this must also rely on DCOM?)

A code excerpt of what I have at present:
SecurityPermission sp = new SecurityPermission (SecurityPermissionFlag.UnmanagedCode);
RegistryPermission rp = new RegistryPermission (RegistryPermissionAccess.AllAccess,"HKEY_LOCAL_MACHINE");
RegistryKey regkey = RegistryKey.OpenRemoteBaseKey( RegistryHive.LocalMachine, txtTargetComputer);
RegistryKey RegistryBase = regkey.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\W32Time\\Parameters",true); // throws exception
type_val = (string) RegistryBase.GetValue("type");

Anyone know how OpenRemoteBaseKey actually works? With our WMI implementation we found that we could set registry values on a remote machine (not the web server) when we ran the asp app on the web server. The documentation on WMI seemed to indicate that we couldn't impersonate twice -- or delegate. Will OpenRemoteBaseKey provide for this kind of delegation -- that is, running the app on client A; hosting the web on server B; and changing the registry on remote host C ?

Thanks for any further help,
-Michael Rose
Unisys Corp:confused: