Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BSTR and SysAllockStringByteLen() in C++

I'm new to C++, so this may be a noobish question; I have the following function:

#define SAFECOPYLEN(dest, src, maxlen)                               \
{                                                                    \
    strncpy_s(dest, maxlen, src, _TRUNCATE);                          \
    dest[maxlen-1] = '\0';                                            \
}

short _stdcall CreateCustomer(char* AccountNo)
{
    char tmpAccountNumber[9];
    SAFECOPYLEN(tmpAccountNumber, AccountNo, 9);
    BSTR strAccountNumber = SysAllocStringByteLen(tmpAccountNUmber, 9);

    //Continue with other stuff here.
}

When I debug through this code, I pass in the account number "A101683" for example. When it does the SysAllocStringByteLen() part, the account number becomes a combination of Chinese symbols...

Anyone that can shed some light on this?

like image 984
Mr. Smith Avatar asked Jan 28 '26 14:01

Mr. Smith


1 Answers

SysAllocStringByteLen is meant for when you are creating a BSTR containing binary data, not actual strings - no ANSI to unicode conversion is performed. This explains why the debugger shows the string as containing apparently chinese symbols, it is trying to interpret the ANSI string copied into the BSTR as unicode. You should probably use SysAllocString instead - this will convert the string correctly to unicode you must pass it a unicode string. If you are working with actual text, this is the function you should be using.

like image 71
1800 INFORMATION Avatar answered Jan 31 '26 03:01

1800 INFORMATION



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!