-
Feb 26th, 2021, 10:55 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
I often need to test some C#/VB.NET code in VB6. I'd like to know whether there is a way to directly call some classes in the .NET basic library (for example: mscorlib.dll or System.Runtime.dll) in VB6, such as Array, String, Encoding, Marshal, etc.
In addition, can we use the open source and cross-platform .NET-Core framework in VB6? Thanks.
Last edited by SearchingDataOnly; Feb 26th, 2021 at 12:16 PM.
-
Feb 26th, 2021, 10:15 PM
#2
Thread Starter
Frenzied Member
Re: Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
The link below partially solved my problem (Encoding):
https://www.vbforums.com/showthread....=1#post5512336
-
Feb 26th, 2021, 10:39 PM
#3
Hyperactive Member
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
-
Feb 27th, 2021, 12:07 AM
#4
Thread Starter
Frenzied Member
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
Originally Posted by loquat
Thank you, but that's not what I want.
I hope to reduce the configuration steps and call the .NET basic library with only one line (or 2-3 lines) of code, for example:
Code:
Set oBuilder = CreateObject("System.Text.StringBuilder")
'or
Set oArray = CreateObject("System.Collections.ArrayList")
In addition, I'd like to know what tools are available to easily view the exported functions of a DLL.
Last edited by SearchingDataOnly; Feb 27th, 2021 at 12:21 AM.
-
Mar 1st, 2021, 03:31 AM
#5
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
Originally Posted by SearchingDataOnly
Thank you, but that's not what I want.
I hope to reduce the configuration steps and call the .NET basic library with only one line (or 2-3 lines) of code, for example:
Code:
Set oBuilder = CreateObject("System.Text.StringBuilder")
'or
Set oArray = CreateObject("System.Collections.ArrayList")
There is no straight forward way to do this that I'm aware of. .Net and COM are fundamentally incompatible. This is why COM Interop was created. It might be possible to achieve something like this by using COM Interop but instead of exporting the class directly, you export a class with methods that work like CreateObject on the .Net side and have those methods return the objects you when used on the VB6 side. I have no idea if that would actually work and if it does it probably won't work with classes that are not marked as ComVisible and of course, such objects can only be interacted with through late binding, that is assuming that this little work around actually works.
EDIT:
I just tested Joe's method in the following post and it seems you can instantiate .Net objects directly using CreateObject however there are problems. See next post.
Last edited by Niya; Mar 1st, 2021 at 02:23 PM.
-
Mar 1st, 2021, 09:43 AM
#6
Addicted Member
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
I've used .NET with VB6 to get Hebrew dates.
Drop this code into a form1;
Code:
Public Hebrew
Private Sub Form_Load()
Set Hebrew = CreateObject("System.Globalization.HebrewCalendar")
Debug.Print TheMonth() + " " + TheDay() + ", " + TheYear()
Set Hebrew = Nothing
Unload Me
End Sub
Function TheDay()
TheDay = CStr(Hebrew.GetDayOfMonth(Date))
End Function
Function TheMonth()
Select Case Hebrew.GetMonth(Date)
Case 1
TheMonth = "Tishrei"
Case 2
TheMonth = "Cheshvan"
Case 3
TheMonth = "Kislev"
Case 4
TheMonth = "Tevet"
Case 5
TheMonth = "Shevat"
'
' Take into account Leap Years
'
Case 6
If LeapYear() Then
TheMonth = "Adar Alef"
Else
TheMonth = "Adar"
End If
Case 7
If LeapYear() Then
TheMonth = "Adar Beit"
Else
TheMonth = "Nissan"
End If
Case 8
If LeapYear() Then
TheMonth = "Nissan"
Else
TheMonth = "Iyar"
End If
Case 9
If LeapYear() Then
TheMonth = "Iyar"
Else
TheMonth = "Sivan"
End If
Case 10
If LeapYear() Then
TheMonth = "Sivan"
Else
TheMonth = "Tamuz"
End If
Case 11
If LeapYear() Then
TheMonth = "Tamuz"
Else
TheMonth = "Av"
End If
Case 12
If LeapYear() Then
TheMonth = "Av"
Else
TheMonth = "Elul"
End If
Case 13
TheMonth = "Elul"
'
' This should never be executed
'
Case Else
TheMonth = CStr(Hebrew.GetMonth(Date))
End Select
End Function
Function TheYear()
TheYear = CStr(Hebrew.GetYear(Date))
End Function
Function LeapYear()
If Hebrew.IsLeapYear(Hebrew.GetYear(Date)) Then
LeapYear = vbTrue
Else
LeapYear = vbFalse
End If
End Function
Joe
-
Mar 1st, 2021, 02:31 PM
#7
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
Originally Posted by Joe Caverly
I've used .NET with VB6 to get Hebrew dates.
Drop this code into a form1;
Code:
Public Hebrew
Private Sub Form_Load()
Set Hebrew = CreateObject("System.Globalization.HebrewCalendar")
Debug.Print TheMonth() + " " + TheDay() + ", " + TheYear()
Set Hebrew = Nothing
Unload Me
End Sub
Function TheDay()
TheDay = CStr(Hebrew.GetDayOfMonth(Date))
End Function
Function TheMonth()
Select Case Hebrew.GetMonth(Date)
Case 1
TheMonth = "Tishrei"
Case 2
TheMonth = "Cheshvan"
Case 3
TheMonth = "Kislev"
Case 4
TheMonth = "Tevet"
Case 5
TheMonth = "Shevat"
'
' Take into account Leap Years
'
Case 6
If LeapYear() Then
TheMonth = "Adar Alef"
Else
TheMonth = "Adar"
End If
Case 7
If LeapYear() Then
TheMonth = "Adar Beit"
Else
TheMonth = "Nissan"
End If
Case 8
If LeapYear() Then
TheMonth = "Nissan"
Else
TheMonth = "Iyar"
End If
Case 9
If LeapYear() Then
TheMonth = "Iyar"
Else
TheMonth = "Sivan"
End If
Case 10
If LeapYear() Then
TheMonth = "Sivan"
Else
TheMonth = "Tamuz"
End If
Case 11
If LeapYear() Then
TheMonth = "Tamuz"
Else
TheMonth = "Av"
End If
Case 12
If LeapYear() Then
TheMonth = "Av"
Else
TheMonth = "Elul"
End If
Case 13
TheMonth = "Elul"
'
' This should never be executed
'
Case Else
TheMonth = CStr(Hebrew.GetMonth(Date))
End Select
End Function
Function TheYear()
TheYear = CStr(Hebrew.GetYear(Date))
End Function
Function LeapYear()
If Hebrew.IsLeapYear(Hebrew.GetYear(Date)) Then
LeapYear = vbTrue
Else
LeapYear = vbFalse
End If
End Function
Joe
Very interesting. I hadn't thought about trying this. I assumed OP did and it didn't work.
However, this doesn't seem to work for all classes. For example it doesn't work for the StringBuilder but it does work for the ArrayList class:-
Code:
Set b = CreateObject("System.Collections.ArrayList")
b.Add "Hello"
b.Add "World"
For i = 0 To b.Count - 1
Debug.Print b.Item(i)
Next
My guess is not all classes in the .Net Framework are tagged with attributes for the compiler to have created a COM IDispatch interface when Microsoft compiled them. Nevertheless, I think this is exactly what the OP wanted.
-
Mar 1st, 2021, 06:29 PM
#8
Addicted Member
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
Here's an example for using StringBuilder;
Code:
Private Sub Form_Load()
Set s = CreateObject("System.Text.StringBuilder")
s.Append_3 "The quick brown fox jumped over the lazy dog. "
s.Append_3 "Mary had a little lamb."
Debug.Print s.Length
Debug.Print s.Capacity
Debug.Print s.ToString
Set s = Nothing
Unload Me
End Sub
Joe
-
Mar 2nd, 2021, 06:26 PM
#9
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
Originally Posted by Joe Caverly
Here's an example for using StringBuilder;
Code:
Private Sub Form_Load()
Set s = CreateObject("System.Text.StringBuilder")
s.Append_3 "The quick brown fox jumped over the lazy dog. "
s.Append_3 "Mary had a little lamb."
Debug.Print s.Length
Debug.Print s.Capacity
Debug.Print s.ToString
Set s = Nothing
Unload Me
End Sub
Joe
Oh....Append_3, that's why it didn't work for me. I was trying to use Append. Good to know.
-
Mar 2nd, 2021, 09:05 PM
#10
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
Originally Posted by Niya
Oh....Append_3
Nice name for a method
-
Mar 3rd, 2021, 02:07 AM
#11
Addicted Member
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
Originally Posted by Eduardo-
Nice name for a method
I think it is because the method Append of the class StringBuilder has several overloads, it is not possible in COM, so the system adds a numeral to identify what is the real method you are calling
-
Mar 3rd, 2021, 02:22 AM
#12
Lively Member
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
Add Replace and Insert :
Code:
Dim sb As Object
Dim value As Long
Set sb = CreateObject("System.Text.StringBuilder")
sb.Append_3 "The quick brown fox jumped over the lazy dog. "
sb.Append_3 "Mary had a little lamb."
Debug.Print sb.Length, sb.Capacity
Debug.Print sb.ToString
sb.Replace "quick", "slow"
Debug.Print sb.ToString
value = 4
sb.Insert_2 value, "very very "
Debug.Print sb.ToString
Set sb = Nothing
-
Mar 4th, 2021, 05:24 PM
#13
Thread Starter
Frenzied Member
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
Thank you, Joe Caverly, Niya, Eduardo, gilman and LeoFar.
Yes, it's because there are always strange function names like ComputeHash_2, GetBytes_4, Append_3, Insert_2, so I hope someone could recommend some good tools to view the exported functions of these DLLs.
In addition, RC6.Properties does not seem to be able to reflect the properties of late-bound objects.
-
Mar 5th, 2021, 06:23 AM
#14
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
Originally Posted by SearchingDataOnly
I hope someone could recommend some good tools to view the exported functions of these DLLs.
Back when I just started with .Net I used to use a program called .Net Reflector that could decompile .Net DLLs and show you all it's internals including public classes. At the time it was the most recommended decompiler, however, I don't know if this is still the case. I haven't used a de-compiler in years since a lot of .Net libraries I use are open sourced and can be easily found on GitHub and of course the source code for the Framework itself is online.
-
Mar 5th, 2021, 07:00 AM
#15
Addicted Member
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
Originally Posted by Niya
Back when I just started with .Net I used to use a program called .Net Reflector that could decompile .Net DLLs and show you all it's internals including public classes. At the time it was the most recommended decompiler, however, I don't know if this is still the case. I haven't used a de-compiler in years since a lot of .Net libraries I use are open sourced and can be easily found on GitHub and of course the source code for the Framework itself is online.
That is not the problem, the problem is that if a method is overloaded how to call the correct overload of the method, for example the method Append of the class StringBuilder has several overloads, and to call Append(s as string) you have call Append_3, that's the problem, how to know you have to call Append_3 and not Append_2 or Append_1.
-
Mar 5th, 2021, 09:03 PM
#16
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
Originally Posted by gilman
That is not the problem, the problem is that if a method is overloaded how to call the correct overload of the method, for example the method Append of the class StringBuilder has several overloads, and to call Append(s as string) you have call Append_3, that's the problem, how to know you have to call Append_3 and not Append_2 or Append_1.
Oh I wasn't addressing the problem of overloads in particular.
-
Mar 6th, 2021, 01:01 PM
#17
Hyperactive Member
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
Is there a list of .Net classes that can be used in the described way?
Or can it only be determined by trial and error?
-
Mar 6th, 2021, 09:23 PM
#18
Addicted Member
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
Here's the link to the source code for System.Text.Stringbuilder;
stringbuilder.cs
A search of the classes for the ComVisible attribute would indicate if the class could be called from VB6.
I was hoping that I could do this doing a search of the Reference Source, but that result was not successful.
You could also download the source code of .NET Framework from the Reference Source page, and do a local search once downloaded.
Joe
-
Mar 6th, 2021, 09:39 PM
#19
Thread Starter
Frenzied Member
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
Originally Posted by Joe Caverly
Here's the link to the source code for System.Text.Stringbuilder;
stringbuilder.cs
A search of the classes for the ComVisible attribute would indicate if the class could be called from VB6.
I was hoping that I could do this doing a search of the Reference Source, but that result was not successful.
You could also download the source code of .NET Framework from the Reference Source page, and do a local search once downloaded.
Joe
Very useful info, thank you very much, Joe.
-
Mar 7th, 2021, 03:37 AM
#20
Hyperactive Member
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
Originally Posted by Joe Caverly
A search of the classes for the ComVisible attribute would indicate if the class could be called from VB6.
Very valuable information. Many thanks.
-
Mar 7th, 2021, 04:15 AM
#21
Addicted Member
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
An easier way to know which classes are exported is to add a reference to mscorlib.tlb to a vb project, but it only shows the classes, not the methods or properties.
-
Mar 7th, 2021, 04:54 AM
#22
Hyperactive Member
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
By analogy with the example of post # 12, I tried to use the FileInfo class:
https://referencesource.microsoft.co...d41,references
It contains everything you need:
Code:
[ComVisible(true)]
public sealed class FileInfo: FileSystemInfo
I did this:
Code:
Dim sb As Object
Private Sub Form_Load()
Set sb = CreateObject("System.IO.FileInfo")
End Sub
And I got error # 429.
-
Mar 7th, 2021, 05:03 AM
#23
Addicted Member
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
Originally Posted by Argus19
By analogy with the example of post # 12, I tried to use the FileInfo class:
https://referencesource.microsoft.co...d41,references
It contains everything you need:
Code:
[ComVisible(true)]
public sealed class FileInfo: FileSystemInfo
I did this:
Code:
Dim sb As Object
Private Sub Form_Load()
Set sb = CreateObject("System.IO.FileInfo")
End Sub
And I got error # 429.
The problem is that the only constructor of the FileInfo class requires a parameter, so it cannot be created through COM since COM only allows to create classes whose constructor does not need parameters
-
Mar 7th, 2021, 05:17 AM
#24
Hyperactive Member
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
Originally Posted by gilman
The problem is that the only constructor of the FileInfo class requires a parameter, so it cannot be created through COM since COM only allows to create classes whose constructor does not need parameters
What does it mean that having [ComVisible (true)] is not enough?
-
Mar 7th, 2021, 05:22 AM
#25
Addicted Member
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
Originally Posted by Argus19
What does it mean that having [ComVisible (true)] is not enough?
Yes, you can use the class, but you can't create instances of this class, for creating an instance of a class the class must have a constructor without parameters.
The easyest way to see if you can create a class, is adding the reference to the mscorlib.tlb and try to create it in VB,
Dim x as new mscorlib.FileInfo
don't compile
-
Mar 7th, 2021, 05:31 AM
#26
Hyperactive Member
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
Did not work out.
Added a link to mscorlib.dll. It did not help.
Something I do not understand.
-
Mar 7th, 2021, 08:10 AM
#27
Addicted Member
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
Originally Posted by Argus19
Did not work out.
Added a link to mscorlib.dll. It did not help.
Something I do not understand.
You don't have to add a reference to the DLL file, you have to add a reference to the TLB file
-
Mar 7th, 2021, 11:23 AM
#28
Hyperactive Member
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
I wrote the mscorlib.tlb file to the program folder. I add a link but nothing appears.
Last edited by Argus19; Mar 7th, 2021 at 01:45 PM.
-
Mar 8th, 2021, 02:01 AM
#29
Addicted Member
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
Not, in the references of your proyect you have to add a reference to this file.
-
Mar 8th, 2021, 04:02 AM
#30
Hyperactive Member
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
I am doing this:
Project->References->Browse->Folder->mscorlib.tlb->Open->OK
-
Mar 8th, 2021, 04:59 AM
#31
Addicted Member
-
Mar 8th, 2021, 05:14 AM
#32
Hyperactive Member
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
It turns out like this:
-
Mar 8th, 2021, 05:40 AM
#33
Addicted Member
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
The class File is exported by mscorlib, is displayed in the object browser so you can use it, but is not displayed in the intelisense when you try to create an instance with New, yo can't create an instance of the class
If you try:
Dim x as mscorlib.File
It works fine
but
Set x = New mscorlib.File
don't works
The FileInfo is displayed in the object browser, so you can use it, but if you try to create an instance with New, it isn't displayed in the intelisense, so you can't create an instance of the class, using New or using CreateObject
Last edited by gilman; Mar 8th, 2021 at 05:45 AM.
-
Mar 8th, 2021, 07:14 AM
#34
Hyperactive Member
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
Originally Posted by gilman
If you try:
Dim x as mscorlib.File
It works fine
Yes, that's how it works. It remains to figure out how to initialize it. As I understand it, you need to use the "Set" operator.
-
Mar 8th, 2021, 08:57 AM
#35
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
You can check MS docs about .NET File class and see why it is not used as you expect from VB6.
.NET classes may contain static (in C# terminology) or shared (in VB.NET) methods that can be invoked without creating creating class object - the same like Modules work in VB6. Actually this is the way to get "module" functionality in other .NET languages like C# (as VB.NET already has it). So as you do not create object for something you use from module, you can't instantiate System.IO.File class for the same reason.
Anyway, this is not the end of the story. Some .NET classes can be instantiated like normal objects, have constructors (but now with possibility to have parameters in the constructor) and all properties and methods. But they can also contain those shared/static methods you can directly use without instantiating.
For example File.Exists can be directly used:
VB.NET Code:
If File.Exists("C:\Temp\test.txt") Then
In VB6 you can use FileSystemObject.FileExists but you have to create the object first:
VB6 Code:
Dim fso As New FileSystemObject
If fso.FileExists("C:\Temp\test.txt") Then
So in VB6 app you can define module, e.g. FileHelpers.bas with FileExists helper function:
VB6 Code:
Public Function FileExists(ByVal fileName As String) As Boolean
Dim fso As New FileSystemObject
FileExists = fso.FileExists("C:\Temp\test.txt")
Set fso = Nothing
End Function
And use it somewhere like If FileExists("C:\Temp\test.txt") Then.
I hope this will help analyzing which classes and methods from .NET you can use directly and which may need creating proxy .NET COM objects.
-
Mar 8th, 2021, 09:34 AM
#36
Hyperactive Member
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
Originally Posted by peterst
.NET classes may contain static (in C# terminology) or shared (in VB.NET) methods that can be invoked without creating creating class object - the same like Modules work in VB6. Actually this is the way to get "module" functionality in other .NET languages like C# (as VB.NET already has it). So as you do not create object for something you use from module, you can't instantiate System.IO.File class for the same reason.
Anyway, this is not the end of the story. Some .NET classes can be instantiated like normal objects, have constructors (but now with possibility to have parameters in the constructor) and all properties and methods. But they can also contain those shared/static methods you can directly use without instantiating.
Thank. It became clearer.
I wanted to try the FileInfo class out of interest, as I could not find a description of the .Net classes that can be used in VB 6.0.
VB 6.0 has many features. I would like to understand how to increase its capabilities.
-
Mar 8th, 2021, 09:51 AM
#37
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
Originally Posted by Argus19
Thank. It became clearer.
I wanted to try the FileInfo class out of interest, as I could not find a description of the .Net classes that can be used in VB 6.0.
VB 6.0 has many features. I would like to understand how to increase its capabilities.
The easiest way is to install VS 2019 Community and do some small test apps to see how it works there and then, only if required, create .NET COM classes and use them in VB6. You can target .NET Framework 4.0 if you want WinXP compatibility (this may need to play with individual components in Visual Studio Installer).
-
Mar 8th, 2021, 10:06 AM
#38
Hyperactive Member
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
-
Mar 8th, 2021, 10:38 AM
#39
Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6
Another possibility is to quickly test code without multi-gigabyte installation like VS is LINQPad. The free version lacks some features like autocompletion but you can still copy some code from sites like MS docs for .NET, modify and test it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|