How to pass a string into COM
-
I'm a begineer in COM. Please anyone help me. I have to pass a String value(CString) from an edit box into COM Object(LPCTSTR). ThankYou
You need to do something like this where your CString is called cString, and your com function is pInt->Fuction():
BSTR bstr;
bstr = ::SysAllocString(cString);
pInt->Function(bstr);
::SysFreeString(bstr);
Build a man a fire, and he will be warm for a day
Light a man on fire, and he will be warm for the rest of his life! -
You need to do something like this where your CString is called cString, and your com function is pInt->Fuction():
BSTR bstr;
bstr = ::SysAllocString(cString);
pInt->Function(bstr);
::SysFreeString(bstr);
Build a man a fire, and he will be warm for a day
Light a man on fire, and he will be warm for the rest of his life!Just an observation: If the requirement is only to send the contents of the CString to the server (as in this case), then the solution is okay. If the server changes it and the client needs it back, a BSTR * will be required. The IDL should have [in] BSTR bstr in the first case. For the second type it should have [in, out] BSTR *pbstr With best regards, Sayan Email:sayanmukherjee@indiatimes.com
-
You need to do something like this where your CString is called cString, and your com function is pInt->Fuction():
BSTR bstr;
bstr = ::SysAllocString(cString);
pInt->Function(bstr);
::SysFreeString(bstr);
Build a man a fire, and he will be warm for a day
Light a man on fire, and he will be warm for the rest of his life! -
I'm a begineer in COM. Please anyone help me. I have to pass a String value(CString) from an edit box into COM Object(LPCTSTR). ThankYou
-
Thanks for you Help. I did as you explained. But it's showing an error. error C2664: 'SysAllocString' : cannot convert parameter 1 from 'class CString' to 'const unsigned short *' Please explain
There's a few things you need to take into account. Your COM object's interface that you're trying to reach... did you implement it using IUnknown or IDispatch? IUnknown will let you more or less pass any kind of data you want including good 'ol CHAR and CHAR* type stuff. IDispatch, which is required if your component is or ever will be used in Visual Basic or any scripting language (JavaScript or VB Script), is much more limited. From my experience and I don't doubt some will disagree, IDispatch is "safer" in most cases regardless of whether your object is needed in VB or not (particularly if you have to support Windows 95/98). IDispatch, however, greatly narrows down your options to a degree on types. Sure, there's ways around this restriction, but basically with IDispatch you have some basic integer types... after that you really have to use BSTR's and VARIANT's. These are a real pain in the... keister, particularly in C++ but you'll have better results. The solution using SysAllocString mentioned above would be the correct usage if you're using IDispatch and passing a BSTR. I'm gathering from the type you mentioned that you chose IUnknown. In this case, it's really quite simple, CString strData("Hello World"); // Load your smart pointer to your object IMyInterfacePtr pInterface; HRESULT hr = pInterface.CreateInstance(__uuidof(MyCoClass)); if (SUCCEEDED(hr)) { hr = pInterface->YourFunctionCall(strData.GetBuffer(strData.GetLength())); if (SUCCEEDED(hr)) { } else { } strData.ReleaseBuffer(); }