_com_util::ConvertStringToBSTR 使用时的注意事项

在进行COM编程的时候常常用到VARIANT类型的变量, 其中的字符串分量为bstrVal即双字节的BSTR, 如果需要将其转换为字符串STRING我以前使用强制类型转换:
    (const char*)bsVal;
在看了潘爱民老师的<<COM编程原理>>偶尔发现如下函数(例子摘自MSDN)

   //compile options needed: /Gr or /Gz
   #include <comutil.h>
   int main()
   {
       char sz[]="hello";
       _bstr_t b;
       b = _com_util::ConvertStringToBSTR(sz);
       char * p = _com_util::ConvertBSTRToString(b);
       return 1;
   }


注意: 上述代码编译时候出现如下错误

error LNK2001: unresolved external symbol "char * __fastcall
   _com_util::ConvertBSTRToString(unsigned short *)"
   (?ConvertBSTRToString@_com_util@@YIPADPAG@Z)

   error LNK2001: unresolved external symbol "unsigned short * __fastcall
   _com_util::ConvertStringToBS
   TR(char const *)" (?ConvertStringToBSTR@_com_util@@YIPAGPBD@Z)

解决: 在Link页加入comsupp.lib就可以了, 不过我的程序编译的时候没有上述错误, 但是在同事的机器上出现了.

我的用法 -- _com_util::ConvertStringToBSTR();

    char*   pTemp;
    CString csTemp;

    pTemp = _com_util::ConvertBSTRToString(bsVal);
    csTemp = pTemp;
    delete pTemp;
    pTemp = NULL;

    // 这是我们公司的一位高手的最安全的用法
    // 对于ConvertStringToBSTR不需要

posted on 2006-01-28 10:05  痛并快乐着  阅读(7507)  评论(4编辑  收藏  举报

导航