Is there a managed equivalent of CoCreateInstanceAsAdmin()? RRS feed

  • Question

  • 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.
    Monday, July 3, 2006 11:21 PM


All replies

  • Did this ever go anywhere? I mean, is there now a fully managed sample (caller, COM object) for CreateElevatedComObject?

    Thursday, January 25, 2007 10:51 AM
  • 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?


    Sunday, January 28, 2007 5:53 PM
  • 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;



    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)


    Monday, January 29, 2007 5:23 PM
  • 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)


    Monday, January 29, 2007 5:58 PM
  • 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)"
           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...


    Monday, January 29, 2007 8:00 PM
  • I put together a sample for the benefit of others:


    However, I definitely hit the wall with writing a managed elevated component, mostly for the following reason:


    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?


    Tuesday, January 30, 2007 9:29 AM
  • You can find the final all-managed code solution here:


    It took me a few days, but I finally got there. Hopefully this saves others from searching as long as I had to.


    Monday, February 5, 2007 9:53 PM
  • If click the Managed Elevatation (Interface) button, I get this error:

    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,
    Sunday, February 11, 2007 11:58 PM
  • 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...




    Wednesday, April 4, 2007 5:05 AM
  • Christoph,


    Based on yours I created a project that successfully asks for user permission under Vista, but once granted I get the 0x80070002 error code you were getting. I have been looking at yours and cannot spot were the problem or difference might be. Any help or hint you can provide will be really appreciated.


    Thanks and regards,



    Wednesday, September 26, 2007 10:43 AM
  • I'm poking at this sample and getting the same error - FileNotFoundException - 0x80070002.  Any path to resolution?  I have posted a revised sample here that is show the exception (under Windows 7 x64):


    Friday, February 11, 2011 11:06 PM
  • Following is the exception I have got and the solution that I find it working in my case,

    • FileNotFoundException: The system cannot find the file specified. (Exception from HRESULT: 0x80070002)

            When the assembly is not yet registered to the GAC (C:\Windows\Microsoft.NET\assembly\GAC_32\)

            Solution: register assembly into GAC including its dependencies (in case the interface is in other assembly) and point the LocalizedString to the registered assembly in the GAC

    • COMException: Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))

            When the assembly attribute ComVisible set to false (AssemblyInfo.cs)       

            Solution: in AssemblyInfo.cs  [assembly: ComVisible(true)]

    • COMException: The class is not configured to support Elevated activation. (Exception from HRESULT: 0x80080017)


            For 64-bit OS, instead of registering it under


            found out that putting it under Wow6432Node will resolve the problem.




    • Proposed as answer by Laksmono W Friday, November 9, 2012 3:39 AM
    • Unproposed as answer by Laksmono W Friday, November 9, 2012 5:54 AM
    • Edited by Laksmono W Tuesday, November 13, 2012 2:44 PM
    Friday, November 9, 2012 3:36 AM