Is there a managed equivalent of CoCreateInstanceAsAdmin()?

Better yet, can I build a completely managed component that behaves the same way? Yeah I could build a COM component in .NET but something is wrong here if .NET is getting left out in the cold WRT UAC security.
[210 byte] By [KeithHill] at [2008-3-6]
# 1
Did this ever go anywhere? I mean, is there now a fully managed sample (caller, COM object) for CreateElevatedComObject?

Chris

christophw at 2007-8-30 > top of Msdn Tech,Software Development for Windows Vista,Security for Applications in Windows Vista...
# 2

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)]
static internal object LaunchElevatedCOMObject(Guid Clsid, Guid InterfaceID)
{

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

christophw at 2007-8-30 > top of Msdn Tech,Software Development for Windows Vista,Security for Applications in Windows Vista...
# 3

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

christophw at 2007-8-30 > top of Msdn Tech,Software Development for Windows Vista,Security for Applications in Windows Vista...
# 4

Before I get a bloody nose trying: does the system default DllSurrogate support activating .NET COM components?

http://weblogs.asp.net/kennykerr/archive/2006/09/29/Windows-Vista-for-Developers-_1320_-Part-4-_1320_-User-Account-Control.aspx

(the link has details on the necessary registry settings)

Chris

christophw at 2007-8-30 > top of Msdn Tech,Software Development for Windows Vista,Security for Applications in Windows Vista...
# 5

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

christophw at 2007-8-30 > top of Msdn Tech,Software Development for Windows Vista,Security for Applications in Windows Vista...
# 6

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

christophw at 2007-8-30 > top of Msdn Tech,Software Development for Windows Vista,Security for Applications in Windows Vista...
# 7

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

christophw at 2007-8-30 > top of Msdn Tech,Software Development for Windows Vista,Security for Applications in Windows Vista...
# 8
If click the Managed Elevatation (Interface) button, I get this error:

ARGUMENT EXCEPTION
Value does not fall within the expected range.

The error occurs on this line of code:

object retVal = UnsafeNativeMethods.CoGetObject(monikerName, ref bo, InterfaceID);

in the LaunchElevatedCOMObject method

Please help.

Thank you,

traimo at 2007-8-30 > top of Msdn Tech,Software Development for Windows Vista,Security for Applications in Windows Vista...
# 9

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

tumistry at 2007-8-30 > top of Msdn Tech,Software Development for Windows Vista,Security for Applications in Windows Vista...

Software Development for Windows Vista

Site Classified