Click here to Skip to main content
15,885,366 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.2K   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: main.cpp
///     License: Free, but autor and company must be mentioned.
///-////////////////////////////////////////////////////////////////////////////////////////////////

#include <windows.h>
#include "dWebCtrl.h"
#include "resource.h"

///     Declare Windows procedure
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

char szDirectorioTrabajo[MAX_PATH]={'\0'};
HINSTANCE hInst;

///     Make the class name into a global variable
char szClassName[ ] = "DaveWareBrowserApp";

dWebCtrl WebBrowser;

///-////////////////////////////////////////////////////////////////////////////////////////////////
///         WinMain
///-////////////////////////////////////////////////////////////////////////////////////////////////
int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    hInst=hThisInstance;

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = "IDR_CUSTOM_POPUP";                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "DaveWare: Calling Javascript functions from Plain C++",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);
    UpdateWindow (hwnd);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}

///-////////////////////////////////////////////////////////////////////////////////////////////////
///         Windows Procedure
///-////////////////////////////////////////////////////////////////////////////////////////////////
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{

    switch (message)                  /* handle the messages */
    {
        case WM_COMMAND:
            switch LOWORD(wParam)
            {
                case IDM_COMMAND_ALERT:
                    WebBrowser.ExecuteScript("javascript:message('David is the best');");
                    break;

                case IDM_FILE_EXIT:
                    SendMessage(hwnd,WM_DESTROY,(WPARAM)0,(LPARAM)0);
                    break;

            }

            break;

        case WM_CREATE:
        {
            GetCurrentDirectory(MAX_PATH-1,szDirectorioTrabajo);

            WebBrowser.Iniciar(hwnd,hInst);
            break;
        }

        case WM_SIZE:

            WebBrowser.OnResize(0,0);
            break;


        case WM_QUIT:
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;


        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

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