代码:CoInitialize(NULL);
SHDocVw::IShellWindowsPtr m_pSHWinds;
m_pSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows));
long nCount = m_pSHWinds->GetCount(); 
IDispatch * pDisp;
HRESULT hr;
for (long i=1;i<=nCount;i++)
{
         _variant_t va(i, VT_I4); 
pDisp = m_pSHWinds->Item(va); 
if (NULL != pDisp)
{
SHDocVw::IWebBrowser2Ptr pBrowser(pDisp);
         }
}请问我执行到最后一个语句SHDocVw::IWebBrowser2Ptr pBrowser(pDisp)时出错:privileged instruction, 这是什么原因?

解决方案 »

  1.   

    for (long i=1;i<=nCount;i++) 这里改为for (long i=0;i<nCount;i++)void CGetIESrcDlg::GetRunningIESrc()
    {
    // Import the following files in your stdafx.h
    // #import <mshtml.tlb> // Internet Explorer 5
    // #import <shdocvw.dll>
    //  Refer to "Connect to Internet Explorer Instances, From your own Process. " in www.codeguru.com
    //  enumeration all shell windows, only URL and source code of the first IE window is shown, you have to
    //modify according your own need.
    SHDocVw::IShellWindowsPtr m_spSHWinds;
    CoInitialize(NULL);
    if(m_spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows)) == S_OK)
    {
    IDispatchPtr spDisp;
    long nCount = m_spSHWinds->GetCount();
    for (long i = 0; i < nCount; i++)
    {
    _variant_t va(i, VT_I4);
    spDisp = m_spSHWinds->Item(va);
    SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);
    if (spBrowser != NULL)
    {
    IDispatchPtr spDisp;
    if(spBrowser->get_Document(&spDisp) == S_OK && spDisp!= 0 )
    {
    MSHTML::IHTMLDocument2Ptr spHtmlDocument(spDisp);
    MSHTML::IHTMLElementPtr spHtmlElement;
    if(spHtmlDocument==NULL)
    continue;
    spHtmlDocument->get_body(&spHtmlElement);
    if(spHtmlDocument==NULL)
    continue;
    _bstr_t str ;
    str=spBrowser->GetLocationURL();//URL of IE window();
    AfxMessageBox(str);
    MSHTML::IHTMLDocument3* pHTMLDoc3;
    HRESULT hr = spHtmlDocument->QueryInterface(__uuidof(MSHTML::IHTMLDocument3),(LPVOID*)&pHTMLDoc3);
    ASSERT(SUCCEEDED(hr));
    MSHTML::IHTMLElement* pDocElem;
    hr = pHTMLDoc3->get_documentElement(&pDocElem);
    pHTMLDoc3->Release();
    ASSERT(SUCCEEDED(hr));
    BSTR bstrHTML;
    pDocElem->get_outerHTML(&bstrHTML);
    pDocElem->Release();
    AfxMessageBox(CString(bstrHTML));
    SysFreeString(bstrHTML);
    }
    break;
    }
    } }
    else {
    AfxMessageBox("Shell Windows interface is not avilable");
    }
    CoUninitialize();
    }