Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在.NET中接收SENS事件_C#_.net_Com_Com+ - Fatal编程技术网

C# 在.NET中接收SENS事件

C# 在.NET中接收SENS事件,c#,.net,com,com+,C#,.net,Com,Com+,我想在屏幕保护程序打开/关闭时接收SENS事件。我的代码基于以下文章: 问题是es.Store抛出一个System.UnauthorizedAccessException,而访问被拒绝。(HRESULT的异常:0x80070005(E_ACCESSDENIED))您可能会遇到的副作用。您可能需要放宽事件系统dll的安全限制 我会,如果你还没有,首先尝试作为管理员。我刚刚在WinServer2008上测试了你的代码,作为一名完全管理员,没有任何问题 另外,还有一篇关于.Net中事件系统的非常好的文

我想在屏幕保护程序打开/关闭时接收SENS事件。我的代码基于以下文章:


问题是
es.Store
抛出一个System.UnauthorizedAccessException,而
访问被拒绝。(HRESULT的异常:0x80070005(E_ACCESSDENIED))

您可能会遇到的副作用。您可能需要放宽事件系统dll的安全限制

我会,如果你还没有,首先尝试作为管理员。我刚刚在WinServer2008上测试了你的代码,作为一名完全管理员,没有任何问题


另外,还有一篇关于.Net中事件系统的非常好的文章

什么操作系统?如果是Vista或7,那么您可能希望以管理员权限(VS作为管理员权限或可执行文件本身具有管理员权限)执行此代码@Ron抱歉,应该提到这一点。Windows 7和yes使用管理员权限运行修复了它您是完全正确的,以管理员身份运行代码解决了问题-我不认为我需要管理员权限来接收事件(并且在文档中找不到任何关于此的提述..叹气),但异常应该给我一个线索。
private static readonly string GUID = "{" + typeof(ScreensaverHandler).GUID.ToString() + "}";

[ComImport, Guid("4E14FBA2-2E22-11D1-9964-00C04FBBB345")]
private class EventSystem { }

[ComImport, Guid("7542E960-79C7-11D1-88F9-0080C7D771BF")]
private class EventSubcription { }

[ComImport, Guid("AB944620-79C6-11d1-88F9-0080C7D771BF")]
private class EventPublisher { }

[ComImport, Guid("cdbec9c0-7a68-11d1-88f9-0080c7d771bf")]
private class EventClass { }

public ScreensaverHandler() {
    IEventSystem es = (IEventSystem) new EventSystem();
    IEventSubscription sub = (IEventSubscription) new EventSubcription();
    sub.Description = "description";
    sub.SubscriptionName = "subscriptionname";
    sub.SubscriptionID = GUID;
    sub.InterfaceID = GetInterfaceGuid(typeof(SensEvents.ISensLogon));
    sub.SubscriberInterface = this; // implements SensEvents.ISensLogon 
    es.Store("EventSystem.EventSubscription", sub);
}

private static string GetInterfaceGuid(Type type) {
    object[] attributes = type.GetCustomAttributes(typeof(GuidAttribute), true);
    return "{" + ((GuidAttribute)attributes[0]).Value + "}";
}