-
Jun 2nd, 2022, 06:16 AM
#1
Thread Starter
Hyperactive Member
[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
-
Jun 2nd, 2022, 07:15 AM
#2
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
-
Jun 2nd, 2022, 07:33 AM
#3
Thread Starter
Hyperactive Member
Re: How to handle a callback which uses cDecl as its calling convention
Thanks a ton will take a look.
-
Jun 2nd, 2022, 07:38 AM
#4
Thread Starter
Hyperactive Member
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
-
Jun 2nd, 2022, 07:58 AM
#5
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
-
Jun 2nd, 2022, 09:42 AM
#6
Re: How to handle a callback which uses cDecl as its calling convention
-
Jun 2nd, 2022, 10:56 AM
#7
Re: How to handle a callback which uses cDecl as its calling convention
Originally Posted by vbwins
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.
-
Jun 2nd, 2022, 11:36 AM
#8
Re: How to handle a callback which uses cDecl as its calling convention
Originally Posted by The trick
I assume it could also work with callbacks and not just imports right? It doesn't explicitly say that it could handle this.
Originally Posted by dz32
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.
-
Jun 2nd, 2022, 11:53 AM
#9
Thread Starter
Hyperactive Member
Re: How to handle a callback which uses cDecl as its calling convention
Originally Posted by The trick
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.
-
Jun 2nd, 2022, 03:32 PM
#10
Re: How to handle a callback which uses cDecl as its calling convention
Originally Posted by Niya
I assume it could also work with callbacks and not just imports right? It doesn't explicitly say that it could handle this.
Originally Posted by vbwins
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
Last edited by The trick; Jun 2nd, 2022 at 03:43 PM.
-
Jun 3rd, 2022, 12:37 AM
#11
Thread Starter
Hyperactive Member
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.
-
Jun 3rd, 2022, 03:12 AM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|