Results 1 to 39 of 39

Thread: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2020
    Posts
    209

    Resolved [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.

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Aug 2020
    Posts
    209

    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

  3. #3
    Addicted Member
    Join Date
    Jan 2015
    Posts
    244

    Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6


  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2020
    Posts
    209

    Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6

    Quote Originally Posted by loquat View Post
    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.

  5. #5
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    6,027

    Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6

    Quote Originally Posted by SearchingDataOnly View Post
    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.
    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


    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

  6. #6
    Member
    Join Date
    May 2012
    Location
    42.787034,-81.176367
    Posts
    41

    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

  7. #7
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    6,027

    Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6

    Quote Originally Posted by Joe Caverly View Post
    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.
    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


    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

  8. #8
    Member
    Join Date
    May 2012
    Location
    42.787034,-81.176367
    Posts
    41

    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

  9. #9
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    6,027

    Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6

    Quote Originally Posted by Joe Caverly View Post
    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.
    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


    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

  10. #10
    PowerPoster
    Join Date
    Feb 2017
    Posts
    3,058

    Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6

    Quote Originally Posted by Niya View Post
    Oh....Append_3
    Nice name for a method

  11. #11
    Lively Member gilman's Avatar
    Join Date
    Jan 2017
    Location
    Bilbao
    Posts
    108

    Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6

    Quote Originally Posted by Eduardo- View Post
    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

  12. #12
    Junior Member
    Join Date
    Nov 2020
    Posts
    30

    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

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Aug 2020
    Posts
    209

    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.

  14. #14
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    6,027

    Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6

    Quote Originally Posted by SearchingDataOnly View Post
    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.
    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


    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

  15. #15
    Lively Member gilman's Avatar
    Join Date
    Jan 2017
    Location
    Bilbao
    Posts
    108

    Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6

    Quote Originally Posted by Niya View Post
    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.

  16. #16
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    6,027

    Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6

    Quote Originally Posted by gilman View Post
    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.
    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


    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

  17. #17
    Addicted Member
    Join Date
    Jul 2020
    Posts
    164

    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?

  18. #18
    Member
    Join Date
    May 2012
    Location
    42.787034,-81.176367
    Posts
    41

    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

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Aug 2020
    Posts
    209

    Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6

    Quote Originally Posted by Joe Caverly View Post
    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.

  20. #20
    Addicted Member
    Join Date
    Jul 2020
    Posts
    164

    Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6

    Quote Originally Posted by Joe Caverly View Post
    A search of the classes for the ComVisible attribute would indicate if the class could be called from VB6.
    Very valuable information. Many thanks.

  21. #21
    Lively Member gilman's Avatar
    Join Date
    Jan 2017
    Location
    Bilbao
    Posts
    108

    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.

  22. #22
    Addicted Member
    Join Date
    Jul 2020
    Posts
    164

    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.

  23. #23
    Lively Member gilman's Avatar
    Join Date
    Jan 2017
    Location
    Bilbao
    Posts
    108

    Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6

    Quote Originally Posted by Argus19 View Post
    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

  24. #24
    Addicted Member
    Join Date
    Jul 2020
    Posts
    164

    Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6

    Quote Originally Posted by gilman View Post
    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?

  25. #25
    Lively Member gilman's Avatar
    Join Date
    Jan 2017
    Location
    Bilbao
    Posts
    108

    Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6

    Quote Originally Posted by Argus19 View Post
    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

  26. #26
    Addicted Member
    Join Date
    Jul 2020
    Posts
    164

    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.

  27. #27
    Lively Member gilman's Avatar
    Join Date
    Jan 2017
    Location
    Bilbao
    Posts
    108

    Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6

    Quote Originally Posted by Argus19 View Post
    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

  28. #28
    Addicted Member
    Join Date
    Jul 2020
    Posts
    164

    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.

  29. #29
    Lively Member gilman's Avatar
    Join Date
    Jan 2017
    Location
    Bilbao
    Posts
    108

    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.

  30. #30
    Addicted Member
    Join Date
    Jul 2020
    Posts
    164

    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

  31. #31
    Lively Member gilman's Avatar
    Join Date
    Jan 2017
    Location
    Bilbao
    Posts
    108

    Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6

    The reference:
    Name:  reference.png
Views: 83
Size:  14.6 KB
    the object browser:
    Name:  ObjectExplorer.png
Views: 84
Size:  32.5 KB
    and the IntelliSense:
    Name:  IntelliSense.png
Views: 80
Size:  6.7 KB
    As yuo can see you can't create an instance of the FileInfo class

  32. #32
    Addicted Member
    Join Date
    Jul 2020
    Posts
    164

    Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6

    It turns out like this:
    Name:  Cl.jpg
Views: 82
Size:  43.1 KB

  33. #33
    Lively Member gilman's Avatar
    Join Date
    Jan 2017
    Location
    Bilbao
    Posts
    108

    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.

  34. #34
    Addicted Member
    Join Date
    Jul 2020
    Posts
    164

    Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6

    Quote Originally Posted by gilman View Post
    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.

  35. #35
    Hyperactive Member
    Join Date
    Jun 2019
    Posts
    280

    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:
    1. 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:
    1. Dim fso As New FileSystemObject
    2. 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:
    1. Public Function FileExists(ByVal fileName As String) As Boolean
    2.     Dim fso As New FileSystemObject
    3.     FileExists = fso.FileExists("C:\Temp\test.txt")
    4.     Set fso = Nothing
    5. 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.

  36. #36
    Addicted Member
    Join Date
    Jul 2020
    Posts
    164

    Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6

    Quote Originally Posted by peterst View Post
    .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.

  37. #37
    Hyperactive Member
    Join Date
    Jun 2019
    Posts
    280

    Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6

    Quote Originally Posted by Argus19 View Post
    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).

  38. #38
    Addicted Member
    Join Date
    Jul 2020
    Posts
    164

    Re: [RESOLVED] Directly call the classes in the .NET base lib (mscorlib.dll) in VB6

    Many thanks.

  39. #39
    Hyperactive Member
    Join Date
    Jun 2019
    Posts
    280

    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
  •  



Click Here to Expand Forum to Full Width