Making a program using COM in windows

Pages: 123
And here is IWebBrowserEvents.cpp...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// IWebBrowserEvents.cpp
#ifndef UNICODE
   #define  UNICODE
#endif
#ifndef _UNICODE
   #define  _UNICODE
#endif
#define  MyDebug
#include <windows.h>
#include <cstdio>
#include <string>
#include "Main.h"
#include "IWebBrowserEvents.h"
#include "IWebBrowser.h"
extern "C" const IID LIBID_WebBrowser      = {0xEAB22AC0,0x30C1,0x11CF,{0xA7,0xEB,0x00,0x00,0xC0,0x5B,0xAE,0x0B}};
extern "C" const IID IID_IWebBrowserEvents = {0x34A715A0,0x6587,0x11D0,{0x92,0x4A,0x00,0x20,0xAF,0xC7,0xAC,0x4D}};
#ifdef MyDebug
   extern FILE* fp;
#endif


CEventSink::CEventSink() : m_cRef(0)       //CEventSink Constructor
{
 ITypeLib* pTypeLib=NULL;
 HRESULT hr;

 #ifdef MyDebug
 fprintf(fp,"\n    Entering CEventSink() Constructor!\n");
 fprintf(fp,"      this = 0x%p\n",this);
 #endif
 this->m_pTypeInfo=NULL;
 hr=LoadRegTypeLib(LIBID_WebBrowser,1,0,LANG_NEUTRAL,&pTypeLib);
 if(SUCCEEDED(hr))
 {
    hr = pTypeLib->GetTypeInfoOfGuid(IID_IWebBrowserEvents, &m_pTypeInfo);
    pTypeLib->Release();
 }
 #ifdef MyDebug
 fprintf(fp,"    Leaving CEventSink() Constructor!\n\n");
 #endif
}


CEventSink::~CEventSink()
{
 #ifdef MyDebug
 fprintf(fp,"    Entering CEventSink() Destructor!\n");
 fprintf(fp,"      this->m_cRef = %ld\n",this->m_cRef);
 #endif
 if(this->m_pTypeInfo)
    this->m_pTypeInfo->Release();
 #ifdef MyDebug
 fprintf(fp,"    Leaving CEventSink() Destructor!\n");
 #endif
}


HRESULT CEventSink::QueryInterface(REFIID riid, void** ppv)
{
 if(riid == IID_IUnknown)
    *ppv = (IUnknown*)this;
 else if(riid == IID_IDispatch)
    *ppv = (IDispatch*)this;
 else if(riid == IID_IWebBrowserEvents)
    *ppv = (DWebBrowserEvents2*)this;
 else
 {
    *ppv = NULL;
    return E_NOINTERFACE;
 }
 AddRef();

 return S_OK;
}


ULONG CEventSink::AddRef()
{
 this->m_cRef++;
 return this->m_cRef;
}


ULONG CEventSink::Release()
{
 if(--m_cRef != 0)
    return m_cRef;
 else
    delete this;

 return 0;
}


HRESULT CEventSink::GetTypeInfoCount(UINT* pCountTypeInfo)
{
 *pCountTypeInfo = 1;
 return S_OK;
}


HRESULT CEventSink::GetTypeInfo(UINT iTypeInfo, LCID lcid, ITypeInfo** ppITypeInfo)
{
 *ppITypeInfo = NULL;
 if(iTypeInfo != 0)
    return DISP_E_BADINDEX;
 m_pTypeInfo->AddRef();
 *ppITypeInfo = m_pTypeInfo;
 return S_OK;
}


HRESULT CEventSink::GetIDsOfNames(REFIID riid, LPOLESTR* rgszNames, UINT cNames, LCID lcid, DISPID* rgDispId)
{
 if(riid != IID_NULL)
    return DISP_E_UNKNOWNINTERFACE;
 return DispGetIDsOfNames(m_pTypeInfo, rgszNames, cNames, rgDispId);
}


HRESULT CEventSink::Invoke(DISPID dispId, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDParms, VARIANT* pVarResult, EXCEPINFO* pErInfo, UINT* pErr)
{
 if(riid != IID_NULL)
    return DISP_E_UNKNOWNINTERFACE;
 else
 {
    switch(dispId)
    {
      case 0xfc:  // NavigateComplete2
        {
           this->NavigateComplete2(pDParms->rgvarg[1].pdispVal, pDParms->rgvarg[0].pvarVal);
           break;
        }
    }
 }

 return S_OK;
}


void CEventSink::NavigateComplete2(IDispatch* pDisp, VARIANT* pVariantURL)
{
 #ifdef MyDebug
    fprintf(fp,"  Entering CEventSink::NavigateComplete2()\n");
    fprintf(fp,"    pVariantURL->bstrVal = %S\n",pVariantURL->bstrVal);
    fprintf(fp,"  Leaving CEventSink::NavigateComplete2()\n\n");
 #else
    MessageBox(NULL,pVariantURL->bstrVal,L"Received Event CEventSink::NavigateComplete2()",MB_ICONINFORMATION);
 #endif
}


void CEventSink::StoreGridPointer(IWebBrowser* pIWebBrowser)
{
 this->pWebBrowser=pIWebBrowser;
}
Here would be the Output.txt debug log file created by a run of the above on an x64 machine running x64 but not connected to the internet and where I clicked the 'Naviogate' button...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Entering WinMain()
  Entering fnWndProc_OnCreate()
    Wea->hIns                 = 0x0000000000400000
    Wea->hWnd                 = 0x0000000000180216
    hContainer                = 0x0000000000330384
    S_OK                      = 0
    hr(AtlAxWinInit)          = 49738
    AtlAxWinInit() Succeeded!
    hr(AtlAxCreateControl)    = 0
    ppUnkContainer            = 0x00000000003b76c0
    hr(AtlAxGetControl)       = 0
    pUnkIExplorer             = 0x00000000005f8ec0
    pWebBrowser               = 0x00000000005f90a0
    pConnectionPointContainer = 0x00000000005f8fd0
    pConnectionPoint          = 0x00000000005f9020

    Entering CEventSink() Constructor!
      this = 0x00000000003bc140
    Leaving CEventSink() Constructor!

    pEventSink                = 0x00000000003bc140
  Leaving fnWndProc_OnCreate()

  hWnd = 0x0000000000180216

  Entering fnWndProc_OnCommand : Case BTN_NAVIGATE
    pWebBrowser = 0x00000000005f90a0
  Leaving fnWndProc_OnCommand : Case BTN_NAVIGATE

  Entering CEventSink::NavigateComplete2()
    pVariantURL->bstrVal = http://www.cplusplus.com/
  Leaving CEventSink::NavigateComplete2()

  Entering CEventSink::NavigateComplete2()
    pVariantURL->bstrVal = http://www.cplusplus.com/
  Leaving CEventSink::NavigateComplete2()

  Entering fnWndProc_OnDestroy()
    pConnectionPoint = 0x00000000005f9020
    dwCookie         = 1
    Entering CEventSink() Destructor!
      this->m_cRef = 0
    Leaving CEventSink() Destructor!
    pWebBrowser      = 0x00000000005f90a0
  Entering fnWndProc_OnDestroy()
Leaving WinMain()


You can learn a lot about the serquencing of calls by studying such output. Note particularly how the entirety of the code in fnWndProc_OnCreate() executes before the hWnd in WinMain() from the CreateWindowEx() call for the main program window gets printed. So all of fnWndProc_OnCreate() is occurring within the CreateWindowEx() call for the main Window. As such, all that code can be considered as part of a C based object Constructor call.
Topic archived. No new replies allowed.
Pages: 123