|
Hi,
I'm creating dll using VS2005 ATL project. I'm using third party SDK API's which extracts data into a *string variable. Now I'm facing problem with converting the *string to _bstr_t and *BSTR.
Please guide me whether is it possible to convert *string to _bstr_t and *BSTR, if so how to do it. If possible with a sample piece of code.
thanking you,
Arun
|
|
|
|
|
Hello agarunk,
By "*string", do you mean a pointer to a STL string ?
By "*BSTR", do you mean a pointer to a BSTR ?
- Bio.
|
|
|
|
|
Hi Bio,
Its "std::string* ", i.e., pointer to std string.
and yes "BSTR* " is pointer to a BSTR.
-Arun
|
|
|
|
|
Both the _bstr_t wrapper class and BSTR expect the text to be UNICODE, rather than ANSI or MBCS. While _bstr_t can convert from LPCSTR, BSTR on it's own can't.
You will need to use the c_str() member function of the std::string to get your LPCSTR in the first place (unlike MFC/ATL/WTL CString class, there's no LPCTSTR operator). Like I say, you can then use this pointer with something like A2W or MultiByteToWideChar.
Steve S
Developer for hire
|
|
|
|
|
Hi Steve,
Below is the implementation which I'm using now....
BSTR* TargetBSTR ;<br />
string *SourceStr=new string;<br />
string StrBuff;<br />
const char *CharBuff;<br />
<br />
StrBuff=*SourceStr;<br />
CharBuff=StrBuff.c_str();<br />
*TargetBSTR = _com_util::ConvertStringToBSTR(CharBuff);
Is this what you are asking me to do???
It is working fine for me for time being... I would like to know if I'm doing any mistake.
Is there any issues with it???
-Arun
|
|
|
|
|
agarunk wrote: BSTR* TargetBSTR ;
string *SourceStr=new string;
string StrBuff;
const char *CharBuff;
StrBuff=*SourceStr;
CharBuff=StrBuff.c_str();
*TargetBSTR = _com_util::ConvertStringToBSTR(CharBuff);
That is far more code than you need to do for this. See my response below.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Steve S wrote: Both the _bstr_t wrapper class and BSTR expect the text to be UNICODE, rather than ANSI or MBCS. While _bstr_t can convert from LPCSTR, BSTR on it's own can't.
This isn't completely accurate. _bstr_t has a constructor that takes a const char* . It will do the conversion for you.
Basically, if you need to convert std::string* to a _bstr_t* (or even to a BSTR* ), here is a simple way to do it:
std::string* pStlString = new string("my stuff");
_bstr_t* pBstrWrapper = new _bstr_t(pStlString->c_str());
BSTR* pBstr = pBstrWrapper->GetAddress();
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
I sit corrected
I confess that these days I very rarely use ANSI/MBCS, since I no longer do stuff for Win9x/ME platforms. Lucky me, my end-customers all use W2K or later...
Steve S
Developer for hire
|
|
|
|
|
Since I'm in the Linux world at my new job, I'm a bit rusty myself. I do find that using the T-functions (along with classes that can use both char* and wchar_t*) alleviates much of those headaches.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
|
Hi All,
I want to return a Class pointer from ATL Dll.Currently I am trying method using IDispatch * but giving problem, return pointer is invalid . Please give me a solution for this.
Thanks in Advance.
The secret of life is not enjoyment
but education through experience.
- Swami Vivekananda.
-- modified at 8:30 Monday 24th July, 2006
|
|
|
|
|
You don't return class pointers in COM: It violates the whole concept of COM. COM components are meant to be (programming) language neutral so you can't be returning classes from COM objects: What good is a C++ class to VB, for example? Can you be more specific with what (exactly) you're trying to achieve?
|
|
|
|
|
Thanks For the Reply.
Details : I have used one class in COM Interface. This class gets manipulated in the com method. Further I need the class pointer so that I can access the values of that class which are modified by the COM. I want the pointer in the function from where I invoked the COM method.
The secret of life is not enjoyment
but education through experience.
- Swami Vivekananda.
|
|
|
|
|
Is there anything preventing you from creating and exposing another COM Interface that returns the values you are interested in?
|
|
|
|
|
|
No you don't. A COM interface should provide all the functionality, which may well include accessors for properties.
If you start handing out raw C++ class pointers, it is no longer COM, no longer marshallable, remotable, or usable by anything other than a VC++ client, and possibly not even that if you later switch to a different version of VC++.
An interface is not a class. A class may implement one or more interfaces (typically IUnknown, sometimes IDispatch, and one or more interfaces derived from them), but an interface is not a pointer to an object, nor should it be treated as one. This means no access via an interface to member data, and no casting the interface to something else directly.
You need to read (or re-read) Don Box's "Essential COM", plus there is an MSDN article which essentially describes the process of going from a C++ class to COM interfaces in design terms (here[^])
Steve S
Developer for hire
|
|
|
|
|
Hi All,
I am writing a one component in which, I have to register the callback method in the component when client Advise for connection. How should i do this?
Thanks in Advance.
Amar.
|
|
|
|
|
Check out the documentation for IConnectionPointImpl and IConnectionPointContainerImpl.
Your component needs to implement IConnectionPoint, and the Advise method will typically store the provided sink in some container (like an array, or a map, or a list...)
When something happens in the component that triggers an event (such as changing a property, or a timer firing, or...) the component iterates through all the entries in that container, and calls the appropriate method(s) passing any parameters as required.
Steve S
Developer for hire
|
|
|
|
|
there is an m_vec member which contains a list of clients which the sinked the event.
Sudeesh
|
|
|
|
|
Dear Gurus,
You may not be aware that I have lost my job as our company has given us lay off.
I am really expecting a job in MFC, VC++.
I have given 2-3 technical interviews but it has made clear that interviews need special attention and preparation ,
I would be highly thankful , if someone provides me the compilation of interview questions on VC++, MFC, C++, ATL COM, Threading. If with answers then it will be a faster preparation for me. (Sorry being lazy here, but situation is a bit tedious with me right now).
ANy links also will help a lot.
If you want to send me few things, then email address is
rpathak03011978@yahoo.com
Looking forward some help from Codeproject.
Thanks ,
pathak
|
|
|
|
|
template<class TYPE, class ARG_TYPE>
class CBanana : public CObject
{
protected:
struct CApple
{
int x;
int y;
};
int GetInt(int);
CApple* GetApple(CApple*);
};
template<class TYPE, class ARG_TYPE>
int CBanana<TYPE, ARG_TYPE>::GetInt(int i)
{
return i;
}
template<class TYPE, class ARG_TYPE>
CBanana<TYPE, ARG_TYPE>::CApple*
CBanana<TYPE, ARG_TYPE>::GetApple(CBanana::CApple* pApple)
{
return pApple;
}
|
|
|
|
|
You don't use the template parameters anywhere....What's the point of using templates? There are times when this is done intentionally but this doesn’t like any case I’ve seen.
Steve
|
|
|
|
|
I was trying to illustrate the compile error as simply as possible. This is not the actual code.
|
|
|
|
|
Try this:
template<typename TYPE, typename ARG_TYPE>
typename CBanana<TYPE, ARG_TYPE>::CApple*
CBanana<TYPE, ARG_TYPE>::GetApple(typename CBananaa<TYPE, ARG_TYPE>::CApple* pApple)
{
return pApple;
}</code>
I believe you need the underlinded typename s in standard C++ but some non-standard complient compilers may not like them.
Steve
|
|
|
|
|
That works thanks. I'm still a bit concerned that this will come back
and bite me. The typename is used to resolve ambiguity but I don't see
any ambiguity and afxtempl.h manages without it. Is there something
more fundamentally wrong ?
Nvat.
|
|
|
|