开发者

Copying a BSTR into a char[1024]?

开发者 https://www.devze.com 2023-01-19 04:37 出处:网络
I am working on a web browser with c++ using IWebBrowser2. Declared on the t开发者_运维问答op, outside any scope, I have this:

I am working on a web browser with c++ using IWebBrowser2. Declared on the t开发者_运维问答op, outside any scope, I have this:

static char buf[1024];

In the documentcomplete event, I set the BSTR with:

CComBSTR bstrHTMLText;
X->get_outerHTML(&bstrHTMLText);

What I want to do is to copy bstrHTMLText value into buf (if bstrHTMLText length is > 1024 just fill buf and ignore the rest). every time documentcomplete fires.

How can I do this?


A BSTR is secretly a pointer to a Unicode string, with a hidden length prefix sitting in front of the string data. So, you can just cast it to wchar_t* and then convert that wide-character string to an ANSI string, e.g. using WideCharToMultiByte:

WideCharToMultiByte(CP_UTF8,
                    0, 
                    bstrHTMLText.m_str,
                    SysStringLen(bstrHTMLText.m_str),
                    buf,
                    sizeof(buf),
                    NULL,
                    NULL);
0

精彩评论

暂无评论...
验证码 换一张
取 消