如何使用.NET升级COM组件 [英] How to UAC elevate a COM component with .NET
问题描述
我发现了一个文章,介绍如何提升使用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.
我知道,以管理员身份启动应用程序(或另一个应用程序),可以在一个单独的进程中执行任务是可能的(参见例如帖子from Daniel Moth ,but what I am寻找是一种从同一个未升级的.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#中编写COM对象,然后我可以通过COM高级API从C#调用。
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对象提升。如果我可以在专门的程序集中写下以下行:
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 */ }
}
然后让我的主应用程序只是instanciate ElevatedClass
通过 CoCreateInstanceAsAdmin
调用。
and then have my main application just instanciate ElevatedClass
through the CoCreateInstanceAsAdmin
call. But maybe I am just dreaming.
推荐答案
(您还需要针对UnsafeNativeMethods的 Vista Bridge 示例。 CoGetObject方法)
(You also need the Vista Bridge sample for UnsafeNativeMethods.CoGetObject method)
这里给出了C#代码,显示了几种不同的提升方式,包括COM对象
Which gives you C# code that shows a few different ways to elevate, including a COM object
(不完整的代码示例 - 抓取上述文件)
(Incomplete code sample - grab the files above)
[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);
}
这篇关于如何使用.NET升级COM组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!