천객만래 [千客萬來] (It has an interminable succession of visitors)


[펌] ActiveX에서 javascript 함수 호출방법


출처 : http://k.80port.net/board/view.php?id=techboard&page=5&sn1=&divpage=1&sn=off&ss=on&sc=on&select_arrange=headnum&desc=asc&no=140

void CallScript()

{
    HRESULT hr;
    IWebBrowser2 * pparent=NULL;
    pparent = (IWebBrowser2*) GetIWebPointer();
    LPDISPATCH lpDispatch = NULL;


    hr = pparent->get_Document(&lpDispatch);
    if (!SUCCEEDED(hr))
    {
        AfxMessageBox("error get document");
        lpDispatch->Release();
        return;
    }

    IHTMLDocument2 *m_pDoc2;
    hr = lpDispatch->QueryInterface(IID_IHTMLDocument2, (void**) &m_pDoc2);
    if (!SUCCEEDED(hr))
    {
        AfxMessageBox("error get document interface");
        m_pDoc2->Release();
        return;
    }
   
    CComQIPtr< IHTMLDocument > pDoc( m_pDoc2 );

    //
    // 스크립트 엔진의 디스패치 를 얻는다.
    //

    CComPtr< IDispatch > pDisp;
    hr = pDoc->get_Script( &pDisp );

    //
    // 스크립트 엔진디스패치에서 주어진 함수를 찾는다. 즉 dispid를 세트.
    //

    OLECHAR FAR* szFunctionName = L"EndUpload";        // 호출할 Javascript 명
    DISPID dispid;

    hr = pDisp->GetIDsOfNames( IID_NULL, &szFunctionName, 1,
                              LOCALE_SYSTEM_DEFAULT, &dispid);

    //
    // Invoke하기 위한 파라미터 세트.
    //

    DISPPARAMS dispParams;
    dispParams.rgvarg = NULL;
    dispParams.rgdispidNamedArgs = NULL;
    dispParams.cArgs = 0;
    dispParams.cNamedArgs = 0;

    hr = pDisp->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT,
                       DISPATCH_METHOD, &dispParams, NULL, NULL, NULL);
 }

 

방법 2

void CallScript()
{
    LPOLECLIENTSITE pIClientSite;
    IDispatch* pIHTMLDocument = NULL;
    IHTMLDocument2* pDoc = NULL;
    IServiceProvider* pISP = NULL;

    CComPtr< IDispatch > pDisp;

    try
    {
        pIClientSite = GetClientSite();
       
        hr = pIClientSite->QueryInterface(IID_IServiceProvider, (void**)&pISP);
        if (hr != S_OK) throw 0;
       
        hr = pISP->QueryService(SID_SContainerDispatch, IID_IDispatch, (void **)&pIHTMLDocument);
        if (hr != S_OK) throw 0;

        hr = pIHTMLDocument->QueryInterface(IID_IHTMLDocument2,(void**)&pDoc);
        if (hr != S_OK) throw 0;
       
        hr = pDoc->get_Script( &pDisp );
        if (hr != S_OK) throw 0;
       
        OLECHAR FAR* szFunctionName = L"EndUpload";        // 호출할 Javascript 명
       
        DISPID dispid;
       
        hr = pDisp->GetIDsOfNames( IID_NULL, &szFunctionName, 1,  LOCALE_SYSTEM_DEFAULT, &dispid);
        if (hr != S_OK) throw 0;
       
        DISPPARAMS dispParams;
        dispParams.rgvarg = NULL;
        dispParams.rgdispidNamedArgs = NULL;
        dispParams.cArgs = 0;
        dispParams.cNamedArgs = 0;
       
        hr = pDisp->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &dispParams, NULL, NULL, NULL);
        if (hr != S_OK) throw 0;

    }
    catch (...)
    {
         
    }

    if(pDoc) pDoc->Release();   
    if(pIHTMLDocument) pIHTMLDocument->Release();   
    if(pISP) pISP->Release();   
    if(pDisp) pDisp->Release();
//    if(pIClientSite) pIClientSite->Release();
}
 

Posted by SB패밀리
1. IWebBrowser2 에서 IHTMLDocument2 인터페이스 구하기

IWebBrowser2 *m_pWebFromUnknown;
IWebBrowser2 *m_pWebFromDoc;

STDMETHODIMP CTestObj::SetSite( IUnknown *pUnkSite )
{
    IOleClientSite *pClientSite;
    HRESULT hr;

    hr = pUnkSite-> QueryInterface( IID_IOleClientSite, 
    (LPVOID *)&pClientSite );
    if ( FAILED( hr ) ) return S_OK;

    IOleContainer *pContainer;
    hr = pClientSite-> GetContainer( &pContainer );
    pClientSite-> Release();
    if ( FAILED( hr ) ) return S_OK;
    
    IServiceProvider *pServiceProvider;
    hr = pContainer-> QueryInterface( IID_IServiceProvider, 
    (LPVOID *)&pServiceProvider );
    pContainer-> Release();
    if ( FAILED( hr ) ) return S_OK;

    hr = pServiceProvider-> QueryService( 
SID_SWebBrowserApp, IID_IWebBrowser2, (LPVOID *)
&m_pWebFromUnknown );
    pServiceProvider-> Release();
    if ( FAILED( hr ) ) return S_OK;

    LPDISPATCH pDocDisp;
    hr = m_pWebFromUnknown-> get_Document( &pDocDisp );
    m_pWebFromUnknown-> Release();

    return S_OK;
}

 

2. IHTMLDocument2 에서 IWebBrowser2 인터페이스 구하기


STDMETHODIMP CTestObj::setdoc(LPDISPATCH lpDisp)
{
// IHTMLDocument2 인터페이스 포인터에서 IWebBrowser2 인터페이스 
포인터를 구합니다.

    CComQIPtr<  IHTMLDocument2>  pDoc = lpDisp;

    IOleCommandTarget *pCmdTarget = NULL;
    pDoc-> QueryInterface( IID_IOleCommandTarget, (LPVOID *)
&pCmdTarget );

    if ( pCmdTarget ) {
        IServiceProvider *pSP;
        HRESULT hr = pCmdTarget-> QueryInterface( 
        IID_IServiceProvider, (LPVOID *)&pSP );
        pCmdTarget-> Release();

        if ( SUCCEEDED( hr ) ) {
            hr = pSP-> QueryService( 
            SID_SWebBrowserApp, IID_IWebBrowser2, (LPVOID *)&m_pWebFromDoc );

        }
    }

    if ( m_pWebFromDoc == m_pWebFromUnknown ) {
        ::MessageBox( NULL, "똑같습니
            다.", "IWebBrowser2 인터페이스 포인터 비교", MB_OK );
    }

    return S_OK;
}
 
 
Posted by SB패밀리