Thank you for your comments.
I'd suggest to have an initial buffer of 64 bytes allocated on the stack, and allocate a larger buffer only if required. That improves performance.
lpChar and Str are referenced after XmbLookupString() or XLookupString() is called. If both a buffer on stack and a buffer on heap can be created, every time the buffer is referenced, I need to check whether the used buffer is one on stack or not:
if (isStack) { lpChar[0] = 0; } else { lpChar2[0] = 0; }
This implementation is ugly and won't improve performance so much.