Re: Returning IStream as IDisPatch causes crash in VB?
From: Kim Gräsman (kim_at_mvps.org)
Date: 03/20/04
- Next message: Bruce Stemplewski: "Re: Returning IStream as IDisPatch causes crash in VB?"
- Previous message: Jean GENG: "Re: how can ATL ActiveX control driver a script progress bar by event in real-time"
- In reply to: Bruce Stemplewski: "Returning IStream as IDisPatch causes crash in VB?"
- Next in thread: Bruce Stemplewski: "Re: Returning IStream as IDisPatch causes crash in VB?"
- Reply: Bruce Stemplewski: "Re: Returning IStream as IDisPatch causes crash in VB?"
- Reply: Bruce Stemplewski: "Re: Returning IStream as IDisPatch causes crash in VB?"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 20 Mar 2004 12:35:00 +0100
Hi Bruce,
> STDMETHODIMP CMyObject::get_MyStream(IDispatch **ppVal)
> {
>
> HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE,1000);
> LPVOID pvData = GlobalLock(hGlobal);
> LPSTREAM pStream = NULL;
> HRESULT hr = CreateStreamOnHGlobal(hGlobal,TRUE,&pStream);
> *ppVal = (IDispatch*)pStream;
> (*ppVal)->AddRef();
>
> return S_OK;
> }
Any way you look at it, a pointer to IDispatch is not the same as a pointer
to IStream, which the following line is trying to convince the compiler:
> *ppVal = (IDispatch*)pStream;
The crash is happening because VB is trying to locate some method, either in
IDispatch or IUnknown, and call it, but the signature available in the
IStream interface doesn't match what it's expecting, so the stack is
trashed, and the application blows up.
Also, the allocation of memory is not necessary - if you pass NULL to
CreateStreamFromHGlobal, it creates a growable stream behind-the-scenes:
STDMETHODIMP CMyObject::get_MyStream(IDispatch **ppVal)
{
CComPtr<IStream> spStream;
HRESULT hr = CreateStreamOnHGlobal(NULL, TRUE, &spStream);
CComPtr<IDispatch> spRetVal;
hr = ConstructSomeObjectWrappingTheStream(spStream, &spRetVal);
*ppVal = spRetVal.Detach();
return S_OK;
}
Now what you need to do, is to write some object wrapping the IStream
interface, and exposing it as an IDispatch interface, with
automation-compatible parameter types.
Casting is never the right thing to do.
-- Best regards, Kim Gräsman
- Next message: Bruce Stemplewski: "Re: Returning IStream as IDisPatch causes crash in VB?"
- Previous message: Jean GENG: "Re: how can ATL ActiveX control driver a script progress bar by event in real-time"
- In reply to: Bruce Stemplewski: "Returning IStream as IDisPatch causes crash in VB?"
- Next in thread: Bruce Stemplewski: "Re: Returning IStream as IDisPatch causes crash in VB?"
- Reply: Bruce Stemplewski: "Re: Returning IStream as IDisPatch causes crash in VB?"
- Reply: Bruce Stemplewski: "Re: Returning IStream as IDisPatch causes crash in VB?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|