我找到了一篇关于如何通过调用提升用 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.
我知道以管理员身份启动应用程序(或其他应用程序)是可能且非常容易的,以在单独的进程中执行任务(例如,参见 Daniel Moth 的帖子,但我正在寻找的是一种从在同一个未提升的 .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.
关于如何通过 CoCreateInstanceAsAdmin
API 从 C# 项目中实例化用 C# 编写的 COM 对象的任何想法都会非常有帮助.所以我真的很想学习如何用 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 */ }
}
然后让我的主应用程序通过 CoCreateInstanceAsAdmin
调用来实例化 ElevatedClass
.但也许我只是在做梦.
and then have my main application just instanciate ElevatedClass
through the CoCreateInstanceAsAdmin
call. But maybe I am just dreaming.
看Windows Vista UAC 演示示例代码
(您还需要 UnsafeNativeMethods.CoGetObject 的 Vista Bridge 示例方法)
(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
(不完整的代码示例 - 获取上面的文件)
[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 组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!