在Mac VBA中调用dylib函数

Calling dylib functions in Office for Mac VBA

本文关键字:dylib 函数 调用 Mac VBA      更新时间:2023-10-16

我试图调用一个简单的函数,它存储在一个简单的dylib文件中,从一个简单的Word for Mac宏,

我用Xcode5在OS-X Mountain Lion上创建。dylib,并从Word for Mac 2011中调用它。

首先是libWord.dylib:

Test.h

#ifndef __Word__Test__
#define __Word__Test__
bool testFunc();
#endif

Test.cpp

#include "Test.h"
bool testFunc(){
    return true;
}

和宏:

词宏观

Private Declare Function testFunc Lib "/Users/usrName/Documents/libWord.dylib" () As Boolean
Sub TestLibFunc()
    Dim b As Boolean
    b = testFunc
    StatusBar = b
End Sub

宏可以找到dylib(我放在上面的目录中),但是一直抛出:

"运行时错误'453':指定DLL函数未找到"

我也尝试过将函数声明为类的一部分:

class testClass{
    static bool testFunc();
}
bool testClass::testFunc(){
    return true;
}

,然后尝试使用上面的Declare语句和一个带有别名的语句来调用它:

Private Declare Function testFunc Lib "/Users/usrName/Documents/libWord.dylib" Alias "testClass::testFunc" () As Boolean

我还尝试用":"替换库路径名中的"/",所有这些都给出相同的结果。

那么,我错过了什么?就我所见过的所有例子而言:

Office 2011 for Mac中的VBA Shell功能

在MacOSX中返回字符串到VBA

http://social.msdn.microsoft.com/forums/en us/b060f291 - 0754 - 4 - e85燃料a7b9 e64259e6baad/vba -使用-共享-图书馆-办公室- 2011 mac?forum=isvvba

上面的应该工作(以同样的方式,遥控器应该正好在你离开它的地方)。但显然我一定是做错了什么,任何关于在哪里看的指示都是受欢迎的(越明显和羞辱越好)。

与其查找混乱的名称,我认为最简单的方法是将函数声明包装起来:

extern "C" {
bool testFunc();
}

成功!(呃,一直都是在那里 ?)

本教程简要介绍了从VBA调用.dylib函数的基础知识。

请确保安装了Xcode命令行工具,以便您可以使用nm命令从库中获取符号