cking
cking

Reputation: 1

Excel crashing on longer unicode string returned from xll addin

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:

  1. NTDLL.DLL (crashing point, due to writing to memory 0X000000000 )
  2. Kernal32.dll
  3. XLL addin DLL
  4. Excel.exe

Anybody has any clue?

Upvotes: 0

Views: 526

Answers (1)

Keith A. Lewis
Keith A. Lewis

Reputation: 771

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.

Upvotes: 1

Related Questions