Results 1 to 9 of 9

Thread: [RESOLVED] Mapping between C# and VB6 (C# to VB6)

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2020
    Posts
    314

    Resolved [RESOLVED] Mapping between C# and VB6 (C# to VB6)

    I'm currently translating some C# code into VB6 code. I have a few questions:

    (1) What is the VB6 data type corresponding to IntPtr in C#? Is it Long-Type or OLE_HANDLE? Or do I need to write a special class to correspond to IntPtr? Or use TypeDef?

    (2) What is the best way to implement C# delegate in VB6? E.g:
    Code:
    public delegate IntPtr Scintilla_DirectFunction(IntPtr ptr, int iMessage, IntPtr wParam, IntPtr lParam);
    Thanks.
    Last edited by SearchingDataOnly; Feb 13th, 2021 at 11:05 AM.

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

    Re: Mapping between C# and VB6 (C# to VB6)

    (1) Use Long in VB6. IntPtr is a .Net data type that is used to represent pointers. It is a 64 bit value in a 64 bit process and it's 32 bits when running in a 32 bit process.

    (2) A delegate is really just a function pointer. Whenever you use the AddressOf operator in VB6, you're actually doing the same thing people would use delegates for in .Net.

    Here's a crude implementation of delegate semantics in VB6:-
    Code:
    '
    Public Sub MySub(ByVal i As Integer)
    End Sub
    
    Public Function GetFunctionPointer(ByVal func As Long) As Long
        GetFunctionPointer = func
    End Function
    The above code is placed in a module and you can do this:-
    Code:
        Dim delegate As Long
        delegate = GetFunctionPointer(AddressOf MySub)
    The difference in C# is that instead of using a Long, you would use a Delegate which is strongly typed.
    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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2020
    Posts
    314

    Re: Mapping between C# and VB6 (C# to VB6)

    Quote Originally Posted by Niya View Post
    (1) Use Long in VB6. IntPtr is a .Net data type that is used to represent pointers. It is a 64 bit value in a 64 bit process and it's 32 bits when running in a 32 bit process.
    Yes, using Long-Type to map IntPtr is the simplest and most effective method. I originally wanted to imitate the Int64 in vbCorLib to write an IntPtr class, but it seemed too complicated to do so.

    Quote Originally Posted by Niya View Post
    (2) A delegate is really just a function pointer. Whenever you use the AddressOf operator in VB6, you're actually doing the same thing people would use delegates for in .Net.

    ...

    The difference in C# is that instead of using a Long, you would use a Delegate which is strongly typed.
    Thank you for your explanation, Niya.

    I wonder whether it would be better to provide delegates through Implements. E.g:
    IScintilla.cls
    Code:
    Public Function DirectFunction(ByVal ptr As Long, ByVal iMessage As Long, ByVal wParam As Long, Byval lParam As Long) As Long
    Then use "Implements IScintilla" in other Classes to provide delegates. But I don't know if this method will lose some performance.
    Last edited by SearchingDataOnly; Feb 14th, 2021 at 11:06 AM.

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

    Re: Mapping between C# and VB6 (C# to VB6)

    Quote Originally Posted by SearchingDataOnly View Post
    Yes, using Long-Type to map IntPtr is the simplest and most effective method. I originally wanted to imitate the Int64 in vbCorLib to write an IntPtr class, but it seemed too complicated to do so.
    You could implement an Int64 type relatively easy if you wanted in VB6. The problem is that you cannot override operators in VB6 so you won't be able to use it like built-in primitives like Longs and Integers.

    The idea is that you create a class that represents the 64 bit Integer then you provide methods for operations like addition, subtraction, modulo etc. This is exactly how BigIntegers are implemented in .Net. A BigInteger is just a class with it's operators overloaded so it can be used as if it were like any other integer type. The thing is, overloading the operators is just a convenience for people using these types. It's perfectly fine to work with these types through methods. It will be more verbose and might annoy some people but it's not so bad.

    The tricky part of creating an 64 bit Integer type in VB6 would actually be the math. Internally the class would actually treat these Integers as two DWORDs or simply two Longs, one that represents the higher 4 bytes and one that represents the lower 4 bytes. Implementation of operations like addition and subtraction would involve some tricky bit-wise operations on these two Longs. It's not exactly easy but it's not exactly hard either. Even somebody like me who is not really that talented at math could figure it out in a day if I were so inclined.

    Quote Originally Posted by SearchingDataOnly View Post
    Thank you for your explanation, Niya.

    I wonder whether it would be better to provide delegates through Implements. E.g:
    IScintilla.cls
    Code:
    Public Function DirectFunction(ByVal ptr As Long, ByVal iMessage As Long, ByVal wParam As Long, Byval lParam As Long) As Long
    Then use "Implements IScintilla" in other Classes to provide delegates. But I don't know if this method will lose some performance.
    Not sure how to respond to this. Whether or not this is even necessary is completely dependent on what you're trying to do. Again, delegates are just function pointers. I don't see why you can't save yourself the trouble and just work with the function pointers directly. It's more raw and a lot less safe than a strongly typed delegate but if you know what you're doing you can get by. What are you trying to do anyway?
    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

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2020
    Posts
    314

    Re: Mapping between C# and VB6 (C# to VB6)

    Quote Originally Posted by Niya View Post
    You could implement an Int64 type relatively easy if you wanted in VB6. The problem is that you cannot override operators in VB6 so you won't be able to use it like built-in primitives like Longs and Integers.

    The idea is that you create a class that represents the 64 bit Integer then you provide methods for operations like addition, subtraction, modulo etc. This is exactly how BigIntegers are implemented in .Net. A BigInteger is just a class with it's operators overloaded so it can be used as if it were like any other integer type. The thing is, overloading the operators is just a convenience for people using these types. It's perfectly fine to work with these types through methods. It will be more verbose and might annoy some people but it's not so bad.

    The tricky part of creating an 64 bit Integer type in VB6 would actually be the math. Internally the class would actually treat these Integers as two DWORDs or simply two Longs, one that represents the higher 4 bytes and one that represents the lower 4 bytes. Implementation of operations like addition and subtraction would involve some tricky bit-wise operations on these two Longs. It's not exactly easy but it's not exactly hard either. Even somebody like me who is not really that talented at math could figure it out in a day if I were so inclined.
    https://github.com/kellyethridge/VBCorLib

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

    Re: Mapping between C# and VB6 (C# to VB6)

    Quote Originally Posted by SearchingDataOnly View Post
    They have an implementation of delegates in there. At a glance it looks like they are using the raw IUnknown interface of some kind of COM object to implement delegates. Not sure how that is meant to be used though. This Kelly Ethridge looks likes she knows her stuff however. You involved in that project? Are you Kelly Ethridge herself?
    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

  7. #7
    Fanatic Member
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    907

    Re: [RESOLVED] Mapping between C# and VB6 (C# to VB6)

    The stuff she provides is rather nifty, I am surprised we haven't heard more here about her CorLib libraries.

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

    Re: [RESOLVED] Mapping between C# and VB6 (C# to VB6)

    Kelly is a he, not a she.

    VBCorLib was last updated February 2, 2021

    More info at the VBCorLib for VB6 blog.

    Joe

  9. #9
    Fanatic Member
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    907

    Re: [RESOLVED] Mapping between C# and VB6 (C# to VB6)

    Quote Originally Posted by Joe Caverly View Post
    Kelly is a he, not a she.
    Ooops, Kelly is normally a girl's name over here. I saw the update, a useful tool set of polyfill tools for those wishing to move to VB.FRED, it would seem. I could certainly use some of that.

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