我是 C++ 新手,所以这可能是一个新手问题;我有以下功能:
#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.
}
当我调试这段代码时,我传入了帐号“A101683”。当它执行SysAllocStringByteLen()部分时,帐号就变成了中文符号的组合...
有谁可以解释一下吗?
最佳答案
SysAllocStringByteLen适用于当您创建包含二进制数据而不是实际字符串的 BSTR 时 - 不执行 ANSI 到 unicode 的转换。这解释了为什么调试器将字符串显示为包含明显的中文符号,它试图将复制到 BSTR 中的 ANSI 字符串解释为 unicode。您可能应该使用 SysAllocString相反 - 这会将字符串正确转换为 unicode 您必须向其传递一个 unicode 字符串。如果您正在处理实际文本,那么您应该使用此函数。
关于.net - C++ 中的 BSTR 和 SysAllockStringByteLen(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1431598/