dotnet interop:
I would like to know what is required in order to create a C# class that can fire events to a COM object. I have seen many posts of the reverse (catching COM object events in C#), but not many of the case where the C# class is firing the events. As an example, consider a MyProject Active X DLL project with a MyClass class defined as follows: Option Explicit Public Event MyEvent(ByVal MyData As String) Public Function Initialize(ByVal fvbStart As Boolean) As String End Function Then, I create a C# class (reference the VB interface DLL, turn COMVisible on, and implement the interface) as follows: using System; using System.Runtime.InteropServices; namespace CSharpTest { [Guid("b05432d9-5a53-4bff-b1dd-68d5fe414f16")] public class InterfaceTestClass : MyProject.MyClass { public InterfaceTestClass() { return; } public string Initialize(bool fvbStart) { return "OK"; } public event MyProject.__MyClass_MyEventEventHandler MyEvent; } } Finally, I create a VB EXE (simple form with 1 button) as follows to use my C# component: Option Explicit Dim MyTest1 As MyProject.MyClass Dim WithEvents MyTest2 As MyProject.MyClass Private Sub Command1_Click() Set MyTest1 = CreateObject("CSharpTest.InterfaceTestClass") Set MyTest2 = CreateObject("CSharpTest.InterfaceTestClass") End Sub Every time, I get a "Runtime Error 459 - Object or class does not support the set of events". What must I do so that my C# object will expose that it does events? I have seen reference to the ComSourceInterfaces attribute, but I'm not sure how to use that in this case as I've only seen it demonstrated where the event interface is defined in C#. I have attached the samples I created to demonstrate this problem. Any help would be greatly appreciated! Thanks, Tyler begin 666 Sample.zip M4$L#! H``````"X,7S$````````````````+`) `0U-H87)P5&5S="]31'L` M``$````(``#QZWQC9&!I$&%@8#!@@ `?(&9D!3-918'$_=1TAGV9AAY6]2JF MZUAPRS$R,3 P,6QA8 ?),DLP_&>49P")@=0J``D%$)M9!"+."!$7`E,J$#$\ M=C)P@US(( #5Q\P`M6,E@Q"*'8H@-I,$`T@+-G$F+.(`550-``<WAX1!.(>$ M02*%A$%02P,$% `"``@`EKM>,;HQ^.G.`P``P0D``!H`>@!#4VAA<G!497-T M+T%S<V5M8FQY26YF;RYC<U-$90"\``````@`_1C>*F-D8&D086!@,&" `!\@ M9F0%,UE%@<3]U'2&?9F&'E;U*J;K6'#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9RYW >6ED**A;)>F*P2;L6JRIBP8L$KQ41N4<:A9U%1OB?Z#92R%B<.QJ;Q M5(HW2%777M$@PEY$,!I@ER+7G0Y02+T084RP1\=^1$.RMF^L6\LF\6M2%C.0 MRNX=5DXJ"\VKQ=4X]/O^Q!L@,AP/D><'&D=&?33R/3? N$\GWFAMFT:G=1#K M3]OJMN44"L@SR%,&LMY9"*G:EIZ)\NQXS#3VDFG<<3%/N'[77+;H;1Y$Y NV MK,JD+C;P$K8]/=29PKQ::GESUL(S<$@D:*F]-1:.Z"?;6C>GR_2?'&X[+REG
Hi I will research the issue and update you with new information ASAP. Best regards, Peter Huang Microsoft Online Partner Support Get Secure! - www.microsoft.com/security This posting is provided "AS IS" with no warranties, and confers no rights.
Hi Tyler, Sorry for delay responding. For how to use ComSourceInterfaces attribute, here is an article, here is an article, if you still have any concern on this issue, please feel free to post here. A COM class wizard for VS.NET 2003 http://www.thecodeproject.com/csharp/CSComTemplate.asp Best regards, Peter Huang Microsoft Online Partner Support Get Secure! - www.microsoft.com/security This posting is provided "AS IS" with no warranties, and confers no rights.
Thanks Peter, The document you reference describes how to create/hook-up a *new* event sink in C# that can be used by VB projects, but it doesn't really cover how to implement one that is defined in VB. I have managed to come up with something that works, but it requires me to define the interface (in its entirety) in C#. I do not understand why I must "re-define" the interface in C# to make it work when my VB source project has already defined the interface. For example, I know that if I add the following lines to the top of the InterfaceTestClass.cs file that it will send the events successfully: // Step 1: Defines an event sink interface (MyEvents) to be // implemented by the COM sink. [GuidAttribute("F258F359-919E-48F3-BA8F-782D99FA6675")] [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)] [ComImport(), ComVisible(false)] public interface Dummy { void MyEvent(string fvsData); } [Guid("b05432d9-5a53-4bff-b1dd-68d5fe414f16"), ComSourceInterfaces(typeof(Dummy)), ClassInterface(ClassInterfaceType.None)] public class InterfaceTestClass : MyProject.MyClass ... However, if I simply add the following lines it doesn't work: [Guid("b05432d9-5a53-4bff-b1dd-68d5fe414f16"), ComSourceInterfaces(typeof(MyProject.__MyClass)), ClassInterface(ClassInterfaceType.None)] public class InterfaceTestClass : MyProject.MyClass ... If you look at the type library that is generated in both cases, it is equivalent (there are not differences): // Generated .IDL file (by the OLE/COM Object Viewer) // // typelib filename: CSharpTest.tlb [ uuid(7190690E-409C-37A5-B67C-B9BE9BFB1BD3), version(1.0), custom(90883F05-3D28-11D2-8F17-00A0C9A6186D, CSharpTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null) ] library CSharpTest { // TLib : // TLib : : {8C51CF60-49E5-4F3E-880E-7E85F6BF67D7} importlib("MyProject.dll"); // TLib : Common Language Runtime Library : {BED7F4EA-1A96-11D2-8F08-00A0C9A6186D} importlib("mscorlib.tlb"); // Forward declare all types defined in this typelib [ uuid(B05432D9-5A53-4BFF-B1DD-68D5FE414F16), version(1.0), custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, CSharpTest.InterfaceTestClass) ] coclass InterfaceTestClass { interface _Object; interface _MyClass; [default, source] dispinterface __MyClass; }; }; So, IMO, this is either a bug or I'm doing something wrong that I do not yet understand. It seems like a lot of extra work to require developers in C# to explicitly define event interfaces when they're already defined in another assembly/dll. My question now comes down to: Is explicitly defining the event interface the only way to make this work in C# (for interfaces with a lot of events, this can be quite cumbersome and error-prone), or is there some way I can make it work without having to define the event interface in C#? Thanks, Tyler
Hi, So far I am researching the issue and I will give you a response ASAP. Thanks for you reply! Best regards, Peter Huang Microsoft Online Partner Support Get Secure! - www.microsoft.com/security This posting is provided "AS IS" with no warranties, and confers no rights.
Hi Based on my research, we do need to declare the source interface in C# as the sample I provide before so far. Because now the scenario is that we wants to expose the Expose the .net event to com client, so the source interface must be defined in the managed code, that is to say in the managed metadata. If we imported the source interface from typelibrary, we may need to modify the IL code which is very tedious and is tend to cause error. But the VB.NET has a ComClassAttribute Class, which will simplify the procedure. e.g. [VB.NET] Imports Microsoft.VisualBasic <ComClass(TestClass.ClassId, TestClass.InterfaceId, TestClass.EventsId)> _ Public Class TestClass Public Const ClassId As String = "83B75C2E-C54B-4cb3-9B53-B0AC24555590" Public Const InterfaceId As String = "581B36DE-7D3D-4a97-ACAB-AFD556F628CB" Public Const EventsId As String = "554A893A-F819-4be9-A2B1-2A05CA0A3D5F" Public Event Ring() Public Sub FireRing() RaiseEvent Ring() End Sub End Class [VB6] Dim WithEvents o As VBNETTest.TestClass Private Sub Command1_Click() Set o = New VBNETTest.TestClass o.FireRing End Sub Private Sub o_Ring() MsgBox "hello" End Sub The vb.net compiler will generate the similar metadata code with the C#. While in C#, we have to define a source interface first and then use the ComSourceInterfacesAttribute. Best regards, Peter Huang Microsoft Online Partner Support Get Secure! - www.microsoft.com/security This posting is provided "AS IS" with no warranties, and confers no rights.
Thank you for the information Peter. I have defined the interface, and everything works well. Do you know if there is any planned enhancement to the next version of Visual Studio to make this C# event sink interface definition work easier/automated/generated? Thanks, Tyler
Hi Tyler, So far I am not sure if the VB.NET's feature will be adopted by C#, but I think you may try to submit the wish to the MSWish website. Microsoft offers several ways for you to send comments or suggestions about Microsoft products. If you have suggestions for product enhancements that you would like to see in future versions of Microsoft products, please contact us using one of the methods listed later in this article. Let us know how we can improve our products. Product Enhancement suggestions can include: " Improvements on existing products. " Suggestions for additional features. " Ways to make products easier to use. World Wide Web - To send a comment or suggestion via the Web, use one of the following methods: " In Internet Explorer 6, click Send Feedback on the Help menu and then click the link in the Product Suggestion section of the page that appears. " In Windows XP, click Help and Support on the Start menu. Click Send your feedback to Microsoft, and then fill out the Product Suggestion page that appears. " Visit the following Microsoft Web site: http://www.microsoft.com/ms.htm " Click Microsoft.com Guide in the upper-right corner of the page and then click Contact Us . Click the link in the Product Suggestion section of the page that appears. " Visit the following Microsoft Product Feedback Web site: " http://register.microsoft.com/mswish/suggestion.asp" and then complete and submit the form. E-mail - To send comments or suggestions via e-mail, use the following Microsoft Wish Program e-mail address, mswish@microsoft.com. FAX - To send comments or suggestions via FAX, use the following Microsoft FAX number, (425) 936-7329. Each product suggestion is read by a member of our product feedback team, classified for easy access, and routed to the product or service team to drive Microsoft product and/or service improvements. Because we receive an abundance of suggestions (over 69,000 suggestions a year!) we can't guarantee that each request makes it into a final product or service. But we can tell you that each suggestion has been received and is being reviewed by the team that is most capable of addressing it. All product or service suggestions received become the sole property of Microsoft. Should a suggestion be implemented, Microsoft is under no obligation to provide compensation. Best regards, Peter Huang Microsoft Online Partner Support Get Secure! - www.microsoft.com/security This posting is provided "AS IS" with no warranties, and confers no rights.
Don't see what you're looking for? Try a search.
|