Results 1 to 12 of 12

Thread: [RESOLVED] How to handle a callback which uses cDecl as its calling convention

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2019
    Posts
    416

    Resolved [RESOLVED] How to handle a callback which uses cDecl as its calling convention

    Further to this does anybody know how to handle this. I can only assume that this is my problem at this stage.

    WinPcap callbacks are called using cDecl

    https://www.vbforums.com/showthread....nPcap-callback

  2. #2
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,056

    Re: How to handle a callback which uses cDecl as its calling convention

    I am pretty sure I have an example of a cdecl callback here:

    http://sandsprite.com/CodeStuff/vb_cdecl.zip

    The best solution is to write a small c dll to wrap the api
    And proxy the results back to your vb app. Often times this can also simplify the rest of the c api and struct etc to keep translation to vb at a minimum.

    Sometimes I also tweak and recompile the c code to make it more vb friendly if it is open source and doesn’t have bad build requirements

    The intermediate dll also allows easy updates vrs the recompile approach

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2019
    Posts
    416

    Re: How to handle a callback which uses cDecl as its calling convention

    Thanks a ton will take a look.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2019
    Posts
    416

    Re: How to handle a callback which uses cDecl as its calling convention

    Looks like its a bit above my kindergarten c++ Pretty much what I have to achieve is that winpcap calls the c++ callback which then std calls my vb function.

    Its a start. thanks

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: How to handle a callback which uses cDecl as its calling convention

    Yeah, had the same thought as dz32, but with Freepascal :-)
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  6. #6

  7. #7
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,056

    Re: How to handle a callback which uses cDecl as its calling convention

    Quote Originally Posted by vbwins View Post
    Looks like its a bit above my kindergarten c++ Pretty much what I have to achieve is that winpcap calls the c++ callback which then std calls my vb function.

    Its a start. thanks
    Yup, any time you invest in learning to integrate c with your vb will be well spent. It’s like putting money in the bank. There is so much c code out there and it can interact very seamlessly with vb6.

    I can’t recommend it highly enough.

  8. #8
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: How to handle a callback which uses cDecl as its calling convention

    Quote Originally Posted by The trick View Post
    I assume it could also work with callbacks and not just imports right? It doesn't explicitly say that it could handle this.

    Quote Originally Posted by dz32 View Post
    Yup, any time you invest in learning to integrate c with your vb will be well spent. It’s like putting money in the bank. There is so much c code out there and it can interact very seamlessly with vb6.

    I can’t recommend it highly enough.
    I will second this. You don't have to master it but learning C/C++ provides unimaginable value.
    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

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2019
    Posts
    416

    Re: How to handle a callback which uses cDecl as its calling convention

    Quote Originally Posted by The trick View Post
    Thanks trick but the problem is in the other direction. A winpcap c++ dil,is calling back a vb function but using cdecl to do so.

  10. #10
    PowerPoster
    Join Date
    Feb 2015
    Posts
    2,672

    Re: How to handle a callback which uses cDecl as its calling convention

    Quote Originally Posted by Niya View Post
    I assume it could also work with callbacks and not just imports right? It doesn't explicitly say that it could handle this.
    Quote Originally Posted by vbwins View Post
    Thanks trick but the problem is in the other direction. A winpcap c++ dil,is calling back a vb function but using cdecl to do so.
    Feel free to read description )) :
    ...Moreover this Add-in adds the ability to use the CDecl keyword for user functions...
    There is a special section in the description called "How to enable CDecl keyword for user functions?".

    You can see the example of a CDECL callback:
    Code:
    ' //
    ' // qsort C library usage
    ' // by The trick 2021
    ' //
    
    Option Explicit
    
    Private Declare Sub qsort CDecl Lib "msvcrt" ( _
                             ByRef pFirst As Any, _
                             ByVal lNumber As Long, _
                             ByVal lSize As Long, _
                             ByVal pfnComparator As Long)
                    
    Sub Main()
        Dim z() As Long
        Dim i As Long
        Dim s As String
        
        ReDim z(10)
        
        For i = 0 To UBound(z)
            z(i) = Int(Rnd * 1000)
        Next
        
        qsort z(0), UBound(z) + 1, LenB(z(0)), AddressOf Comparator
        
        For i = 0 To UBound(z)
            Debug.Print z(i)
        Next
    
    End Sub
    
    Private Function Comparator CDecl( _
                     ByRef a As Long, _
                     ByRef b As Long) As Long
        Comparator = a - b
    End Function

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2019
    Posts
    416

    Re: How to handle a callback which uses cDecl as its calling convention

    I did read the description and thank you for your help. I take it that the patch is only applied when the add in is enabled?

    I am also not sure how to build the add in as I have never done this before but will read the documentation carefully and give it a go.

    EDIT - Ok that was simple. It does work and it does solve the immediate problem.

    Thanks The Trick
    Last edited by vbwins; Jun 3rd, 2022 at 12:58 AM.

  12. #12
    PowerPoster
    Join Date
    Jun 2012
    Posts
    2,375

    Re: How to handle a callback which uses cDecl as its calling convention

    Just for completeness to this thread. Below a small ASM wrapper for cdecl callbacks. (by Paul Caton)
    That for example I use for my VBSQLite lib.

    Code:
    Option Explicit
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long)
    Private Declare Function VirtualAlloc Lib "kernel32" (ByRef lpAddress As Long, ByVal dwSize As Long, ByVal flAllocType As Long, ByVal flProtect As Long) As Long
    Private Declare Function VirtualFree Lib "kernel32" (ByRef lpAddress As Long, ByVal dwSize As Long, ByVal dwFreeType As Long) As Long
    Private Const MEM_COMMIT As Long = &H1000
    Private Const MEM_RELEASE As Long = &H8000&
    Private Const PAGE_EXECUTE_READWRITE As Long = &H40
    
    Sub Main()
        Dim z() As Long
        Dim i As Long
        Dim s As String
        
        ReDim z(10)
        
        For i = 0 To UBound(z)
            z(i) = Int(Rnd * 1000)
        Next
        
        Dim asm_wrapper As Long
        asm_wrapper = CreateCDeclCallback(AddressOf Comparator, 8)
        
        qsort z(0), UBound(z) + 1, LenB(z(0)), asm_wrapper
        
        VirtualFree ByVal asm_wrapper, 0, MEM_RELEASE: asm_wrapper = 0
        
        For i = 0 To UBound(z)
            Debug.Print z(i)
        Next
    
    End Sub
    
    Private Function Comparator( _
                     ByRef a As Long, _
                     ByRef b As Long) As Long
        Comparator = a - b
    End Function
    
    
    Private Function CreateCDeclCallback(ByVal Address As Long, ByVal cbParam As Byte) As Long
    ' Thanks to Paul Caton's Call CDECL source code.
    Dim ASMWrapper As Long
    ASMWrapper = VirtualAlloc(ByVal 0&, 28, MEM_COMMIT, PAGE_EXECUTE_READWRITE)
    Dim asm(0 To 2) As Currency
    asm(0) = 465203369712025.6232@
    asm(1) = -140418483381718.8329@
    asm(2) = -4672484613390.9419@
    CopyMemory ByVal ASMWrapper, ByVal VarPtr(asm(0)), 24
    CopyMemory ByVal UnsignedAdd(ASMWrapper, 24), &HC30672, 4
    CopyMemory ByVal UnsignedAdd(ASMWrapper, 10), UnsignedSub(UnsignedSub(Address, ASMWrapper), 14), 4
    CopyMemory ByVal UnsignedAdd(ASMWrapper, 16), cbParam, 1
    CreateCDeclCallback = ASMWrapper
    End Function
    
    Private Function UnsignedAdd(ByVal Start As Long, ByVal Incr As Long) As Long
    UnsignedAdd = ((Start Xor &H80000000) + Incr) Xor &H80000000
    End Function
    
    Private Function UnsignedSub(ByVal Start As Long, ByVal Decr As Long) As Long
    UnsignedSub = ((Start And &H7FFFFFFF) - (Decr And &H7FFFFFFF)) Xor ((Start Xor Decr) And &H80000000)
    End Function
    In above example it uses the qsort cdecl API. Of course you would need a universal call wrapper which allows you to call that API. This is just a way to get the callback working.

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