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
private struct WindowData {
public System.Windows.Forms.HtmlDocument document;
public IConnectionPoint icpWin;
public int cookieWin;
public IConnectionPoint icpDoc;
public int cookieDoc;
}

private WindowData setWindowComConnect(object pDisp){
WindowData data = new WindowData();
// UCOM Connect
Guid guid;
data.document = (HTMLDocument)((SHDocVw.IWebBrowser2)pDisp).Document;
// Document
IConnectionPointContainer pConPtCon = (IConnectionPointContainer)data.document;
guid = typeof(HTMLDocumentEvents2).GUID;
pConPtCon.FindConnectionPoint(ref guid, out data.icpDoc);
data.icpDoc.Advise(this, out data.cookieDoc);
// Window
IConnectionPointContainer pCpcWin = (IConnectionPointContainer)data.document.parentWindow;
guid = typeof(HTMLWindowEvents2).GUID;
pCpcWin.FindConnectionPoint(ref guid, out data.icpWin);
data.icpWin.Advise(this, out data.cookieWin);
return data;
}
private void unsetWindowComConnect(WindowData data){
if (data.icpWin != null) {
try {
data.icpWin.Unadvise(data.cookieWin);
Marshal.ReleaseComObject(data.icpWin);
} catch (Exception ex) {
}
}
if (data.icpDoc != null) {
data.icpDoc.Unadvise(data.cookieDoc);
Marshal.ReleaseComObject(data.icpDoc);
}
if (data.document != null) {
Marshal.ReleaseComObject(data.document);
}
}