bstr

Is there a way to write a BSTR literal?

China☆狼群 提交于 2019-12-05 18:08:58
When calling a function that expects a BSTR it'd be nice to be able to write something like: iFoo->function( bs"HELLO" ); However the only workaround I'm aware of is to use a wrapper that calls SysAllocString etc., e.g.: iFoo->function( WideString(L"HELLO").c_bstr() ); which is kind of ugly. Is there actually such an option to create a BSTR literal? Motivation: easier-to-read code, and faster runtime performance by avoiding an allocation and deallocation. Clarification: I am only talking about situations where the caller (i.e. us) has ownership of the BSTR, for example: calling a function that

Shall we treat BSTR type in COM as value or reference?

泄露秘密 提交于 2019-12-05 10:04:49
From book ATL Internals , I knew BSTR is different from OLECHAR*, and there are CComBSTR and CString for BSTR. According MSDN Allocating and Releasing Memory for a BSTR , I knew memory management responsibility for caller/callee. Take this line from MSDN, HRESULT CMyWebBrowser::put_StatusText(BSTR bstr) I still do not know how to handle bstr properly in my implementation. Since I still have a basic question for BSTR -- should we treat bstr as a value (like int) or as a reference (like int*), at least on COM interface boundary. I want to convert BSTR as soon as possible to CString/CComBSTR in

How do you efficiently copy BSTR to wchar_t[]?

北城以北 提交于 2019-12-05 08:31:55
I have a BSTR object that I would like to convert to copy to a wchar__t object. The tricky thing is the length of the BSTR object could be anywhere from a few kilobytes to a few hundred kilobytes. Is there an efficient way of copying the data across? I know I could just declare a wchar_t array and alway allocate the maximum possible data it would ever need to hold. However, this would mean allocating hundreds of kilobytes of data for something that potentially might only require a few kilobytes. Any suggestions? Euro Micelli First, you might not actually have to do anything at all, if all you

Why are C#/.Net strings length-prefixed and null terminated?

浪子不回头ぞ 提交于 2019-12-04 16:56:56
问题 After reading What's the rationale for null terminated strings? and some similar questions I have found that in C#/.Net strings are, internally, both length-prefixed and null terminated like in BSTR Data Type. What is the reason strings are both length-prefixed and null terminated instead of eg. only length-prefixed? 回答1: Length prefixed so that computing length is O(1) . Null terminated to make marshaling to unmanaged blazing fast (unmanaged likely expects null-terminated strings). 回答2: Here

How to best convert CString to BSTR to pass it as an “in” parameter into a COM method?

你。 提交于 2019-12-04 00:53:42
问题 I need to convert a CString instance into a properly allocated BSTR and pass that BSTR into a COM method. To have code that compiles and works indentically for both ANSI and Unicode I use CString::AllocSysString() to convert whatever format CString to a Unicode BSTR. Since noone owns the returned BSTR I need to take care of it and release it after the call is done in the most exception-safe manner posible and with as little code as possible. Currently I use ATL::CComBSTR for lifetime

VBScript “Type Mismatch” issue with “[in, out] BSTR * ” parameter

谁说胖子不能爱 提交于 2019-12-02 03:31:10
问题 I'm working with third-party COM object that has some of its methods passing values back as BSTR pointer. Since VBscript supports only Variant type attempts to use in a way like Object.Method(sMyString) reasonably end with "Type mismatch" error. I suspect this error is generated by the COM object itself rather then VBscript interpreter since the object gets string instead of pointer. I tried to workaround it defining array of strings but it's still the same error. So I was wondering if

How to properly call IDispatch::Invoke with a required BSTR* parameter

六眼飞鱼酱① 提交于 2019-12-02 02:49:30
问题 There are many examples of how to call IDispatch::Invoke with a BSTR* parameter. I have this working with many other "SomeType*" parameter but no matter what I try, I either get HRESULT of Type Mismatch, E_OUTOFMEMORY or an access violation. It seems to me I am doing something wrong with memory but I am following the different examples I have found... As a side note, the final "[out] UINT puArgErr" argument is never filled with the argument index that is causing the problem. However, I know

VBScript “Type Mismatch” issue with “[in, out] BSTR * ” parameter

烂漫一生 提交于 2019-12-02 00:29:40
I'm working with third-party COM object that has some of its methods passing values back as BSTR pointer. Since VBscript supports only Variant type attempts to use in a way like Object.Method(sMyString) reasonably end with "Type mismatch" error. I suspect this error is generated by the COM object itself rather then VBscript interpreter since the object gets string instead of pointer. I tried to workaround it defining array of strings but it's still the same error. So I was wondering if someone had similar problem and what workarounds were utilized. Just to emphasize. I DO NOT have control over

How to convert _bstr_t to CString

本小妞迷上赌 提交于 2019-12-01 14:13:42
问题 I have a _bstr_t variable bstrErr and I am having a CString variable csError . How do I set the value which come in bstrErr to csError ? 回答1: Is it not possible just to cast it: _bstr_t b("Steve"); CString cs; cs = (LPCTSTR) b; I think this should work when the project is Unicode. 回答2: CString has contructors and assignment operators for both LPCSTR and LPCWSTR, so there is never a need to call WideCharToMultiByte, and you can't get the casting wrong in unicode or non-unicode mode. You can

Pass BSTR from C++ DLL function to VB6 application

余生颓废 提交于 2019-12-01 12:53:00
I have this code in my VB6 app: Private Declare Function FileGetParentFolder Lib "Z-FileIO.dll" _ (ByVal path As String) As String Output.AddItem FileGetParentFolder(FileText.Text) Output is a list, FileText is a text field containing a file path. My C++ DLL contains this function: extern "C" BSTR ZFILEIO_API FileGetParentFolder(Path p) { try { return SysAllocString(boost::filesystem::path(p).parent_path().c_str()); } catch (...) { return SysAllocString(L""); } } where Path is typedef'd as LPCSTR . The argument comes into my DLL perfectly, but whatever I try to pass back, the VB6 app shows