Jua*_*uan 6 c# internet-explorer mshtml webbrowser-control
我有这两种方法来获取当前的旅行日志条目并转到通过调用GetTravelLogEntry方法检索的日志条目:
public static ITravelLogEntry GetTravelLogEntry(WebBrowser webBrowser)
{
int HRESULT_OK = 0;
SHDocVw.IWebBrowser2 axWebBrowser = (SHDocVw.IWebBrowser2)webBrowser.ActiveXInstance;
IServiceProvider psp = axWebBrowser as IServiceProvider;
if (psp == null) throw new Exception("Could not get IServiceProvider.");
IntPtr oret = IntPtr.Zero;
int hr = psp.QueryService(ref SID_STravelLogCursor, ref IID_ITravelLogStg, out oret);
if ((oret == IntPtr.Zero) || (hr != HRESULT_OK)) throw new Exception("Failed to query service.");
ITravelLogStg tlstg = Marshal.GetObjectForIUnknown(oret) as ITravelLogStg;
if (null == tlstg) throw new Exception("Failed to get ITravelLogStg");
ITravelLogEntry ptle = null;
hr = tlstg.GetRelativeEntry(0, out ptle);
if (hr != HRESULT_OK) MessageBox.Show("Failed to get travel log entry with error " + hr.ToString("X"));
Marshal.ReleaseComObject(tlstg);
return ptle;
}
public static void TravelToTravelLogEntry(WebBrowser webBrowser, ITravelLogEntry travelLogEntry)
{
int HRESULT_OK = 0;
SHDocVw.IWebBrowser2 axWebBrowser = (SHDocVw.IWebBrowser2)webBrowser.ActiveXInstance;
IServiceProvider psp = axWebBrowser as IServiceProvider;
if (psp == null) throw new Exception("Could not get IServiceProvider.");
IntPtr oret = IntPtr.Zero;
int hr = psp.QueryService(ref SID_STravelLogCursor, ref IID_ITravelLogStg, out oret);
if ((oret == IntPtr.Zero) || (hr != HRESULT_OK)) throw new Exception("Failed to query service.");
ITravelLogStg tlstg = Marshal.GetObjectForIUnknown(oret) as ITravelLogStg;
if (null == tlstg) throw new Exception("Failed to get ITravelLogStg");
hr = tlstg.TravelTo(travelLogEntry);
if (hr != HRESULT_OK) MessageBox.Show("Failed to travel to log entry with error " + hr.ToString("X"));
Marshal.ReleaseComObject(tlstg);
}
Run Code Online (Sandbox Code Playgroud)
这WebBrowser是一个.NET WebBrowser控件.在方法ITravelLogStg::TravelTo内部调用时,TravelToTravelLogEntry我得到一个0x80004002,根据此页面是Interface not supported错误的.难道我做错了什么?
PD:我从这里获取了大部分代码.
好吧,您正在尝试导航到旅行日志中的当前条目,这没有多大意义,因为您已经在那里了。我可以重现这个特定情况下的错误,但发现它也不是很有帮助。
但是使用 0 以外的任何内容作为第一个参数GetRelativeEntry,然后调用TravelTo按预期工作。
ITravelLogStg::GetRelativeEntry 返回偏移量指定的条目。正偏移量返回当前条目之后的条目;负偏移量返回当前条目之前的条目。零返回当前条目。
(来源:MSDN)
尝试修改hr = tlstg.GetRelativeEntry(0, out ptle);- 第一个参数指定您要导航的方向。使用 0 以外的其他值应该可以,例如,您可以使用 -1 向后移动一个条目。