Catching SENS Events in .NET, no Guid Attributes
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.Se开发者_StackOverflow社区nsLogonInterop", 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?
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.
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.
精彩评论