1

I have a dll that use this signature:

SFetch(cursor As Integer, pstruct As Any, size As Integer) As Integer

I can call it with simple variables like Integer or more complexe one with custom types.

I want to create a function that would receive a parameter and send it to the dll, I tried different solutions but always have errors.

In all the cases, this is how I called it:

Dim val As Integer
...
.ExecuteRead val, LenB(val)

Public Sub ExecuteRead(ByVal pstruct, structSize As Integer)
        SFetch1 c, pstruct, structSize

Returned Null instead of a value


Public Sub ExecuteRead(pstruct, structSize As Integer)
        SFetch1 c, pstruct, structSize

Returned Variable uses an Automation type not supported in Visual Basic


Public Sub ExecuteRead(pstruct As Variant, structSize As Integer)
        SFetch1 c, pstruct, structSize

Returned Variable uses an Automation type not supported in Visual Basic


Public Sub ExecuteRead(pstruct As Object, structSize As Integer)
        SFetch1 c, pstruct, structSize

Can't call the function ByRef argument type mismatch


Public Sub ExecuteRead(pstruct As Any, structSize As Integer)

Won't even compile, Syntax error

Alex Dupuis
  • 366
  • 3
  • 14
  • In `VBA` use `Long` for anything that is `Integer` in VB. `Integer` in VBA and in VB are quite different. – Vityata Oct 05 '18 at 13:08
  • 1
    declare as `SFetch(cursor As Integer, ByVal pstruct As LongPtr, size As Integer) As Integer` and call with `.ExecuteRead VarPtr(val), LenB(val)`. – Florent B. Oct 05 '18 at 13:32
  • @Vityata, Yeah, I know but I tend to forgot that I should not use Integer in VBA because it's converted to Long any way behind the scene. But thanks for pointing out ;) – Alex Dupuis Oct 05 '18 at 13:55
  • @FlorentB., I didn't get any errors by working with pointers but, I'm not really used to pointers in VBA, how can I get the content of my variable avec that? – Alex Dupuis Oct 05 '18 at 13:57
  • 1
    @AlexDupuis - it is not only the behind the scene conversion (which is quite not relevant here), but if you use Integer in VBA, then VB cannot parse it to its own Integer. Some time ago I wrote [an article](https://www.vitoshacademy.com/c-adding-c-function-to-vbaexcel/) where the `Integer` of the function in the dll library was "transalated" as `Long` in VBA. – Vityata Oct 05 '18 at 14:00
  • 1
    @Vityata, thanks, didn't know that ;) – Alex Dupuis Oct 05 '18 at 14:09
  • @FlorentB., my bad, when I called `SFetch`, I totally forgot to add the ByVal, with that, the value is changed in the variable (which make sens ;) ) Thanks for the help. – Alex Dupuis Oct 05 '18 at 14:21

1 Answers1

1

Just realized that I don't have an official answer so there it is.

This is the function declaration, including the call to my external function:

Public Sub ExecuteRead(ByVal pstruct As LongPtr, structSize As Integer)
    SFetch1 dataCur, ByVal pstruct, structSize

There is how I call it from my main program:

obj.ExecuteRead VarPtr(returnGrp), LenB(returnGrp)
Alex Dupuis
  • 366
  • 3
  • 14