Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Calling a .NET Component from a COM Component

Calling a .NET Component from a COM Component

Scheduled Pinned Locked Moved C#
csharphtmlcomhelp
12 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Shahin77

    Hi guys, I have one user control implemented in .net (c#) and im trying to call it from a form in visual basic. the big problem is i cant add that control to vb designer, i just can add it as reference, so when the vb form comes up i cant see my control. i've done all the steps in this link (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/callnetfrcom.asp). does anybody any hint, thanx in advance.:confused:

    H Offline
    H Offline
    Heath Stewart
    wrote on last edited by
    #2

    Make sure you have registered it correctly with regasm.exe /codebase, or set the project option in VS.NET to do so automatically. Also make sure that you DO NOT use AutoDispatch or AutoDual for your ClassInterfaceAttribute - ALWAYS declare your own class interface (implement as first interface) despite what Microsoft says in their docs. In fact, if you read more information about CCWs in other Microsoft documentation, they state as much, warning about using auto-generated interfaces. Always use a GuidAttribute as well, otherwise your VB project won't reference the right typelib next time you build your C# project. Finally, I have noticed problems before when trying to add controls to VB6 with other work and articles I've done. First trying loading your control in the ActiveX Control Test Container (tstcon32.exe), which should be available from your VS.NET Tools menu if you installed the Platform SDK tools (installed by default). If it doesn't work in there, then you've done something wrong. Re-read the articles you linked or read Nick's at http://www.codeproject.com/dotnet/nettocom.asp[^]. Reply here if you're having problems. If it does work, make sure you customize VB6's toolbox and add the .NET Control as an ActiveX control, not just a project reference.

    Microsoft MVP, Visual C# My Articles

    S 1 Reply Last reply
    0
    • S Shahin77

      Hi guys, I have one user control implemented in .net (c#) and im trying to call it from a form in visual basic. the big problem is i cant add that control to vb designer, i just can add it as reference, so when the vb form comes up i cant see my control. i've done all the steps in this link (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/callnetfrcom.asp). does anybody any hint, thanx in advance.:confused:

      K Offline
      K Offline
      KingTermite
      wrote on last edited by
      #3

      I just went through this last week (here at work). Here is the MSDN article that gave me the proper example I needed. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcwlkcominteroppart2cservertutorial.asp You must generate the GUIDs with regasm like Heath mentioned. From VS go to Tools menu and select "Create GUID".


      There are only 10 types of people in this world....those that understand binary, and those that do not.

      H 1 Reply Last reply
      0
      • H Heath Stewart

        Make sure you have registered it correctly with regasm.exe /codebase, or set the project option in VS.NET to do so automatically. Also make sure that you DO NOT use AutoDispatch or AutoDual for your ClassInterfaceAttribute - ALWAYS declare your own class interface (implement as first interface) despite what Microsoft says in their docs. In fact, if you read more information about CCWs in other Microsoft documentation, they state as much, warning about using auto-generated interfaces. Always use a GuidAttribute as well, otherwise your VB project won't reference the right typelib next time you build your C# project. Finally, I have noticed problems before when trying to add controls to VB6 with other work and articles I've done. First trying loading your control in the ActiveX Control Test Container (tstcon32.exe), which should be available from your VS.NET Tools menu if you installed the Platform SDK tools (installed by default). If it doesn't work in there, then you've done something wrong. Re-read the articles you linked or read Nick's at http://www.codeproject.com/dotnet/nettocom.asp[^]. Reply here if you're having problems. If it does work, make sure you customize VB6's toolbox and add the .NET Control as an ActiveX control, not just a project reference.

        Microsoft MVP, Visual C# My Articles

        S Offline
        S Offline
        Shahin77
        wrote on last edited by
        #4

        You wrote: If it does work, make sure you customize VB6's toolbox and add the .NET Control as an ActiveX control, not just a project reference. could u plz tell me how can i do this in vb? (customize VB6's toolbox and add the .NET Control as an ActiveX control) thanx alot

        H 1 Reply Last reply
        0
        • S Shahin77

          You wrote: If it does work, make sure you customize VB6's toolbox and add the .NET Control as an ActiveX control, not just a project reference. could u plz tell me how can i do this in vb? (customize VB6's toolbox and add the .NET Control as an ActiveX control) thanx alot

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #5

          Just right-click on in the toolbox and click "Components...". Find the assembly name (the value you put in the AssemblyTitleAttribute in AssemblyInfo.cs, or the assembly filename if you didn't put one), check it and click okay. If you don't see it, make sure you registered your assembly with regasm.exe /codebase "path/to/file.dll". This will register any COM-visible types, but you should declare your classes, interfaces, methods, properties, and enums appropriately. See Creating a CCW for COM enabled non .NET applications[^] for more information.

          Microsoft MVP, Visual C# My Articles

          S 1 Reply Last reply
          0
          • H Heath Stewart

            Just right-click on in the toolbox and click "Components...". Find the assembly name (the value you put in the AssemblyTitleAttribute in AssemblyInfo.cs, or the assembly filename if you didn't put one), check it and click okay. If you don't see it, make sure you registered your assembly with regasm.exe /codebase "path/to/file.dll". This will register any COM-visible types, but you should declare your classes, interfaces, methods, properties, and enums appropriately. See Creating a CCW for COM enabled non .NET applications[^] for more information.

            Microsoft MVP, Visual C# My Articles

            S Offline
            S Offline
            Shahin77
            wrote on last edited by
            #6

            hi guys, thanx for your great helps, but im still having problem. I dont know why but i can insert my control in activex control test container in .net but i have to click implemented categories button then just select .net category. but in visual basic or even in .net i just can see it as com reference not come component, i cann't add it to tool box!!! i test this link but this one is the same too, it just shows in references not activex section. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cssample/html/vcsamCOMInteropPart2Sample.asp any help really appreciated.:doh:

            H 1 Reply Last reply
            0
            • K KingTermite

              I just went through this last week (here at work). Here is the MSDN article that gave me the proper example I needed. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcwlkcominteroppart2cservertutorial.asp You must generate the GUIDs with regasm like Heath mentioned. From VS go to Tools menu and select "Create GUID".


              There are only 10 types of people in this world....those that understand binary, and those that do not.

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #7

              Actually, regasm.exe only registers the assembly. You need uuidgen.exe or a plethora of alternative programs / extensions. You're right about where to generate a new GUID from VS.NET, though.

              Microsoft MVP, Visual C# My Articles

              1 Reply Last reply
              0
              • S Shahin77

                hi guys, thanx for your great helps, but im still having problem. I dont know why but i can insert my control in activex control test container in .net but i have to click implemented categories button then just select .net category. but in visual basic or even in .net i just can see it as com reference not come component, i cann't add it to tool box!!! i test this link but this one is the same too, it just shows in references not activex section. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cssample/html/vcsamCOMInteropPart2Sample.asp any help really appreciated.:doh:

                H Offline
                H Offline
                Heath Stewart
                wrote on last edited by
                #8

                COM registration - like almost everything else - is in the system registry. Even the categories are nothing more than GUIDs, so you can actually implement a catagory just by adding a GUID to your control's registration. Unfortunately, you can't do this through any attributes. All you could do is add a registration and unregistration method and use the ComRegisterFunctionAttribute and ComUnregisterFunctionAttribute on those methods. If you just need this for your own development, there are two other things you can do. You can open the VBP project in a vanilla text editor like notepad.exe and add a new component (see the existing lines for examples), save it, then reload your VB project, or you can use regedit.exe to add the catagory like I mentioned above (this shouldn't become a common practice, though, since by implementing some catagories you imply that you support certain interfaces):

                1. Click Start->Run
                2. Type "regedit" (without quotes) and click "OK"
                3. Expand HKEY_CLASS_ROOT\CLSID and find the GUID for your component (the class, not the interface, which you should'be attributed with the GuidAttribute).
                4. Expand the GUID
                5. Expand "Implemented Catagories"
                6. Right-click on "Implemented Catagories" and select "New->Key"
                7. Type "{40FC6ED4-2438-11CF-A3DB-080036F12502}" (without quotes) and hit return.
                8. Restart VB and you should now see it.

                Microsoft MVP, Visual C# My Articles

                S 1 Reply Last reply
                0
                • H Heath Stewart

                  COM registration - like almost everything else - is in the system registry. Even the categories are nothing more than GUIDs, so you can actually implement a catagory just by adding a GUID to your control's registration. Unfortunately, you can't do this through any attributes. All you could do is add a registration and unregistration method and use the ComRegisterFunctionAttribute and ComUnregisterFunctionAttribute on those methods. If you just need this for your own development, there are two other things you can do. You can open the VBP project in a vanilla text editor like notepad.exe and add a new component (see the existing lines for examples), save it, then reload your VB project, or you can use regedit.exe to add the catagory like I mentioned above (this shouldn't become a common practice, though, since by implementing some catagories you imply that you support certain interfaces):

                  1. Click Start->Run
                  2. Type "regedit" (without quotes) and click "OK"
                  3. Expand HKEY_CLASS_ROOT\CLSID and find the GUID for your component (the class, not the interface, which you should'be attributed with the GuidAttribute).
                  4. Expand the GUID
                  5. Expand "Implemented Catagories"
                  6. Right-click on "Implemented Catagories" and select "New->Key"
                  7. Type "{40FC6ED4-2438-11CF-A3DB-080036F12502}" (without quotes) and hit return.
                  8. Restart VB and you should now see it.

                  Microsoft MVP, Visual C# My Articles

                  S Offline
                  S Offline
                  Shahin77
                  wrote on last edited by
                  #9

                  guys I really appreciate your knowledge, Im almost done with my component and i can test it in activex container test program and it works great, but i cant add this component to my vb project, i added this {40FC6ED4-2438-11CF-A3DB-080036F12502} for my class component guid in the registry as u told, but i cant still see it in there as com activex control, can u tell me what this {40FC6ED4-2438-11CF-A3DB-080036F12502} exactly does. thanx alot

                  H 1 Reply Last reply
                  0
                  • S Shahin77

                    guys I really appreciate your knowledge, Im almost done with my component and i can test it in activex container test program and it works great, but i cant add this component to my vb project, i added this {40FC6ED4-2438-11CF-A3DB-080036F12502} for my class component guid in the registry as u told, but i cant still see it in there as com activex control, can u tell me what this {40FC6ED4-2438-11CF-A3DB-080036F12502} exactly does. thanx alot

                    H Offline
                    H Offline
                    Heath Stewart
                    wrote on last edited by
                    #10

                    That's the CATID (catagory ID) for the Controls (as in ActiveX controls) catagory. Use oleview.exe to view these different catagories. This tool is also available by default from the VS.NET Tools menu as the "OLE/COM Object Viewer". Since I can't really see your machine to know if everything is setup correctly, you could always try what I mentioned before: open the .vbp project file in notepad.exe (or some other vanilla text editor) and add the line manually, so it would look something like this:

                    Object={YOUR CLASS GUID}#MajorVersion.MinorVersion#0; Filename.dll

                    It should then appear in the toolbox.

                    Microsoft MVP, Visual C# My Articles

                    S 1 Reply Last reply
                    0
                    • H Heath Stewart

                      That's the CATID (catagory ID) for the Controls (as in ActiveX controls) catagory. Use oleview.exe to view these different catagories. This tool is also available by default from the VS.NET Tools menu as the "OLE/COM Object Viewer". Since I can't really see your machine to know if everything is setup correctly, you could always try what I mentioned before: open the .vbp project file in notepad.exe (or some other vanilla text editor) and add the line manually, so it would look something like this:

                      Object={YOUR CLASS GUID}#MajorVersion.MinorVersion#0; Filename.dll

                      It should then appear in the toolbox.

                      Microsoft MVP, Visual C# My Articles

                      S Offline
                      S Offline
                      Shahin77
                      wrote on last edited by
                      #11

                      it wroks and thanx very alot. I was wondering if you say me what can do to make this control visible for all of component client container, sometimes i need to use this control in vb, vc,delphi,... , what i have to do to make it like the other activex control which commin up when you open the component reference form in the vb or anyother language. and another isues is one i've created one tlb file from that component, but now i cant remove it, i closed every thing, do i need to unregister it,,how !!! thanx very much

                      H 1 Reply Last reply
                      0
                      • S Shahin77

                        it wroks and thanx very alot. I was wondering if you say me what can do to make this control visible for all of component client container, sometimes i need to use this control in vb, vc,delphi,... , what i have to do to make it like the other activex control which commin up when you open the component reference form in the vb or anyother language. and another isues is one i've created one tlb file from that component, but now i cant remove it, i closed every thing, do i need to unregister it,,how !!! thanx very much

                        H Offline
                        H Offline
                        Heath Stewart
                        wrote on last edited by
                        #12

                        As I mentioned a while back, you can implement static methods that are attributed with ComRegisterFunctionAttribute and ComUnregisterFunctionAttribute that use the Microsoft.Win32.RegistryKey to do this manually. These functions are called in addition to normal registration of the class. If you can't release the typelib (.tlb file), you need to remove ALL references to objects defined by the typelib in your project and remove the control from the toolbox. If you have questions about how COM objects in the VB6 IDE, please jump over to the VB forum.

                        Microsoft MVP, Visual C# My Articles

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups