Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Excel在xll加载项返回的较长unicode字符串上崩溃_C++_Vba_Add In_Xll - Fatal编程技术网

C++ Excel在xll加载项返回的较长unicode字符串上崩溃

C++ 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

在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 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 (...) {
        ;
    }

}
  • NTDLL.DLL(崩溃点,由于写入内存0X000000000)
  • 内核32.dll
  • XLL加载项DLL
  • Excel.exe

  • 有人有任何线索吗?

    如果您想从图片中删除VBA,请使用。xll/utility/strings.h中有用于将wide字符串转换为MBCS字符串的包装器。

    在Excel中注册此函数时使用的文本类型是什么?您是否尝试过在VBA中使用Declare Function语句直接调用XLL?