0

开发是在 MS SDK 之上使用 C/C++ 进行的。

XLL的C++部分如下:

__declspec(dllexport) LPWSTR WINAPI xlGetLang(LPSTR in_key) {

    try {

    static XLOPER12 lang;
    static size_t buffer_size = 0;
    static wchar_t * buffer = NULL;

    std::wstring unicode_str;

    // This step get the unicode string assigned to unicode_str
    // with the key in_key from internal dictionary.
    pms::get_instance()->get_lang(in_key, unicode_str);
    // over

    size_t msg_len = unicode_str.length();

    // This step checks whether we need to incraese the buffer
    // for the unicode string to be returned.
    if (buffer_size == 0) {
        buffer = (LPWSTR) malloc(sizeof(wchar_t) * (msg_len + 1));
        buffer_size = msg_len;
    } else if (buffer_size < msg_len) {
        buffer = (LPWSTR) realloc(buffer, sizeof(wchar_t) * (msg_len + 1));
        buffer_size = msg_len;
    }
    // over

    wcsncpy(buffer, unicode_str.c_str(), msg_len);
    buffer[msg_len] = 0;

    return buffer;

    }

    catch (...) {
        ;
    }

}

Excel VBA 在 Application.Run 行中崩溃:

Dim var As String
var = Application.Run("xGetLang", key)

当 XLL 返回短 unicode 字符串(即 wchar_t 的长度为 6)时,XLL 和 VBA 的组合运行正常,但当返回较长的 unicode 字符串(即 wchar_t 的长度为 8)时将开始崩溃(一种情况是“OFFICE: ”)。

崩溃的环境是 Excel 2007 或 Vista 上的 Excel 2010。但是,这种 XLL 和 VBA 的组合在 XP 上的另一台机器 Excel 2007 上运行完全没有问题。

我试图在 XLL 插件函数中放置一个 try catch 块。没有捕获到异常。我还尝试在 VBA 代码中放置一个 ON ERROR 语句,它也没有捕获任何内容。看起来崩溃发生在 XLL return 语句和 excel VBA Application.Run 语句之间。当它崩溃时,我试图检查正在运行的堆栈。如下:

  1. NTDLL.DLL(崩溃点,由于写入内存 0X000000000 )
  2. 内核32.dll
  3. XLL 插件 DLL
  4. Excel.exe

有人有任何线索吗?

4

1 回答 1

1

如果您想摆脱 VBA,请使用http://nxll.codeplex.com。在 xll/utility/strings.h 中有用于将宽转换为 MBCS 字符串的包装器。

于 2011-09-30T22:51:50.530 回答