Skip to content

Instantly share code, notes, and snippets.

@derrickturk
Created March 6, 2014 05:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derrickturk/9382963 to your computer and use it in GitHub Desktop.
Save derrickturk/9382963 to your computer and use it in GitHub Desktop.
Callback functions in VBA: VBA calls DLL calls VBA.
Option Explicit
Declare Sub squareItAndThen Lib "callback.dll" _
(ByVal x As Double, ByVal f As Long)
Public Sub EchoSquares()
Dim x As Double
ChDir ActiveWorkbook.Path
For x = 1 To 10 Step 0.5
squareItAndThen x, AddressOf Echo
Next x
End Sub
Public Sub Echo(ByVal d As Double)
Debug.Print d
End Sub
extern "C" {
using callback [[gnu::stdcall]] = void (*)(double);
[[gnu::dllexport]]
void squareItAndThen [[gnu::stdcall]] (double x, callback f)
{
f(x * x);
}
}
CXX=g++
CXXOPTS=-std=c++11 -pedantic -Wall -Wextra -Werror -static-libgcc -static-libstdc++
OPTOPTS=-O3 -fno-exceptions -fno-rtti -ffast-math -mfpmath=387 -msse3 -mtune=core2 -funroll-loops -s
DLLOPTS=-shared
LINKOPTS=-Wl,--add-stdcall-alias
LOG=
#LOG=-DLOGGING="log.txt"
callback.dll: callback.o
$(CXX) $(CXXOPTS) $(LOG) $(OPTOPTS) $(DLLOPTS) -o callback.dll callback.o $(LINKOPTS)
callback.o: callback.cpp
$(CXX) $(CXXOPTS) $(LOG) $(OPTOPTS) -c callback.cpp
clean:
-rm callback.dll
-rm callback.o
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment