Search code examples
c++cmacoslibxl

Can't pass wchar_t to LibXL writeStr


I'm tring to write Cyrillic characters into a spreadsheet that's being generated in an OSX app:

sheet = xlBookAddSheet(book, "Output", 0);
detailFormat = xlBookAddFormat(book, 0);
wchar_t *w_user = (wchar_t*)malloc(max_chars * sizeof(wchar_t));
//get wchar_t characters...
xlSheetWriteStr(sheet, row, column, w_user, detailFormat);

but the last line won't compile as the xlSheetWriteStr is expecting const char* in place of w_user.

According to the docs I should be able to pass in const wchar_t*. Is there any way of writing my international characters to a cell?


Solution

  • LibXL provides headers for the ANSI and UNICODE versions of the functions.

    Programs willing to use the unicode version of an API should define the UNICODE macro. If you are using Visual Studio it is as easy as going to Project -> Properties -> Character Set and set it to Use Unicode Character Set.

    What you see as xlSheetWriteStr is actually a macro that ends up having the value xlSheetWriteStrA or xlSheetWriteStrW. If UNICODE is not defined the former, if it is de later.

    The function xlSheetWriteStrA is declared in SheetA.h, whereas xlSheetWriteStrW is declared in SheetW.h.

    Note: It is better if you are consistent. If you use the unicode version of a function, use the unicode version of all functions. I say this because in your example I see you are using the ansi version of xlBookAddSheet.

    Update: For the OSX version of the library the macro is _UNICODE, with the preceding underscore (See libxl.h for details).