ユーザ用ツール

サイト用ツール


youtube:vba-dll-002

Toymath

作成日: 2023-08-22 (火)

第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 <cassert>

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

youtube/vba-dll-002.txt · 最終更新: 2024/02/28 18:26 by freemikan

特に明示されていない限り、本Wikiの内容は次のライセンスに従います: CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki