c++excelvisual-c++visual-studio-2005xll

XLL add-in Development - Excel SDK function issue


Currently I'm developing an Excel 2010 Add-In (.xll) with the Excel SDK 2010. Unfortunately there is little documentation from Microsoft (or perhaps I just haven't found it yet). I reached the point that my Add-In is loaded inside of Excel, its menu appears, and I'm able to invoke a function from the menu, which does get executed. Inside of it I try to gather all the Sheet names, but every call to the function results in a different result. The correct Sheet names are present in the returned XLOPER but they are surrounded by changing "weird" characters.

The code in question:

LPXLOPER12 GetWorkbook(void){
    LPXLOPER12 workbooksheets=new XLOPER12,xworkbookname = new XLOPER12;

    memset(xworkbookname,0,sizeof(XLOPER12));
    memset(workbooksheets,0,sizeof(XLOPER12));

    Excel12f(xlfGetDocument,xworkbookname,1,TempInt12(88));
    Excel12f(xlfGetWorkbook,workbooksheets,2,TempInt12(1),xworkbookname);

    // at this point I expect xworkbookname->val.str to contain the workbook name
    // but instead it has garbage before and after
    return 0;
}

Solution

  • You still haven't provided much information here, but I'm going to guess.

    Since computers work with numbers, there has to be a convention for how text is represented. One convention, used by the C programming language, is to say "the first character of the text is in the first memory location, and the text continues until you hit a byte/word with the value of 0". Since you're writing in C, I guess that's what you're expecting.

    But Microsoft didn't originally develop Excel in C. They developed in in Pascal. And the convention used by Pascal is "the first memory location contains the length of the text, and the actual text starts at the second memory location, going for as many memory locations as is specified by the length. There is no terminating 0."

    There's actually quite a lot of documentation available on the MSDN site, for instance http://msdn.microsoft.com/en-us/library/aa730920%28v=office.12%29.aspx

    In particular, they have a graphic (and sample code) showing what routines you need to use to convert between Pascal strings and C strings.