====== Toymath ====== 作成日: 2023-08-22 (火) [[https://youtu.be/q7L6ZtljFRU|第2回 DLLについて]] ===== プロジェクト Toymath ===== ==== toymath.h ==== #ifndef TOYMATH_H #define TOYMATH_H #ifdef TOYMATH_EXPORT # define DECLSPEC __declspec(dllexport) #else # define DECLSPEC __declspec(dllimport) #endif #define TMCALL __stdcall #ifdef __cplusplus extern "C" { #endif DECLSPEC int TMCALL add(int x, int y); #ifdef __cplusplus } #endif #endif ==== toymath.cpp ==== #include "toymath.h" int TMCALL add(int x, int y) { return x + y; } ==== test_toymath.cpp ==== #include "toymath.h" #include int main() { int z = add(100, 200); assert(z == 300); } ==== toymath-vc.def ==== LIBRARY "toymath.dll" EXPORTS add = _add@8 ==== toymath-mgw.def ==== LIBRARY "toymath-mgw.dll" EXPORTS add = add@8 ==== Makefile ==== .PHONY: all all: vc .PHONY: vc mgw vc: toymath-vc.dll mgw: toymath-mgw.dll toymath-vc.dll: toymath.cpp toymath.h toymath-vc.def cl /LD /EHsc /DTOYMATH_EXPORT $< /link /DEF:toymath-vc.def toymath-mgw.dll: toymath.cpp toymath.h g++ -shared -DTOYMATH_EXPORT -o $@ $< -Wl,--out-implib,libtoymath-mgw.dll.a,--add-stdcall-alias .PHONY: clean clean: ${RM} *.exe *.dll *.obj *.lib *.exp *.a ===== VBAモジュール Toymath ===== Declare Function Add Lib "toymath-mgw.dll" Alias "add" (ByVal X As Long, ByVal Y As Long) As Long Sub test_Add() ChDir "C:\Users\happycat\Documents\code\excel-dll\toymath" Z = Add(100, 200) Debug.Assert Z = 300 End Sub