○VBAとVisualC++(Win32 DLL)

Cの処理速度とVBAの手軽さの合体です


■VCにて作成したWin32 DLLをVBAから読み出します。

Public Declare Function getDouble Lib "c:\test.dll" () As Double
Public Declare Sub setDouble Lib "c:\test.dll" (ByVal b As Double)

Public Function test()

    On Error Resume Next
    setDouble (10.005)
    Dim a As Double
    a = getDouble()
    MsgBox (a)
    On Error GoTo 0
    
End Function

出力結果
20.01

----------以下VC Win32 DLLコード----------
コメントにVBAのAPIの呪文を書いてあります

#include <windows.h>

//■VBAでのInteger
//▽VBAでのAPI宣言
//Public Declare Function getShort Lib "c:\test.dll" () As Integer
short shortPos;
extern "C" __declspec(dllexport) short getShort(void){
	return shortPos*2;
}

//▽VBAでのAPI宣言
//Public Declare Sub setShort Lib "c:\test.dll" (ByVal b As Integer)
extern "C" __declspec(dllexport) void setShort(short b){
	shortPos=b;
}

//■VBAでのLong
//▽VBAでのAPI宣言
//Public Declare Function getInt Lib "c:\test.dll" () As Long
int intPos;
extern "C" __declspec(dllexport) int getInt(void){
	return intPos*2;
}

//▽VBAでのAPI宣言
//Public Declare Sub setInt Lib "c:\test.dll" (ByVal b As Long)
extern "C" __declspec(dllexport) void setInt(int b){
	intPos=b;
}

//■VBAでのDouble
//▽VBAでのAPI宣言
//Public Declare Function getDouble Lib "c:\test.dll" () As Double
double doublePos;
extern "C" __declspec(dllexport) double getDouble(void){
	return doublePos*2;
}

//▽VBAでのAPI宣言
//Public Declare Sub setDouble Lib "c:\test.dll" (ByVal b As Double)
extern "C" __declspec(dllexport) void setDouble(double b){
	doublePos=b;
}

//■VBAでのString
//▽VBAでのAPI宣言
//Public Declare Sub setChar Lib "c:\test.dll" (ByVal str As String)
extern "C" __declspec(dllexport) void setChar(char*a){
	MessageBox(NULL,a,"char", MB_OK);
}



▲トップページ > Visual BASIC と C#