Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

BSTR Pointer ...

Status
Not open for further replies.

SteveBrett

Programmer
Apr 24, 2000
107
MT
I have a com wrapper that accepts a pointer to a BSTR as shown below in the code snippet:

conn.SendMsg((LPCSTR)message);
BSTR * recvd = 0;
conn.RecvMsg(recvd);

i need to get the value of recvd but when i run the program it is never naything but 0.

Is this the right way to call a function with a bstr out parameter ?

The dll that xontains the conn class works ok when used with c# via interop so i'm pretty sure it's working ok. i just can't seem to get it to work in c++

many thanks

 
Try this. It's worth a try.

Code:
  conn.SendMsg((LPCSTR)message);
  BSTR* recvd;
  conn.RecvMsg(recvd);

Remove the =0.


HyperEngineer
If it ain't broke, it probably needs improvement.
 
i did try that but get this error:

Warning: attempt to call Invoke with NULL m_lpDispatch!
Warning: attempt to call Invoke with NULL m_lpDispatch!
Run-Time Check Failure #3 - The variable 'recvd' is being used without being defined.

i think the warning as well is a problem ...

any ideas ?
 
You should ALWAYS initialize pointers to NULL!

What does the definition of conn.RecvMsg() look like?
If it doesn't take a reference to a pointer (or pointer to a pointer) then there's no way the value of recvd could be changed.
 
the definition looks like this:

STDMETHODIMP CSSLConnection::RecvMsg(BSTR* bstrResponse)

and returns:

*bstrResponse = temp.Detach(); // return message

i think with the run-rime check failure i may have more than one comm dll registered - am investigating at the moment - still don't know how to call it though ....
 
okay - only one version of the dll exists now but am still getting the following errors when stepping through

Warning: attempt to call Invoke with NULL m_lpDispatch!
Warning: attempt to call Invoke with NULL m_lpDispatch!
Warning: attempt to call Invoke with NULL m_lpDispatch!
 
I can't be sure without seeing the documentation for that conn object, but I'm pretty sure you need to allocate the memory yourself and pass a pointer to that memory to RecvMsg().
Something like this:
Code:
conn.SendMsg( (LPCSTR)message );
const size_t MAX_STR = 1024;
BSTR recvd[ MAX_STR ] = "\0";
conn.RecvMsg( recvd );
// Now print the string in recvd...
Usually if a function allocates memory for you it will take a pointer to a pointer. i.e. BSTR**
 
fixed it with this:

conn.CreateDispatch("XXXX.XXXX.X");

this was the reason that I got the warnings (Warning: attempt to call Invoke with NULL m_lpDispatch!). CreateDispatch() stops this.

CComBSTR result;
conn.RecvMsg(&result);

RecvMsg accepted a pointer to a BSTR, amended it and then returned the result via the same var. Thanks for all the comments and suggestions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top