代码之家  ›  专栏  ›  技术社区  ›  Mr. Smith

C++中的BSTR和SySalk StrugByTeleNe()

  •  0
  • Mr. Smith  · 技术社区  · 15 年前

    我对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”。当它执行sysalloctringbytelen()部分时,帐号将成为中文符号的组合…

    有人能解释一下吗?

    3 回复  |  直到 15 年前
        1
  •  4
  •   1800 INFORMATION    15 年前

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

        2
  •  0
  •   Douglas Mayle    15 年前

    首先,包含safecopylen的行有问题。它不见了),也不清楚它应该做什么。

    第二个问题是,在这段代码中,您没有在任何地方使用accountno。tmpaccountnumber在堆栈中,可以包含任何内容。

        3
  •  0
  •   Igor Zevaka    15 年前

    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.
    }
    
    推荐文章