Excel VBA:実行時エラー 49:C++ dll を呼び出す不適切な DLL 呼び出し規則



Excel-VBA から C++ DLL を呼び出そうとしています。


実行を追跡するために fputs() ロギング呼び出しを挿入すると、DLL 関数が実行されていることがわかり、スタンプがログ ファイルに表示されます。問題は、DLL 関数が戻るたびにエラー 49 が発生することです。


VBA での宣言は次のとおりです。


Private Declare Function InitMCR Lib "MCRBoilerplate.dll" Alias "[email protected]@YGXXZ" ()

これが C++ での宣言です


__declspec(dllexport) void __stdcall initMCR() { ... }

DLL 呼び出しが機能しているように見えるのに、このエラー 49 の動作が発生するのはなぜですか?


答え:


VBA では、void を返す関数 Sub として宣言する必要があります Function の代わりに


したがって、VBA での宣言は次のようになります。


Private Declare Sub InitMCR Lib "MCRBoilerplate.dll" Alias "[email protected]@YGXXZ" ()

VBA Declare ステートメントに関する MSDN ページを参照してください


いくつかのコードの回答


// MathLibrary.h - Contains declarations of math functions #pragma once  #ifdef MATHLIBRARY_EXPORTS #define MATHLIBRARY_API __declspec(dllexport) #else #define MATHLIBRARY_API __declspec(dllimport) #endif  // The Fibonacci recurrence relation describes a sequence F // where F(n) is { n = 0, a //   { n = 1, b //   { n >
1, F(n-2) + F(n-1) // for some initial integral values a and b. // If the sequence is initialized F(0) = 1, F(1) = 1, // then this relation produces the well-known Fibonacci // sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, ... // Initialize a Fibonacci relation sequence // such that F(0) = a, F(1) = b. // This function must be called before any other function. extern "C" MATHLIBRARY_API void fibonacci_init(
const unsigned int a, const unsigned int b);
// Produce the next value in the sequence. // Returns true on success and updates current value and index;
// false on overflow, leaves current value and index unchanged. extern "C" MATHLIBRARY_API bool fibonacci_next();
// Get the current value in the sequence. extern "C" MATHLIBRARY_API unsigned int fibonacci_current();
// Get the position of the current value in the sequence. extern "C" MATHLIBRARY_API unsigned fibonacci_index();
Public Declare Sub fibonacci_init Lib "C:\development\MathLibrary\Release\MathLibrary.dll" (ByVal a As Integer, ByVal a As Integer) Public Declare Function fibonacci_next Lib "C:\development\MathLibrary\Release\MathLibrary.dll" () As Boolean Public Declare Function fibonacci_current Lib "C:\development\MathLibrary\Release\MathLibrary.dll" () As Integer  Public Function run_dll()
Dim b As Integer
Call fibonacci_init(1, 1)
b = fibonacci_current() End Function
const char* str = "abcdefg";
extern "C" __declspec(dllexport) int size() {
return strlen(str);
} extern "C" __declspec(dllexport) bool test(char* pReturn) {
int nSize = strlen(str);
lstrcpynA(pReturn, str, nSize);
return true;
}
Public Declare Function size Lib "C:\development\MathLibrary\Release\Trial.dll" () As Long Public Declare Function test Lib "C:\development\MathLibrary\Release\Trial.dll" (ByVal p As Long) As Boolean  (1) Public Function run_dll() (2)    Dim bb As Boolean (3)    Dim sz As Integer (4)    Dim s As String
(5) sz = size()
(6) s = Space(sz)
(7) bb = test(StrPtr(s)) (8) End Function

No