C++ 中的 BSTR 和 SysAllockStringByteLen()

发布于 2024-08-05 02:01:58 字数 703 浏览 8 评论 0原文

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

任何人都可以解释这一点吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无声情话 2024-08-12 02:01:58

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

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.

泛滥成性 2024-08-12 02:01:58

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

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

First of all, there's a problem with the line containing SAFECOPYLEN. It's missing ')' and it's not clear what it's supposed to do.

The second issue is that you're not using AccountNo anywhere in this code. tmpAccountNumber is on the stack, and could have anything in it.

辞取 2024-08-12 02:01:58

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.
}

BSTR are double-byte character arrays so you can't just copy a char* array into it. Instead of passing it "A12123" try 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.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文