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.
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
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
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?
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?
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
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
Re: COM Interop example. How to use .Net classes in VB6.
Originally Posted by ChenLin
I have successfully demonstrated on this machine, but it cannot be used on other computers, even if it has been packaged with vbrap4.
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:-
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.
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
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.
Re: COM Interop example. How to use .Net classes in VB6.
Originally Posted by loquat
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.
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
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.
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
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 ?
Re: COM Interop example. How to use .Net classes in VB6.
Originally Posted by Krammig
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.
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
Re: COM Interop example. How to use .Net classes in VB6.
Originally Posted by Niya
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 ?
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.
Re: COM Interop example. How to use .Net classes in VB6.
Originally Posted by Krammig
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.
Originally Posted by loquat
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.
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