bstr

Pass BSTR from C++ DLL function to VB6 application

两盒软妹~` 提交于 2019-12-01 10:33:48
问题 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

Which is better code for converting BSTR parameters to ANSI in C/C++?

会有一股神秘感。 提交于 2019-12-01 08:57:54
So far I've discovered I can convert incoming BSTRs to ANSI in two (of many?) ways, and I'm curious to know whether one is "better" than the other with respect to speed / efficiency etc. The way I've been using for a while is use the USES_CONVERSION and W2A macros, e.g. BSTR __stdcall F(BSTR p1, BSTR p2 ) { USES_CONVERSION; LPSTR sNum1 = W2A( p1 ); LPSTR sNum2 = W2A( p2 ); Recently, however, I came across another technique: BSTR __stdcall F(BSTR p1, BSTR p2 ) { long amt = wcstombs( NULL, p1, 1 ); sNum1 = (char *) malloc( amt ); wcstombs( sNum1, p1, amt ); *(sNum1 + amt) = '\0'; amt = wcstombs(

C++: Convert wchar_t* to BSTR?

泪湿孤枕 提交于 2019-12-01 06:02:33
I'm trying to convert a wchar_t * to BSTR . #include <iostream> #include <atlstr.h> using namespace std; int main() { wchar_t* pwsz = L"foo"; BSTR bstr(pwsz); cout << SysStringLen(bstr) << endl; getchar(); } This prints 0 , which is less than what I'd hoped. What is the correct way to do this conversion? You need to use SysAllocString (and then SysFreeString). BSTR bstr = SysAllocString(pwsz); // ... SysFreeString(bstr); A BSTR is a managed string with the characters of the string prefixed by their length. SysAllocString allocates the correct amount of storage and set up the length and

C++: Convert wchar_t* to BSTR?

别说谁变了你拦得住时间么 提交于 2019-12-01 03:55:14
问题 I'm trying to convert a wchar_t * to BSTR . #include <iostream> #include <atlstr.h> using namespace std; int main() { wchar_t* pwsz = L"foo"; BSTR bstr(pwsz); cout << SysStringLen(bstr) << endl; getchar(); } This prints 0 , which is less than what I'd hoped. What is the correct way to do this conversion? 回答1: You need to use SysAllocString (and then SysFreeString). BSTR bstr = SysAllocString(pwsz); // ... SysFreeString(bstr); A BSTR is a managed string with the characters of the string

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

六眼飞鱼酱① 提交于 2019-12-01 03:41:04
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 management: ATL::CComBSTR converted; converted.Attach( sourceString.AllocSysString() ); //simply attaches to

Who owns returned BSTR?

我的未来我决定 提交于 2019-11-30 20:13:56
Suppose a method from a COM interface returns BSTR value. Am I right in my opinion that I must free it? The code example at http://msdn.microsoft.com/en-us/library/aa365382(VS.85).aspx does not do that. Who's wrong? The MSDN sample is wrong. The caller frees out and in/out bstrs. If it's in/out you have to pass in null or a valid bstr. If it's out only, it doesn't have to be initialized. It's not super clear from msdn's com allocation rules , but the client stub allocates the memory on out values so from the caller's point of view the server did. Who else can free it but the caller? Tony is

Who owns returned BSTR?

前提是你 提交于 2019-11-30 04:23:04
问题 Suppose a method from a COM interface returns BSTR value. Am I right in my opinion that I must free it? The code example at http://msdn.microsoft.com/en-us/library/aa365382(VS.85).aspx does not do that. Who's wrong? 回答1: The MSDN sample is wrong. The caller frees out and in/out bstrs. If it's in/out you have to pass in null or a valid bstr. If it's out only, it doesn't have to be initialized. It's not super clear from msdn's com allocation rules, but the client stub allocates the memory on

BSTR's and VARIANT's under… mac os x

亡梦爱人 提交于 2019-11-29 12:49:41
Under mac os x I have office 2011 and its excel and VBA, and I have gcc-5.3.0's g++. I played a lot to passing arrays (of numerical built-in types) from VBA to the dylib (extension of dll's on mac os x) and updating them and sending them back to VBA, see for instance : passing c/c++ dylib function taking pointer to VBA on mac Now, I would like to do the same with strings, and first with just one string, not an array. I want to receive the string from VBA, modify it in C++, and send it back, updated, to VBA. The code on the C++ side is : #include <stdlib.h> #include <ctype.h> //for toupper

Convert BSTR to char*

谁说我不能喝 提交于 2019-11-29 05:55:05
Anyone know how to convert BSTR to char* ? Update: I tried to do this, but don't know if it is right or wrong. char *p= _com_util::ConvertBSTRToString(URL->bstrVal); strcpy(testDest,p ); dirkgently Your code is okay. ConvertBSTRToString does just that. As for the strcpy , testDest needs to be large enough to hold the string pointed to by p . Note that since ConvertBSTRToString allocates a new string you will need to free it somewhere down the line. Once you are done make sure you do: delete[] p; A couple of caveats though (as you can see from BSTR documentation on MSDN ): On Microsoft Windows,

BSTR's and VARIANT's under… mac os x

孤街浪徒 提交于 2019-11-28 06:33:38
问题 Under mac os x I have office 2011 and its excel and VBA, and I have gcc-5.3.0's g++. I played a lot to passing arrays (of numerical built-in types) from VBA to the dylib (extension of dll's on mac os x) and updating them and sending them back to VBA, see for instance : passing c/c++ dylib function taking pointer to VBA on mac Now, I would like to do the same with strings, and first with just one string, not an array. I want to receive the string from VBA, modify it in C++, and send it back,