본문 바로가기
IT-개발,DB

[개발/VC++] ActiveX에서 javascript 함수 호출방법

by SB리치퍼슨 2011. 1. 31.


[펌] 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();
}
 

반응형

댓글