Excel 在从 xll 插件返回的较长 unicode 字符串上崩溃
开发是在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 和 XLL 的组合当 XLL 返回短 unicode 字符串(即长度为 6 的 wchar_t)时,VBA 运行正常,但当返回较长的 unicode 字符串(即长度为 8 的 wchar_t)时,VBA 将开始崩溃(其中一种情况是“OFFICE :”)。
崩溃环境是Vista上的Excel 2007或Excel 2010。然而,XLL 和 XLL 的这种组合VBA 在 XP 上的另一台机器上的 Excel 2007 上运行完全没有问题。
我尝试在 XLL 插件函数中放置一个 try catch 块。没有发现异常。我还尝试在 VBA 代码中添加 ON ERROR 语句,但它也没有捕获任何内容。看起来崩溃发生在 XLL return 语句和 Excel VBA Application.Run 语句之间。我尝试在崩溃时检查正在运行的堆栈。如下:
- NTDLL.DLL(崩溃点,由于写入内存 0X000000000 )
- Kernal32.dll
- XLL addin DLL
- Excel.exe
有人有任何线索吗?
The development of is with C/C++ on top of MS SDK.
The C++ piece of XLL is as follows:
__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 (...) {
;
}
}
The Excel VBA crashes in the Application.Run line:
Dim var As String
var = Application.Run("xGetLang", key)
The combination of XLL & VBA runs ok when XLL returns short unicode string (i.e. with wchar_t of lenghth 6), but will start crashing when the longer unicode string (i.e. with wchar_t of lenghth 8) is returned (one such case is "OFFICE :").
The crashing environment is Excel 2007 or Excel 2010 on Vista. However, this combination of XLL & VBA runs with no issue at all on another machine Excel 2007 on XP.
I have tried to put a try catch block in the XLL addin function. There is no exception caught. I also tried to put an ON ERROR statement in the VBA code, it does not catch anything either. It looks like the crashing happens between XLL return statement and excel VBA Application.Run statment. I tried to check the running stack when it crashes. it is as follows:
- NTDLL.DLL (crashing point, due to writing to memory 0X000000000 )
- Kernal32.dll
- XLL addin DLL
- Excel.exe
Anybody has any clue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想摆脱 VBA,请使用 http://nxll.codeplex.com。 xll/utility/strings.h 中有用于将宽字符串转换为 MBCS 字符串的包装器。
If you want to get VBA out of the picture use http://nxll.codeplex.com. There are wrappers for converting wide to MBCS strings in xll/utility/strings.h.