gpt4 book ai didi

c++ - Excel 在从 xll 插件返回的较长 unicode 字符串上崩溃

转载 作者:行者123 更新时间:2023-11-28 08:16:35 24 4
gpt4 key购买 nike

在 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)时将开始崩溃(一个这样的情况是“办公室 :”)。

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

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

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

有人知道吗?

最佳答案

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

关于c++ - Excel 在从 xll 插件返回的较长 unicode 字符串上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7550397/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com