Chris
The VistaBridge sample from CrossTechnologySamples.exe (SDK) contains VistaBridge\VistaBridgeLibrary\Library\UACManager.cs which sports the method LaunchElevatedCOMObject. However, it's signature is:
[
return: MarshalAs(UnmanagedType.Interface)]Why is it internal? Is there a special reason for this? (like in "it doesn't work") The VistaBridgeDemoApp doesn't have a demonstration for this method (obviously, for the above reason), any input from MS on this one?
Chris
As is, it doesn't work. But I got it up and running, sort of at least. After making the method public instead of internal, plus comparing a C++ example line by line, I changed the code a bit for UACManager.LaunchElevatedCOMObject:
bo.dwClassContext = (
int)NativeMethods.CLSCTX.CLSCTX_LOCAL_SERVER;That way, I was able to elevate an existing example:
private void tryItButton_Click(object sender, EventArgs e){
Guid IID_ITheElevated =new Guid(0x5EFC3EFB, 0xC7D3, 0x4D00, 0xB7, 0x2E, 0x2F, 0x86, 0x4A, 0x1E, 0xAD, 0x06);Guid CLSID_TheElevated = new Guid(0x253E7696, 0xA524, 0x4E49, 0x9E, 0x50, 0xBF, 0xCC, 0x29, 0x91, 0x31, 0x23);object o = UACManager.LaunchElevatedCOMObject(CLSID_TheElevated, IID_ITheElevated);ITheElevated iface = (ITheElevated)o;iface.ShowMe();
}
Problems to solve now:
* releasing the interface (drat, my interop book isn't on me)
* writing a managed COM object to elevate (that's going to be interesting)
Chris
Before I get a bloody nose trying: does the system default DllSurrogate support activating .NET COM components?
(the link has details on the necessary registry settings)
Chris
Ok, I am almost there by copying & adapting registry entries from an ATL component. This means: I do get the prompt (including the customized message), but after the elevation prompt, I receive a 0x80070002 with the following details:
System.IO.FileNotFoundException was unhandled
Message="The system cannot find the file specified. (Exception from HRESULT: 0x80070002)"
Source="VistaBridgeLibrary"
StackTrace:
at Microsoft.SDK.Samples.VistaBridge.Interop.UnsafeNativeMethods.CoGetObject(String pszName, BIND_OPTS3& pBindOptions, Guid riid)
at Microsoft.SDK.Samples.VistaBridge.Services.UACManager.LaunchElevatedCOMObject(Guid Clsid, Guid InterfaceID) in D:\Daten\VSone 2007\UAC\Samples\VistaBridge\VistaBridgeLibrary\Library\UACManager.cs:line 71
I pretty much suspect that I now hit the mscoree.dll "wall" for actually instantiating my managed component via the DllSurrogate. And I also pretty much assume that I now need qualified help from MS on this one...
Chris
I put together a sample for the benefit of others:
http://chrison.net/UACElevationInManagedCodeStartingElevatedCOMComponents.aspx
However, I definitely hit the wall with writing a managed elevated component, mostly for the following reason:
http://chrison.net/AReferenceToCouldNotBeAdded.aspx
This means that even if I were able to create and instantiate a .NET-based COM component, I wouldn't have a way to cast it to an interface and use it (maybe Reflection or talking Interop directly would help).
However, given that you either need an EXE or DLL with the DllSurrogate anyways for the COM "stuff", there isn't much of an advantage (performance-wise as well as coding uglyness) to using plain-vanilla EXE's with a nice command line (how about passing an XML document). Especially given that you only start an adminstrative task, not keep permanently talking to an object. Therefore the penalty paid should be minimal when resorting to plain EXEs.
Comments? Or are we going to leave this as best practice for implementing elevated tasks using managed code?
Chris
You can find the final all-managed code solution here:
http://chrison.net/UACElevationInManagedCodeANETCOMComponentElevated.aspx
It took me a few days, but I finally got there. Hopefully this saves others from searching as long as I had to.
Chris
object
retVal = UnsafeNativeMethods.CoGetObject(monikerName, ref bo, InterfaceID);in the LaunchElevatedCOMObject methodPlease help.
Thank you,
I am lookin at the same example. I implemented the same in another sample example. While executing CoGetObject() method, I am getting "Specified cast is not valid" exception.
What could be the problem ? Can anybodu help ?
Thanks in advance...
tumistry
ARGUMENT EXCEPTION
Value does not fall within the expected range.