Created
March 6, 2014 05:05
-
-
Save derrickturk/9382963 to your computer and use it in GitHub Desktop.
Callback functions in VBA: VBA calls DLL calls VBA.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern "C" { | |
using callback [[gnu::stdcall]] = void (*)(double); | |
[[gnu::dllexport]] | |
void squareItAndThen [[gnu::stdcall]] (double x, callback f) | |
{ | |
f(x * x); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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