Click here to Skip to main content
15,881,938 members
Articles / Desktop Programming / Win32

JavaScript function access from plain C++: An example

Rate me:
Please Sign up or sign in to vote.
4.31/5 (8 votes)
24 Aug 2008CPOL1 min read 46.1K   1.1K   29  
How to execute JavaScript functions with parameters from C++ code.
///-////////////////////////////////////////////////////////////////////////////////////////////////
///     Autor:  David Elias Flores Escalante
///     Company: TeleTracking SAC
///     URL: http://teletrackingsac.dnsalias.net
///     Date:   24/08/2008
///     File name: dWebCtrl.cpp
///     License: Free, but autor and company must be mentioned.
///-////////////////////////////////////////////////////////////////////////////////////////////////

#include "dWebCtrl.h"

extern char szDirectorioTrabajo[];

///-//////////////////////////////////////////////////////////////////////////////////////////
///    dWebCtrl OnResize
///-//////////////////////////////////////////////////////////////////////////////////////////
void dWebCtrl::Iniciar(HWND hwnd,HINSTANCE hInst)
{
    char pathtofile[512]="";
    BSTR buffer;

    memset(pathtofile,'\0',sizeof(pathtofile)-1);

    hWnd=CreateWindowEx( WS_EX_CLIENTEDGE,//|WS_EX_TOPMOST,///|WS_EX_DLGMODALFRAME,
                        "EDIT",
                        "",
                        WS_CHILD| WS_BORDER |WM_PAINT| WS_VISIBLE,///|WS_DLGFRAME,
                        0,0,0,0,
                        hwnd,
                        NULL,//(HMENU)idVentana,
                        hInst,
                        NULL);

    if (hWnd == NULL)
    {
         MessageBox(hwnd, "no window", "Error", MB_ICONEXCLAMATION | MB_OK);
         return;
    }

    VariantInit(&vEmpty);

    hWebInstance = LoadLibrary("atl.dll");

    wsprintf(pathtofile,"file:///%s/daveware.html",szDirectorioTrabajo);

    size = MultiByteToWideChar(CP_ACP, 0, pathtofile, -1, 0, 0);
    if (!(buffer = (wchar_t *)GlobalAlloc(GMEM_FIXED, sizeof(wchar_t)*size)))
        return;

    MultiByteToWideChar(CP_ACP, 0, pathtofile, -1, buffer, size);

    bstrURL = SysAllocString(buffer);

    SysFreeString(buffer);

    /// R�cup�rer l'adresse de la fonction "AtlAxAttachControl":
    PAttachControl AtlAxAttachControl = (PAttachControl) GetProcAddress(hWebInstance, "AtlAxAttachControl");

    hParent=hwnd;
    hInstance=hInst;

    /// Initialiser la librairie COM pour notre programme:
    OleInitialize(0);

    CoCreateInstance(CLSID_WebBrowser, NULL, CLSCTX_SERVER,IID_IWebBrowser2, (void**)&pIwb);


    if (pIwb)
    {
        VARIANT vEmpty;
        VARIANT_BOOL bBusy = VARIANT_TRUE;
        VariantInit(&vEmpty);

        AtlAxAttachControl(pIwb,hWnd,0);

        result=pIwb->Navigate(bstrURL, &vEmpty, &vEmpty, &vEmpty, &vEmpty);

        if (SUCCEEDED(result))
        {
            while(bBusy == VARIANT_TRUE)
            {
                Sleep(500);
                pIwb->get_Busy(&bBusy);
            }
        }

        pIwb->get_HWND ((long*)(&hWebCtrl));

        SysFreeString(bstrURL);
    }

}

///-//////////////////////////////////////////////////////////////////////////////////////////
///    Destructor
///-//////////////////////////////////////////////////////////////////////////////////////////
dWebCtrl::~dWebCtrl()
{
    pIwb->Quit();
	pIwb->Release();

    OleUninitialize();

	FreeLibrary(hWebInstance);
}

///-//////////////////////////////////////////////////////////////////////////////////////////
///    dWebCtrl OnResize
///-//////////////////////////////////////////////////////////////////////////////////////////
void dWebCtrl::OnResize(int x,int y)
{
    GetWindowRect(hParent,&rect);
    long ParentWidth=rect.right-rect.left;
    long ParentHeight=rect.bottom-rect.top;

    MoveWindow(hWnd,x,y,ParentWidth-8,ParentHeight-32,TRUE);

}

///-//////////////////////////////////////////////////////////////////////////////////////////
///    dWebCtrl get hWebCtrl
///-//////////////////////////////////////////////////////////////////////////////////////////
HWND dWebCtrl::GetWebHwnd()
{
    return(hWebCtrl);

}

///-//////////////////////////////////////////////////////////////////////////////////////////
///    Execute string "Script"
///-//////////////////////////////////////////////////////////////////////////////////////////
void dWebCtrl::ExecuteScript(char* Script)
{
    BSTR bstrScript;
    BSTR bufferScript;
    static DWORD size=0;
    char nameInput[256]={'\0'};

    memset(nameInput,'\0',sizeof(nameInput)-1);

    wsprintf(nameInput,"javascript:%s",Script);

    size = MultiByteToWideChar(CP_ACP, 0, nameInput, -1, 0, 0);
    if (!(bufferScript = (wchar_t *)GlobalAlloc(GMEM_FIXED, sizeof(wchar_t)*size)))
        return;

    MultiByteToWideChar(CP_ACP, 0, nameInput, -1, bufferScript, size);
    bstrScript = SysAllocString(bufferScript);

    SysFreeString(bufferScript);

    pIwb->Navigate(bstrScript, &vEmpty, &vEmpty, &vEmpty, &vEmpty);
    SysFreeString(bstrScript);

    return;
}



By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer TeleTracking SAC
Peru Peru
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions