Re: Behavior of out parameters and dependencies

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Alexander Nickolov (agnickolov_at_mvps.org)
Date: 01/20/05


Date: Thu, 20 Jan 2005 12:07:31 -0800

This is one of the finer points of marshaling. You need the [local]
and [call_as] atributes here:

[pointer_default(unique), ...]
interface IFoo : IUnknown
{
    [local]
    HRESULT GetData( DWORD* pdwSize, BYTE** ppBuffer );
    [call_as(GetData)]
    HRESULT RemGetData( [out]DWORD* pdwSize, [out, size_is(
,*pdwSize)]BYTE** ppBuffer );
};

Then you need to write a small C file to link with the rest of the
proxy/stub code that does the conversion from the local method
into the remote method at the proxy, and back at the stub. Your
clients always call the local method, and that's what you implement
at the server. However, it's always the remote method that travels
on the wire. Of course, you need to modify the proxy/stub DLL
project to compile and link your file as well.

HRESULT WINAPI IFoo_GetData_Proxy(IFoo* pThis, DWORD* pdwSize, BYTE**
ppBuffer)
{
    if (IsBadWritePtr(pdwSize, sizeof(DWORD*)) || IsBadWritePtr(ppBuffer,
sizeof(BYTE**))) {
        return S_FALSE;
    }
    return IFoo_RemGetData_Proxy(pThis, pdwSize, ppBuffer);
}

HRESULT WINAPI IFoo_RemGetData_Stub(IFoo* pThis, DWORD* pdwSize, BYTE**
ppBuffer)
{
    return pThis->lpVtbl->GetData(pThis, pdwSize, ppBuffer);
}

Note the docs for [call_as] is wrong on numerous points. The
needed prototypes are declared in the header file produced
by MIDL.

-- 
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@mvps.org
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================
"Laurents C. R. Meyer" <Laurents C. R. Meyer@discussions.microsoft.com> 
wrote in message news:CFE5270F-EAC9-433A-A95F-13C4C7ABBC70@microsoft.com...
> Hoi!
> I am currently thinking about an interesting exception in the COM usage 
> and
> would like to hear if somebody knows about the behavior.
>
> Let's say there is Some Interface called IFoo. This interface offers one
> method called GetData. Because the caller does not know how large the 
> buffer
> has to be, it is allocated by GetData using CoTaskMemAlloc. The user has 
> the
> possibility to omit a parameter, which results in an return code of 
> S_FALSE.
> The declaration of the interface would look something like this:
>
> [pointer_default(unique), ...]
> interface IFoo : IUnknown
> {
>    HRESULT GetData( [out]DWORD* pdwSize, [out, size_is( ,*pdwSize)]BYTE**
> ppBuffer );
> };
>
> Now, on a usual call like:
>
> DWORD dwSize;
> BYTE* pData;
> GetData( &dwSize, &pData );
>
> everything will be alright.
> But what is the behavior, if I call like this:
>
> BYTE* pData;
> GetData( NULL, &pData );
>
> Even if GetData returns immediately after a paremeter check (allocates no
> memory for ppBuffer), COM would try to dereference pdwSize (i.e. 
> *pdwSize),
> which would result in an error.
> Am I right, or is COM checking the pointer (pdwSize) first if it is zero 
> of
> not and in case it is zero assumes the dereferenced pointer (*pdwSize) is
> zero too?
>
> In case COM returns an error, is it one which can be handled or is COM
> terminating the application?
> Of course this is only a point when using a proxy.
>
> Can anybody help me? Thanks in advance.
>
> Laurents C. R. Meyer 


Relevant Pages

  • Re: Behavior of out parameters and dependencies
    ... In most cases it will be ok to just declare the pointers as ref, ... >> Let's say there is Some Interface called IFoo. ... it is allocated by GetData using CoTaskMemAlloc. ...
    (microsoft.public.win32.programmer.ole)
  • Re: Behavior of out parameters and dependencies
    ... "Laurents C. R. Meyer" ... return IFoo_RemGetData_Proxy(pThis, pdwSize, ppBuffer); ... >>> Let's say there is Some Interface called IFoo. ... COM would try to dereference pdwSize (i.e. ...
    (microsoft.public.win32.programmer.ole)
  • Re: About MIDL and IDispatch
    ... An object implementing this interface only needs to ... This has nothing to do with IDispatch at all. ... // interface IFoo: IDispatch ... method (the DISPID passed to Invoke determines which one). ...
    (microsoft.public.vc.atl)
  • Re: Cannt serialize interface
    ... > i have one interface and a class inherit the interface like below: ... > When i create a web service to delive class FooContainer to client, ... Can not serialize interface IFoo. ...
    (microsoft.public.dotnet.framework)
  • Cannt serialize interface
    ... i have one interface and a class inherit the interface like below: ... Can not serialize interface IFoo. ... public interface IFoo:ISerializable. ...
    (microsoft.public.dotnet.framework)