2

我是 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() 部分时,帐号变成了中文符号的组合......

任何人都可以对此有所了解吗?

4

3 回答 3

6

SysAllocStringByteLen用于创建包含二进制数据而不是实际字符串的 BSTR - 不执行 ANSI 到 unicode 的转换。这解释了为什么调试器将字符串显示为包含明显的中文符号,它试图将复制到 BSTR 中的 ANSI 字符串解释为 unicode。您可能应该改用SysAllocString -这会将字符串正确转换为 unicode,您必须将其传递给 unicode 字符串。如果您使用的是实际文本,那么这是您应该使用的功能。

于 2009-09-16T08:17:31.910 回答
0

首先,包含 SAFECOPYLEN 的行有问题。它缺少')'并且不清楚它应该做什么。

第二个问题是您没有在此代码中的任何地方使用 AccountNo。tmpAccountNumber 在堆栈上,并且可以包含任何内容。

于 2009-09-16T08:10:38.037 回答
0

BSTR 是双字节字符数组,因此您不能只将 char* 数组复制到其中。而不是通过它"A12123"尝试L"A12323"

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

//Continue with other stuff here.
}
于 2009-09-16T08:20:00.910 回答