在 .NET 中捕获 SENS 事件,无 Guid 属性

发布于 2024-08-22 09:14:06 字数 1198 浏览 4 评论 0原文

我正在按照这篇文章通过COM注册SENS事件,但我认为我缺少一些东西。我正在调用文章中所说的 SubscribeToEvents 方法,如下所示:

EventSystemRegistrar.SubscribeToEvents("ManagedSENS EventSubscriber", "ManagedSENS.SensLogonInterop", subscriptionViewerID, this, typeof(SensLogon));

这导致该方法被调用:

private static String GetInterfaceGuid(Type type)
{
    Object[] attributes = type.GetCustomAttributes(typeof(GuidAttribute), true);

    return String.Format("{{{0}}}", ((GuidAttribute)attributes[0]).Value);
}

问题是,那里的类型是他们建议编写的 SensLogon 类,但它没有属性,因此该方法抛出异常。他们说要编写的唯一属性(实际上是 GuidAttributes)是在这些类上,与 SensLogon 类无关(至少据我所知):

[ComImport, Guid("4E14FBA2-2E22-11D1-9964-00C04FBBB345")]
class EventSystem { }
[ComImport, Guid("7542E960-79C7-11D1-88F9-0080C7D771BF")]
class EventSubcription { }
[ComImport, Guid("AB944620-79C6-11d1-88F9-0080C7D771BF")]
class EventPublisher { }
[ComImport, Guid("cdbec9c0-7a68-11d1-88f9-0080c7d771bf")]
class EventClass { }

也许我在这里遗漏了一些东西?我是从这些课程中派生还是其他什么?显示了 SensLogon 类,但它没有任何这些属性。

有没有人做过类似的事情来注册 COM 事件,或者也许可以看看我在哪里不正确地遵循了这篇文章?

I'm following this article to registering SENS events via COM, but I think I'm missing something. I'm calling the SubscribeToEvents method the article says to write, like this:

EventSystemRegistrar.SubscribeToEvents("ManagedSENS EventSubscriber", "ManagedSENS.SensLogonInterop", subscriptionViewerID, this, typeof(SensLogon));

which leads to this method getting called:

private static String GetInterfaceGuid(Type type)
{
    Object[] attributes = type.GetCustomAttributes(typeof(GuidAttribute), true);

    return String.Format("{{{0}}}", ((GuidAttribute)attributes[0]).Value);
}

The problem is, the type there is the SensLogon class they advise writing, but it has no attributes on it, so that method throws an exception. The only attributes, which are, in fact, GuidAttributes, they say to write are on these classes, that have nothing to do with the SensLogon class (at least as far as I can tell):

[ComImport, Guid("4E14FBA2-2E22-11D1-9964-00C04FBBB345")]
class EventSystem { }
[ComImport, Guid("7542E960-79C7-11D1-88F9-0080C7D771BF")]
class EventSubcription { }
[ComImport, Guid("AB944620-79C6-11d1-88F9-0080C7D771BF")]
class EventPublisher { }
[ComImport, Guid("cdbec9c0-7a68-11d1-88f9-0080c7d771bf")]
class EventClass { }

Perhaps I'm missing something here? Was I to derive from these classes or something? The SensLogon class is shown, but it doesn't have any of these attributes.

Has anyone done something similar to register with COM events, or can, perhaps, see where I've followed the article improperly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥抱影子 2024-08-29 09:14:06

我认为您的代码不安全,因为您假设对 type.GetCustomAttributes(...) 的调用在没有检查的情况下工作......我会将其包装在 try/catch 块查看发生了什么...并检查异常...

private static String GetInterfaceGuid(Type type) 
{ 
    string sGuid = string.Empty;
    try{
        Object[] attributes = type.GetCustomAttributes(typeof(GuidAttribute), true); 
        if (attributes != null && attributes.Length >= 1){
           sGuid = String.Format("{{{0}}}", ((GuidAttribute)attributes[0]).Value); 
        }else{
           // FAIL!
        }
    }catch(System.Exception up){
        throw up;
    }
    return sGuid;
} 

ess.dll 是否已注册?您可能需要手动注册?检查 HKEY_CLASSES_ROOT 下的注册表中是否有这些类 id,查看 typelib id...如果它们不存在,则无论 dll 文件位于当前文件夹中的何处,都发出此 regsvr32 ess.dll

希望这有帮助,
此致,
汤姆.

I think your code is unsafe, because you're assuming the call to type.GetCustomAttributes(...) worked without checking....I would wrap this in a try/catch block to see what's happening...and inspect the exception...

private static String GetInterfaceGuid(Type type) 
{ 
    string sGuid = string.Empty;
    try{
        Object[] attributes = type.GetCustomAttributes(typeof(GuidAttribute), true); 
        if (attributes != null && attributes.Length >= 1){
           sGuid = String.Format("{{{0}}}", ((GuidAttribute)attributes[0]).Value); 
        }else{
           // FAIL!
        }
    }catch(System.Exception up){
        throw up;
    }
    return sGuid;
} 

Did the ess.dll get registered at all? You may have to register it manually? Check the registry for those class id's under HKEY_CLASSES_ROOT, look at the typelib id...if they are not there then issue this regsvr32 ess.dll where-ever the dll file is located in the current folder.

Hope this helps,
Best regards,
Tom.

万人眼中万个我 2024-08-29 09:14:06

我想通了。我将 typeof(SensLogon) 传递到 EventSystemRegistrar.SubscribeToEvents 中,而我应该传递 typeof(ISensLogon) (ISensLogon 确实有一个 GuidAttribute)。愚蠢的我。

I figured it out. I was passing typeof(SensLogon) into EventSystemRegistrar.SubscribeToEvents, when I should have been passing typeof(ISensLogon) (ISensLogon does indeed have a GuidAttribute on it). Silly me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文