Results 1 to 21 of 21

Thread: COM Interop example. How to use .Net classes in VB6.

  1. #1

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    COM Interop example. How to use .Net classes in VB6.



    More than a few times now, I've seen people asking how to use .Net components in their VB6 applications. As it seems there is some small demand for it, I decided to show an example of how it is done. It is very simple and as such doesn't require that much explanation. The code itself should be easily understandable by anyone with a modicum of VB6/VB.Net experience.

    Important Note

    In order for the VB6 project in the attachment to work correctly, you MUST open the VB.Net project and compile it. The compiler will automatically register it with COM so it will be usable from VB6. The VB.Net project was created using Visual Studio 2019 so I recommend using that version of Visual Studio to compile it.

    You can also look at this post I made a few years ago for another example of using COM Interop to access .Net classes from VB6. That example is even simpler than this one.

    Any questions are welcomed.
    Attached Files Attached Files
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  2. #2
    Member Dragokas's Avatar
    Join Date
    Aug 2015
    Location
    Ukraine
    Posts
    740

    Re: COM Interop example. How to use .Net classes in VB6.

    Nice! Thank you for the example. Works well, just need to open VS as Admin.
    Malware analyst, VirusNet developer, HiJackThis+ author || my CodeBank works

  3. #3

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: COM Interop example. How to use .Net classes in VB6.

    You're welcomed.

    Yea, it needs admin privileges' to access the registry to write all the COM stuff.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  4. #4
    Member Dragokas's Avatar
    Join Date
    Aug 2015
    Location
    Ukraine
    Posts
    740

    Re: COM Interop example. How to use .Net classes in VB6.

    Actually, this is not what's topic saying, not "using .Net classes".
    It's just an IPC to your own interfaces of .Net COM dll with pre-defined functionality.

    It is possible to similarly access directly (to an arbitrary methods) of CLR?
    Malware analyst, VirusNet developer, HiJackThis+ author || my CodeBank works

  5. #5

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: COM Interop example. How to use .Net classes in VB6.

    Actually, it's nothing like IPC. That method of using COM is actually termed an In-Process Server. What it means is that the COM object reside in the address space of the client, in this case, the VB6 application. IPC involves the use of global OS resources like pipes and TCP/IP connections for applications to communicate across process boundaries. COM In-Process Servers actually work exactly like a typical C style DLL.

    It is possible to similarly access directly (to an arbitrary methods) of CLR?
    Anything is possible if you know how. Question is, is it worth the trouble?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  6. #6
    Hyperactive Member
    Join Date
    Jun 2016
    Location
    España
    Posts
    506

    Re: COM Interop example. How to use .Net classes in VB6.

    can be used without registration?
    for example modTrickUnregCOM or regfree vbRichClient

    a greeting

  7. #7

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: COM Interop example. How to use .Net classes in VB6.

    Quote Originally Posted by yokesee View Post
    can be used without registration?
    for example modTrickUnregCOM or regfree vbRichClient

    a greeting
    Yes it's definitely possible.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  8. #8
    Addicted Member
    Join Date
    May 2016
    Location
    China
    Posts
    197

    Re: COM Interop example. How to use .Net classes in VB6.

    I have successfully demonstrated on this machine, but it cannot be used on other computers, even if it has been packaged with vbrap4.


    Name:  err.png
Views: 2540
Size:  10.8 KB


    This is the error that appears after clicking the display data button.
    Last edited by ChenLin; Jan 20th, 2021 at 02:35 AM.
    QQ: 289778005

  9. #9

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: COM Interop example. How to use .Net classes in VB6.

    Quote Originally Posted by ChenLin View Post
    I have successfully demonstrated on this machine, but it cannot be used on other computers, even if it has been packaged with vbrap4.


    Name:  err.png
Views: 2540
Size:  10.8 KB


    This is the error that appears after clicking the display data button.
    When the .Net project is compiled in Visual Studio, it is automatically registered with COM. I'm guessing you were trying to deploy the binaries on another PC. In this case you would need to be responsible for registering the dll with COM yourself. Try this:-
    Code:
    C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm GridDisplay.dll /tlb:GridDisplay.tlb /codebase
    Regasm is the commandline tool that actually does the work of registering the .Net DLL with COM. The above can be executed from the commandline with the current directory set to the folder that contains the DLL or you could place it in a .Bat file alongside the DLL.

    There's also the option of registration free COM which involves the use of manifests. I myself haven't tried this approach but it seems to be a popular request when dealing with COM. I'll explore that and try to update this with information on how to apply it here when I have the time. It should make deployment of such DLLs much easier. But if you wish to explore it yourself you can read up on it here.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  10. #10
    Hyperactive Member
    Join Date
    Jun 2016
    Location
    España
    Posts
    506

    Re: COM Interop example. How to use .Net classes in VB6.

    There's also the option of registration free COM which involves the use of manifests. I myself haven't tried this approach but it seems to be a popular request when dealing with COM. I'll explore that and try to update this with information on how to apply it here when I have the time. It should make deployment of such DLLs much easier. But if you wish to explore it yourself you can read up on it here.
    It would be great, it is very complicated.
    any way to do the simplest is appreciated.

    a greeting

  11. #11
    Addicted Member
    Join Date
    May 2016
    Location
    China
    Posts
    197

    Re: COM Interop example. How to use .Net classes in VB6.

    The magic @Niya, use
    Code:
    regasm GridDisplay.dll /tlb:GridDisplay.tlb /codebase
    The problem is solved!
    QQ: 289778005

  12. #12
    Hyperactive Member
    Join Date
    Jan 2015
    Posts
    323

    Re: COM Interop example. How to use .Net classes in VB6.


  13. #13

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: COM Interop example. How to use .Net classes in VB6.

    Quote Originally Posted by loquat View Post
    can this method used to use all .net dlls with all dll functions?
    For the most part I'd say yes. However, when you export a .Net class as a COM class you need to be aware of how that class will be marshalled. In most cases it should be straight forward and easy to understand but certain types of functions can be tricky. For example, higher order functions(functions where at least 1 parameter or it's return value is another function) might throw you off a bit. Delegate types in .Net are the mechanism on which higher order functions are built. You need to be aware of how delegates will be marshalled if you're exporting a method that uses them. You can read up on marshalling here.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  14. #14
    PowerPoster
    Join Date
    Jan 2020
    Posts
    3,746

    Re: COM Interop example. How to use .Net classes in VB6.

    you can use vb.net make a com dll,with controls,like a ocx.
    dim a as vbnet.ocx1
    ocx1.puttoHwnd(picture1.hwnd)

  15. #15
    Hyperactive Member
    Join Date
    Jan 2015
    Posts
    323

    Re: COM Interop example. How to use .Net classes in VB6.

    and found this vbDotnetLoader.cls for vba and vb6
    https://gist.github.com/bclothier/19...884c10f4cc8475

  16. #16

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: COM Interop example. How to use .Net classes in VB6.

    Quote Originally Posted by loquat View Post
    and found this vbDotnetLoader.cls for vba and vb6
    https://gist.github.com/bclothier/19...884c10f4cc8475
    I'm not familiar with that approach. But that is written by the one of the developers of TwinBASIC so I'd expect it to be a very high quality piece of code.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  17. #17
    Member
    Join Date
    Apr 2011
    Posts
    47

    Re: COM Interop example. How to use .Net classes in VB6.

    Thank you for the this, I have been trying to do just this for the past several days.

    However a question please.

    I understand fundamentally that you cannot mix 32bit and 64bit DLL's in an app.

    My situation is that I have some VB6 code for both an app and associated DLL (DLL_1 lest say). In addition I have another DLL (DLL_2) that talks to a .SYS driver. I have the source for all of these.

    The driver has been upgraded to x64 and likewise DLL_2 that talks to it has been compiled as x64

    DLL_1 is exposed and in it's x86 version talks happily to the VB6 DLL which talks to the main VB6 EXE

    Of course now that DLL_1 needs to be compiled as x64 to talk to DLL_2 etc. it will not work with the VB6 DLL

    I was hoping this solution of yours would address this but as I noted this solution, the GridDisplayCOMInterop is set to compile as x86 this it works fine with the VB6 component.

    Is there a solution whereby there is a COM wrapper, or Interop file, that enables a VB6 (32bit) app to talk to an x64 DLL ?

    Thanks

  18. #18

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: COM Interop example. How to use .Net classes in VB6.

    Quote Originally Posted by Krammig View Post
    Is there a solution whereby there is a COM wrapper, or Interop file, that enables a VB6 (32bit) app to talk to an x64 DLL ?
    Unfortunately no. The fundamental reason that a 32 bit application cannot use a 64 bit DLL is because Windows does not allow a single process to execute both 32 bit and 64 bit code. It must be one or the other, never both. Standard DLLs work by loading loading it's code into the process space of the EXE that wants to use it. If it's a 32 bit process then the DLL must be a 32 bit DLL. COM DLLs come in two flavors, in-process and out-of-process. What I showed here in this thread is how to use an in-process COM DLL in a VB6 application. You can tell by the name "in-process" that this means the COM DLL will be loaded into process space of the application and since VB6 applications are 32 bit then the DLL must also be 32 bit. There is no way around this.

    However, as far as I know VB6 applications can also use out-of-process COM Servers which is known in the VB6 world as an ActiveX EXE. Because this type of COM server runs in it's own process space, separate from the VB6 application's process space, it can be a 64 bit COM server. However, in all my years I've always dealt strictly with in-process COM servers. I do not know how or even if .Net can create out-of-process COM servers. It should be possible but I've never explored it so I hesitate to speak on that.

    All is not lost though. You can essentially rig this yourself. What you could do is link your 64 bit DLL to a 64 bit .Net EXE application and then use some kind of RPC mechanism like pipes or even plain old Winsock to have your VB6 application communicate with this .Net 64 bit EXE which itself would communicate with the 64 bit DLL. In other words, the .Net 64 bit EXE acts as an intermediary between the 32 bit VB6 EXE and the 64 bit DLL.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  19. #19
    Member
    Join Date
    Apr 2011
    Posts
    47

    Re: COM Interop example. How to use .Net classes in VB6.

    Quote Originally Posted by Niya View Post

    All is not lost though. You can essentially rig this yourself. What you could do is link your 64 bit DLL to a 64 bit .Net EXE application and then use some kind of RPC mechanism like pipes or even plain old Winsock to have your VB6 application communicate with this .Net 64 bit EXE which itself would communicate with the 64 bit DLL. In other words, the .Net 64 bit EXE acts as an intermediary between the 32 bit VB6 EXE and the 64 bit DLL.

    Thanks for the swift reply Niya,

    Actually the DLL_1 on the VB6 side is a 32 bit ActiveX DLL, thus why I was wondering if there just might be a glimmer of hope given that is an out-of-process item. The .Net DLL 64 bit is also supposed to be a COM project that uses ATL which if my understanding is correct on reading up on it, is supposed to facilitate it acting like an ActiveX. But this is only my interpretation and may not be the case

    Given DLL_1 is an ActiveX does that alter your thoughts at all ?

    Cheers

  20. #20
    Hyperactive Member
    Join Date
    Jan 2015
    Posts
    323

    Re: COM Interop example. How to use .Net classes in VB6.

    have found two more pure vb6 method to do this.
    1.the one is vb2clr, on github: https://github.com/jet2jet/vb2clr
    2.the other use CLRCreateInstance in mscoree.dll to create object directly.

  21. #21

    Thread Starter
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: COM Interop example. How to use .Net classes in VB6.

    Quote Originally Posted by Krammig View Post
    Actually the DLL_1 on the VB6 side is a 32 bit ActiveX DLL, thus why I was wondering if there just might be a glimmer of hope given that is an out-of-process item.
    An out-of-process ActiveX binary is called an ActiveX EXE in VB6, not DLL. If it's an ActiveX DLL it is most certainly and in-process server.

    Quote Originally Posted by loquat View Post
    have found two more pure vb6 method to do this.
    1.the one is vb2clr, on github: https://github.com/jet2jet/vb2clr
    2.the other use CLRCreateInstance in mscoree.dll to create object directly.
    That is pretty clever though I can't say if that could be used to use a 64 bit COM server from a 32 bit process.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width