Clarification:

Why does the fuction test that aResult is NOT equal to NULL? If the result is a non-NULL value, wouldn't this mean that aResult has accessed an interface?

Somewhat confused.

Incidently, the XPCOM reference implementation of QueryInterface at http://www.mozilla.org/projects/xpcom/QI.html seems to actually check this. From what I can see, aIID is tested via the equals function, and if it can't be found they place the zero into a temporary nsISupports pointer called foundInterface, if it is found then the address of the interface is placed into this variable. They then test the ptr with the following:

    nsresult status;
    if ( !foundInterface )
      status = NS_NOINTERFACE;
    else
      {
        NS_ADDREF(foundInterface);
        status = NS_OK;
      }

Anyway, I hope that somebody can point out where I'm going wrong here! Incidently, COM seems like a pretty cool way of doing things, and XPCOM rocks!


rblah wrote:
Hi all,

I'm *extremely* new to XPCOM and COM concepts in general, but I'm confused by the weblock tutorial.

The weblock tutorial gives the following code as an example of how to
implement a QueryInterface function:

class Sample: public nsISupports
{
private:
    nsrefcnt mRefCnt;

public:
    Sample();
    virtual ~Sample();

    NS_IMETHOD QueryInterface(const nsIID &aIID, void **aResult);
    NS_IMETHOD_(nsrefcnt) AddRef(void);
    NS_IMETHOD_(nsrefcnt) Release(void);

};

Sample::Sample()
{
    mRefCnt(0);
}

Sample::~Sample()
{
}

NS_IMETHODIMP Sample::QueryInterface(const nsIID &aIID,
void **aResult)
{
if (aResult == NULL)
{
return NS_ERROR_NULL_POINTER;
}
*aResult = NULL;


    if (aIID.Equals(kISupportsIID))
    {
        *aResult = (void *) this;
    }

if (aResult != NULL)
{
return NS_ERROR_NO_INTERFACE;
}
AddRef();
return NS_OK;
}


NS_IMETHODIMP_(nsrefcnt) Sample::AddRef()
{
    return ++mRefCnt;
}

NS_IMETHODIMP_(nsrefcnt) Sample::Release()
{
    if (--mRefCnt == 0)
    {
        delete this;
        return 0;
    }

    return mRefCnt;
}

I'm a bit confused about the code in QueryInterface:

    if (aResult != NULL)
    {
        return NS_ERROR_NO_INTERFACE;
    }


Shouldn't aResult contain a reference to an interface? Why isn't this code something like "if (aResult == NULL)" ??



_______________________________________________ Mozilla-xpcom mailing list [EMAIL PROTECTED] http://mail.mozilla.org/listinfo/mozilla-xpcom

Reply via email to