Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Fast Track Visual C++ 6.0 Programming (Publisher: John Wiley & Sons, Inc.) Author(s): Steve Holzner ISBN: 0471312908 Publication Date: 09/01/98 Search this book:
Previous | Table of Contents | Next
#### Creating the Launch Program Using AppWizard, create an MDI project named Launch and add a new item to the File menu: Install launcher. The user selects this item to install our new hook. Figure 11.3 Using a keyboard hook to launch the Windows calculator. As we learned in Chapter 10, “The Power of Dynamic Link Libraries,” link LaunchDLL.lib into the Launch project now and place LaunchDLL.dll in the c:\windows\system directory. Finally, attach a handling function to the Install Launcher menu item.
    void CLaunchView::OnFileInstalllauncher()
    {
        // TODO: Add your command handler code here
    }
All we have to do here is call InstallLauncher in LaunchDLL.dll.
    void CLaunchView::OnFileInstalllauncher()
    {
        InstallLauncher();         ⇐
    }
Run the Launch program as shown in Figure 11.3 and select the Install Launcher item in the File menu to install the launcher. Then, press ^C while running any program in Windows. The Windows calculator appears on the screen until you end the Launch program. Now we’re using keyboard hooks in our Visual C++ programs. The code for this example, LaunchDLL.h and LaunchDLL.cpp, appears in Listing 11.2. LaunchView.h and LaunchView.cpp appears in Listing 11.3. Listing 11.2 LaunchDLL.h and LaunchDLL.cpp
    //

    #if !defined(AFX_LAUNCHDLL_H__6433B666_A86C_11D1_887F_D
    42B07C10710__INCLUDED_)
    #define AFX_LAUNCHDLL_H__6433B666_A86C_11D1_887F_D
    42B07C10710__INCLUDED_

    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000

    #ifndef __AFXWIN_H__
        #error include ‘stdafx.h’ before including this file for PCH
    #endif

    #include “resource.h”        // main symbols

    #define DllExport    __declspec( dllexport )
    DllExport void WINAPI InstallLauncher();
    LRESULT CALLBACK LauncherHook (int nCode, WORD wParam, DWORD lParam );

    /////////////////////////////////////////////////////////////////////////////
    // CLaunchDLLApp
    // See LaunchDLL.cpp for the implementation of this class
    //

    class CLaunchDLLApp : public CWinApp
    {
    public:
        CLaunchDLLApp();

    // Overrides
        // ClassWizard generated virtual function overrides
        //{{AFX_VIRTUAL(CLaunchDLLApp)
        //}}AFX_VIRTUAL

        //{{AFX_MSG(CLaunchDLLApp)
            // NOTE - the ClassWizard will add and remove member functions here.
            //    DO NOT EDIT what you see in these blocks of generated code !

        //}}AFX_MSG
        DECLARE_MESSAGE_MAP()
    };

    /////////////////////////////////////////////////////////////////////////////

    //{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional
    declarations immediately before the previous line.

    #endif // !defined(AFX_LAUNCHDLL_H__6433B666_A86C_11D1_887F_D
    42B07C10710__INCLUDED_)

    // LaunchDLL.cpp : Defines the initialization routines for the DLL.
    //

    #include “stdafx.h”
    #include “LaunchDLL.h”

    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif

    //#pragma data_seg( “CommMem” )
        HHOOK Hook = NULL;
    //#pragma data_seg()

    HANDLE hDLLInst = 0;

    //
    //    Note!
    //
    //        If this DLL is dynamically linked against the MFC
    //        DLLs, any functions exported from this DLL which
    //        call into MFC must have the AFX_MANAGE_STATE macro
    //        added at the very beginning of the function.

    //
    //        For example:
    //
    //        extern “C” BOOL PASCAL EXPORT ExportedFunction()
    //        {
    //            AFX_MANAGE_STATE(AfxGetStaticModuleState());
    //            // normal function body here
    //        }
    //
    //        It is very important that this macro appear in each
    //        function, prior to any calls into MFC.  This means that
    //        it must appear as the first statement within the
    //        function, even before any object variable declarations
    //        as their constructors may generate calls into the MFC
    //        DLL.
    //
    //        Please see MFC Technical Notes 33 and 58 for additional
    //        details.
    //

    /////////////////////////////////////////////////////////////////////////////
    // CLaunchDLLApp

    BEGIN_MESSAGE_MAP(CLaunchDLLApp, CWinApp)
        //{{AFX_MSG_MAP(CLaunchDLLApp)
            // NOTE - the ClassWizard will add and remove mapping macros here.
            //    DO NOT EDIT what you see in these blocks of generated code!
        //}}AFX_MSG_MAP
    END_MESSAGE_MAP()

    /////////////////////////////////////////////////////////////////////////////
    // CLaunchDLLApp construction

    CLaunchDLLApp::CLaunchDLLApp()
    {

        // TODO: add construction code here,
        // Place all significant initialization in InitInstance
        //m_hInstance;
    }

    /////////////////////////////////////////////////////////////////////////////
    // The one and only CLaunchDLLApp object

    CLaunchDLLApp theApp;

    DllExport void WINAPI InstallLauncher()
    {
        Hook = (HHOOK)SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)LauncherHook,
    theApp.m_hInstance, 0);
    }

    LRESULT CALLBACK LauncherHook (int nCode, WORD wParam, DWORD lParam )
    {
        LRESULT Result = CallNextHookEx(Hook, nCode, wParam, lParam);

        if(nCode == HC_ACTION){
            if ((GetKeyState(VK_CONTROL) < 0) && (wParam == ‘C’)){
                if(lParam & 0x80000000){
                    STARTUPINFO StartupInfo;
                    PROCESS_INFORMATION ProcessInfo;

                    memset(&StartupInfo, 0, sizeof(StartupInfo));
                    StartupInfo.cb = sizeof(StartupInfo);

                    CreateProcess(”c:\\windows\\calc.exe”, NULL, NULL, NULL,
    FALSE, 0,    NULL, NULL, &StartupInfo, &ProcessInfo);
                    Result = 0;
                }
            }
        }

        return Result;
    }
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.