C++ Excel在xll加载项返回的较长unicode字符串上崩溃
在mssdk上用C/C++开发isC++ Excel在xll加载项返回的较长unicode字符串上崩溃,c++,vba,add-in,xll,C++,Vba,Add In,Xll,在mssdk上用C/C++开发is 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 ste
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 (...) {
;
}
}
当XLL返回较短的unicode字符串(即,长度为6的wchar____________________________________________________
崩溃的环境是Vista上的Excel 2007或Excel 2010。但是,XLL和VBA的这种组合在另一台XP上的Excel2007机器上运行时没有任何问题
我已经尝试在XLL addin函数中放置一个try-catch块。没有例外。我还尝试在VBA代码中添加一个ON错误语句,它也无法捕获任何内容。似乎XLL return语句和excel VBA应用程序之间发生了崩溃。请运行station。我试图在堆栈崩溃时检查正在运行的堆栈。详情如下:
__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 (...) {
;
}
}
有人有任何线索吗?如果您想从图片中删除VBA,请使用。xll/utility/strings.h中有用于将wide字符串转换为MBCS字符串的包装器。在Excel中注册此函数时使用的文本类型是什么?您是否尝试过在VBA中使用Declare Function语句直接调用XLL?