Different instances of COM object 
Author Message
 Different instances of COM object

Hi, I have this stupid problem, may be I don't understand something.

I have an object that is doing something with the HTML document open
in IE. All keyboard hooks and other stuff working fine. When I try to
add toolbar button that should call the very same object to do the
very same thing with the document, it fails. To create toolbar button
I used described mechanism of adding Browser Extensions (
http://www.*-*-*.com/
). My object supports both IObjectWithSite and IOleCommandTarget and
Exec() is being called when I click the button, but the
IObjectWithSite.m_spWebBrowser2 is not initialized at this point.
Actually I found out that when I click the button SetSite () is
called, but Exec () then called with different value of 'this'
pointer. It looks like there was 2 objects created and SetSite was
called for the first one and never for another.

Am I doing something wrong with declaration or instantiation?



Thu, 28 Apr 2005 10:11:07 GMT  
 Different instances of COM object
I'd say there's something wrong with your interface map which causes
your 'this' pointer to get through incorrectly. Show the declaration of
your class.
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken


Quote:
> Hi, I have this stupid problem, may be I don't understand something.

> I have an object that is doing something with the HTML document open
> in IE. All keyboard hooks and other stuff working fine. When I try to
> add toolbar button that should call the very same object to do the
> very same thing with the document, it fails. To create toolbar button
> I used described mechanism of adding Browser Extensions (

http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/e...
Quote:
> ). My object supports both IObjectWithSite and IOleCommandTarget and
> Exec() is being called when I click the button, but the
> IObjectWithSite.m_spWebBrowser2 is not initialized at this point.
> Actually I found out that when I click the button SetSite () is
> called, but Exec () then called with different value of 'this'
> pointer. It looks like there was 2 objects created and SetSite was
> called for the first one and never for another.

> Am I doing something wrong with declaration or instantiation?



Sat, 30 Apr 2005 07:08:43 GMT  
 Different instances of COM object

Quote:

> I'd say there's something wrong with your interface map which causes
> your 'this' pointer to get through incorrectly. Show the declaration of
> your class.

Here's the declaration:

---------------------------
/////////////////////////////////////////////////////////////////////////////
// CViewSource
class ATL_NO_VTABLE CViewSource :
        public CComObjectRootEx<CComSingleThreadModel>,
        public CComCoClass<CViewSource, &CLSID_ViewSource>,
        public IObjectWithSiteImpl<CViewSource>,
        public IDispatchImpl<IViewSource, &IID_IViewSource,
&LIBID_HTMLEDITLib>,
        public IOleCommandTarget // derive from IOleCommandTarget
{
public:
        CViewSource() {
                m_bDocumentCompleted = false;
                m_bIsBanListRead = false;
        }

static _ATL_REGMAP_ENTRY RegEntries[];
// Default implementation does not pass _ATL_REGMAP_ENTRY array
// DECLARE_REGISTRY_RESOURCEID(IDR_VIEWSOURCE)
        static HRESULT WINAPI UpdateRegistry(BOOL bRegister)
        {
                return _Module.UpdateRegistryFromResource ( IDR_VIEWSOURCE,
bRegister, RegEntries );
        }

DECLARE_CLASSFACTORY_SINGLETON(CViewSource) // Do not want multiple
objects for each command execution
DECLARE_NOT_AGGREGATABLE(CViewSource)
DECLARE_PROTECT_FINAL_CONSTRUCT()

BEGIN_COM_MAP(CViewSource)
        COM_INTERFACE_ENTRY(IViewSource)
    COM_INTERFACE_ENTRY(IDispatch)
        COM_INTERFACE_ENTRY(IOleCommandTarget) // responds to QueryInterface
for IOleCommandTarget
        COM_INTERFACE_ENTRY(IObjectWithSite)
END_COM_MAP()

// IObjectWithSite
public:
        STDMETHOD(SetSite)(IUnknown *pUnkSite);

// IDispatch
public:    
        STDMETHOD(Invoke)(DISPID, REFIID, LCID, WORD, DISPPARAMS*,
                              VARIANT*, EXCEPINFO*, UINT*);

// IViewSource  
public:
        STDMETHOD(OnDocumentComplete)(BOOL bForceWindow=false);
        STDMETHOD(OnQuit)(void);

// IOleCommandTarget
public:
    STDMETHOD(QueryStatus)( const GUID *pguidCmdGroup, ULONG cCmds,
OLECMD prgCmds[], OLECMDTEXT *pCmdText);
        STDMETHOD(Exec)( const GUID *pguidCmdGroup, DWORD nCmdID, DWORD
nCmdexecopt, VARIANT *pvaIn, VARIANT *pvaOut);

private:
        STDMETHOD(Connect)(void);
        STDMETHOD(Disconnect)(void);
        STDMETHOD(GetDocumentContent)(BOOL addUnconditionally);
        STDMETHOD(RetrieveBrowserWindow)(void);
        static BOOL CALLBACK WndEnumProc(HWND, LPARAM);
        static LRESULT CALLBACK IEKeyboardProc(int, WPARAM, LPARAM);

private:
        CComQIPtr<IWebBrowser2, &IID_IWebBrowser2> m_spWebBrowser2;
    CComQIPtr<IConnectionPointContainer,
&IID_IConnectionPointContainer> m_spCPC;
        CComPtr<IHTMLElement> m_pBody;
        BOOL m_bDocumentCompleted;
        DWORD m_dwCookie;
        HWND m_hwndBrowser;
        RECT m_rcIE, m_rcHTML;
        BOOL m_bIsBanListRead;
        vector<string *> m_BanKeyNames;

Quote:
};

---------------------------

Is something wrong here?



Sun, 01 May 2005 09:18:52 GMT  
 Different instances of COM object
A number of comments.

Quote:
> DECLARE_CLASSFACTORY_SINGLETON(CViewSource)

Singletons don't work properly in in-proc servers. E.g., often multiple
instances of IE run on different STA threads inside a single
IEXPLORE.EXE process. In this case every IE instance will get a direct
pointer to the same instance of your class and your code will be
executed concurrently on multiple threads. I bet you are not prepared
for that. More importantly, you won't be able to find out which instance
you are called from.

Your assumption that a new instance is created each time a button is
pressed is wrong, and I guess this wrong assumption was your motivation
for putting DECLARE_CLASSFACTORY_SINGLETON in there. IE will create your
object the first time toolbar button is pressed, and will keep it alive
until IE instance itself is closed. Each IE instance will create its own
copy of your object, but that's what you actually want.

Can you show how you implement SetSite and Exec?

Quote:
> // IDispatch
> public:
> STDMETHOD(Invoke)(DISPID, REFIID, LCID, WORD, DISPPARAMS*,
>               VARIANT*, EXCEPINFO*, UINT*);

If you are using IDispatchImpl, why don't you need to implement
IDispatch::Invoke manually?

Quote:
> // IViewSource
> public:
> STDMETHOD(OnDocumentComplete)(BOOL bForceWindow=false);

This method is not automation-compatible. Does it really match your IDL
definition? What is the IDL definition for IViewSource?
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken


Quote:

> > I'd say there's something wrong with your interface map which causes
> > your 'this' pointer to get through incorrectly. Show the declaration
of
> > your class.

> Here's the declaration:

> ---------------------------

////////////////////////////////////////////////////////////////////////
/////
Quote:
> // CViewSource
> class ATL_NO_VTABLE CViewSource :
> public CComObjectRootEx<CComSingleThreadModel>,
> public CComCoClass<CViewSource, &CLSID_ViewSource>,
> public IObjectWithSiteImpl<CViewSource>,
> public IDispatchImpl<IViewSource, &IID_IViewSource,
> &LIBID_HTMLEDITLib>,
> public IOleCommandTarget // derive from IOleCommandTarget
> {
> public:
> CViewSource() {
> m_bDocumentCompleted = false;
> m_bIsBanListRead = false;
> }

> static _ATL_REGMAP_ENTRY RegEntries[];
> // Default implementation does not pass _ATL_REGMAP_ENTRY array
> // DECLARE_REGISTRY_RESOURCEID(IDR_VIEWSOURCE)
> static HRESULT WINAPI UpdateRegistry(BOOL bRegister)
> {
> return _Module.UpdateRegistryFromResource ( IDR_VIEWSOURCE,
> bRegister, RegEntries );
> }

> DECLARE_CLASSFACTORY_SINGLETON(CViewSource) // Do not want multiple
> objects for each command execution
> DECLARE_NOT_AGGREGATABLE(CViewSource)
> DECLARE_PROTECT_FINAL_CONSTRUCT()

> BEGIN_COM_MAP(CViewSource)
> COM_INTERFACE_ENTRY(IViewSource)
>     COM_INTERFACE_ENTRY(IDispatch)
> COM_INTERFACE_ENTRY(IOleCommandTarget) // responds to QueryInterface
> for IOleCommandTarget
> COM_INTERFACE_ENTRY(IObjectWithSite)
> END_COM_MAP()

> // IObjectWithSite
> public:
> STDMETHOD(SetSite)(IUnknown *pUnkSite);

> // IDispatch
> public:
> STDMETHOD(Invoke)(DISPID, REFIID, LCID, WORD, DISPPARAMS*,
>               VARIANT*, EXCEPINFO*, UINT*);

> // IViewSource
> public:
> STDMETHOD(OnDocumentComplete)(BOOL bForceWindow=false);
> STDMETHOD(OnQuit)(void);

> // IOleCommandTarget
> public:
>     STDMETHOD(QueryStatus)( const GUID *pguidCmdGroup, ULONG cCmds,
> OLECMD prgCmds[], OLECMDTEXT *pCmdText);
> STDMETHOD(Exec)( const GUID *pguidCmdGroup, DWORD nCmdID, DWORD
> nCmdexecopt, VARIANT *pvaIn, VARIANT *pvaOut);

> private:
> STDMETHOD(Connect)(void);
> STDMETHOD(Disconnect)(void);
> STDMETHOD(GetDocumentContent)(BOOL addUnconditionally);
> STDMETHOD(RetrieveBrowserWindow)(void);
> static BOOL CALLBACK WndEnumProc(HWND, LPARAM);
> static LRESULT CALLBACK IEKeyboardProc(int, WPARAM, LPARAM);

> private:
> CComQIPtr<IWebBrowser2, &IID_IWebBrowser2> m_spWebBrowser2;
>     CComQIPtr<IConnectionPointContainer,
> &IID_IConnectionPointContainer> m_spCPC;
> CComPtr<IHTMLElement> m_pBody;
> BOOL m_bDocumentCompleted;
> DWORD m_dwCookie;
> HWND m_hwndBrowser;
> RECT m_rcIE, m_rcHTML;
> BOOL m_bIsBanListRead;
> vector<string *> m_BanKeyNames;
> };
> ---------------------------

> Is something wrong here?



Mon, 02 May 2005 00:27:31 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Create instance of an com object inside a the Com server

2. Storing Object instances and reteriving Object instances

3. com object and its instances

4. Multiple instances of same COM object

5. Unable to create instance of the COM Object

6. Single COM object instance

7. Getting the instance handle of a COM object

8. Multiple instances of COM object

9. running instance of COM object in ATL/MFC server

10. Instancing COM Objects

11. Service trying to create remote COM object instance.

12. Creating an instance of a COM object

 

 
Powered by phpBB® Forum Software