如何提升UAC使用.NET COM组件 [英] How to UAC elevate a COM component with .NET

查看:265
本文介绍了如何提升UAC使用.NET COM组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找到了如何提高C语言编写的COM对象的文章 ++调用
CoCreateInstanceAsAdmin 。但我一直没能找到或做,是落实我的.NET(C#)应用程序作为一个COM对象的组件,然后打电话到该对象执行这就需要UAC提升的任务的方式。 MSDN文档以此为管理COM对象模型

I've found an article on how to elevate a COM object written in C++ by calling CoCreateInstanceAsAdmin. But what I have not been able to find or do, is a way to implement a component of my .NET (c#) application as a COM object and then call into that object to execute the tasks which need UAC elevation. MSDN documents this as the admin COM object model.

我知道这是可能的,很容易启动应用程序(或其他应用程序)作为管理员,在一个单独的进程来执行任务(见实例的post从丹尼尔蛾,但我所寻找的是一个方法,从相同的,联合国框架内尽一切-elevated .NET可执行文件。这样做当然会,在产卵一个新的进程的COM对象,但由于透明的编组,在.NET COM对象的调用者不宜(太多)意识到这一点。

I am aware that it is possible and quite easy to launch the application (or another app) as an administrator, to execute the tasks in a separate process (see for instance the post from Daniel Moth, but what I am looking for is a way to do everything from within the same, un-elevated .NET executable. Doing so will, of course, spawn the COM object in a new process, but thanks to transparent marshalling, the caller of the .NET COM object should not be (too much) aware of it.

任何想法,我怎么能实例化用C#编写的COM对象,从C#项目,通过 CoCreateInstanceAsAdmin API将是非常有益的。所以我在学习如何写在C#中,我可以从C#通过COM API的标高,然后调用COM对象很感兴趣。

Any ideas as to how I could instanciate a COM object written in C#, from a C# project, through the CoCreateInstanceAsAdmin API would be very helpful. So I am really interested in learning how to write a COM object in C#, which I can then invoke from C# through the COM elevation APIs.

没关系,如果高架COM对象不会在同一个进程中运行。我只是不希望有推出提升了整个应用程序;我只是想有COM对象,将执行code升高。如果我写沿线的东西:

Never mind if the elevated COM object does not run in the same process. I just don't want to have to launch the whole application elevated; I would just like to have the COM object which will execute the code be elevated. If I could write something along the lines:

// in a dedicated assembly, marked with the following attributes:
[assembly: ComVisible (true)]
[assembly: Guid ("....")]

public class ElevatedClass
{
    public void X() { /* do something */ }
}

再有我的主要应用程序只是实例化 ElevatedClass 通过 CoCreateInstanceAsAdmin 电话。但是,也许我只是在做梦。

and then have my main application just instanciate ElevatedClass through the CoCreateInstanceAsAdmin call. But maybe I am just dreaming.

推荐答案

看<一个href=\"http://www.microsoft.com/downloads/details.aspx?FamilyID=2cd92e43-6cda-478a-9e3b-4f831e899433&DisplayLang=en\">Windows Vista的UAC演示示例code

(您还需要 Vista的大桥样本UnsafeNativeMethods.CoGetObject法)

(You also need the Vista Bridge sample for UnsafeNativeMethods.CoGetObject method)

它给你的C#code,显示了几种不同的方式来提升,其中包括COM对象

Which gives you C# code that shows a few different ways to elevate, including a COM object

(未完成code样品 - 抢档以上)

[return: MarshalAs(UnmanagedType.Interface)]
static internal object LaunchElevatedCOMObject(Guid Clsid, Guid InterfaceID)
   {
   string CLSID = Clsid.ToString("B"); // B formatting directive: returns {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} 
   string monikerName = "Elevation:Administrator!new:" + CLSID;

   NativeMethods.BIND_OPTS3 bo = new NativeMethods.BIND_OPTS3();
   bo.cbStruct = (uint)Marshal.SizeOf(bo);
   bo.hwnd = IntPtr.Zero;
   bo.dwClassContext = (int)NativeMethods.CLSCTX.CLSCTX_ALL;

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

   return (retVal);
}

这篇关于如何提升UAC使用.NET COM组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆