Click here to Skip to main content
15,881,600 members
Articles / Programming Languages / C++
Article

Sweep the Minesweeper

Rate me:
Please Sign up or sign in to vote.
4.92/5 (7 votes)
19 Mar 2001 180.9K   3K   48   32
This article shows how to inject your code into another applications address space and then subclass their window to force it to act as you desire.

Sample Image - SweeptheMinesweeper.jpg

Introduction

Have you ever been challenged by anyone? If your answer is yes than it is easy for you to understand my feeling when I, being a Microsoft Windows Developer (just pretending), was challenged by a Java Developer over Microsoft Windows Minesweeper (winmine.exe). How amazing was it that this person criticized Microsoft (and there is nothing wrong with that!) and then did all of their Java Development using Microsoft Platform :)

I asked for some days to practice Minesweeper. During those days I realized that I might not be able to trounce that guy in a speed trial. So being a Microsoft Windows Developer I decided to act as Microsoft Windows Developer (I don't mean cheating!).

I decided to stop the timer of Minesweeper. To get inside Minesweeper's address space I choose WH_CBT hook and wait for the HCBT_CREATEWND notification in my CBTProc(ShellDll_MainHook). Whenever a window with a class name "Minesweeper" came into being I subclassed it for future needs. There is no problem in subclassing because at that moment I am in the address space of the process that created this window. In the new WndProc of Minesweeper I stop any WM_TIMER messages proceeding. As a result, this stops the timer during the game.

LRESULT CALLBACK ShellDll_MainHook(int nCode, WPARAM wParam, LPARAM lParam)
{
    TCHAR szClass[MAX_PATH] = {0};

    if(nCode '<' 0)
        return CallNextHookEx(g_hShellHook, nCode, wParam, lParam);

    if(nCode == HCBT_CREATEWND)
    {
        HWND hwndToNewWindow = reinterpret_cast<HWND>(wParam);

        GetClassName(hwndToNewWindow, szClass, MAX_PATH);
        if(!lstrcmpi(szClass, __TEXT("Minesweeper")))
        {
            g_hwndToMineWindow = hwndToNewWindow;
            if(IsWindowUnicode(g_hwndToMineWindow))
            {
                pfnWndProc = (WNDPROC)SetWindowLongW(g_hwndToMineWindow,
                                                     GWL_WNDPROC, 
                    (LPARAM)(WNDPROC)MineSweeper_SubClassWndProc);
            }
            else
            {
                pfnWndProc = (WNDPROC)SetWindowLongA(g_hwndToMineWindow,
                                                     GWL_WNDPROC, 
                    (LPARAM)(WNDPROC)MineSweeper_SubClassWndProc);
            }
        }
    }
    return CallNextHookEx(g_hShellHook, nCode, wParam, lParam);
}

For more information about Hooks see Kayle Marsh's article about Win32 Hooks in MSDN. For sub-classing see MSDN and http://www.codeproject.com/useritems/safesubclassing.asp

I wrote all the code for the hook and subclassing in a Dynamic Link Library (MinesweeperHook.dll) as is the requirement. To set the Hook I made another application, which on starting sets the hook and waits for Minesweeper while resting in the Tray Notification Area (System Tray). You can unset the hook by closing this application. Just right mouse click and then select close from the menu. This application (MinesweeperExe.exe) sets the hook using MinesweeperHook's exported function ShellDll_Hook(), and then uses Shell_NotifyIcon() to display the Tray Notification area icon. Before exiting it unsets the hook by calling MinesweeperHook's exported function ShellDll_UnhHook().

In DllMain() of MinesweeperHook.dll, it is necessary to un-subclass the Minesweeper because after unsetting the hook our DLL will no longer stay in the Minesweeper's address space. This is the case when we are playing "winmine" and we close the MinesweeperExe.exe through its context menu.

Finally, don't tell me if you find my coding style similar to Dino Esposito :)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Pakistan Pakistan
Mumtaz Zaheer is working as Senior System Analyst with Information Architects, Pakistan (http://www.info-architects.com/).

Comments and Discussions

 
Generalmine sweeper cheat codes Pin
16-Mar-04 15:28
suss16-Mar-04 15:28 
GeneralRe: mine sweeper cheat codes Pin
Christian Graus16-Mar-04 16:15
protectorChristian Graus16-Mar-04 16:15 
GeneralRe: mine sweeper cheat codes Pin
Hawk - The programmer1-Jul-07 22:58
Hawk - The programmer1-Jul-07 22:58 
Questioncodes anyone??? Pin
Anonymous2-Jun-03 17:29
Anonymous2-Jun-03 17:29 
AnswerRe: codes anyone??? Pin
dysfunctional@freesurf.fr4-Sep-07 11:21
dysfunctional@freesurf.fr4-Sep-07 11:21 
GeneralAnd if you just care about the best time... Pin
Luis Alonso Ramos20-May-02 5:04
Luis Alonso Ramos20-May-02 5:04 
... and not really winning the game, you could try going to the Registry Editor to:

HKEY_CURRENT_USER\Software\Microsoft\winmine

and editing Name1, Name2, Name3, Time1, Time2 and Time3 keys. Name1/Time1 correspond to Beginner level, Name1/Time2 to Intermediate and Name3/Time3 to Advanced.

Much easier to impress those Java guys, or not?

-- LuisR

──────────────
  Luis Alonso Ramos
  Chihuahua, Mexico
  www.luisalonsoramos.com

"Do not worry about your difficulties in mathematics, I assure you that mine are greater." -- Albert Einstein
GeneralRe: And if you just care about the best time... Pin
21-May-02 6:31
suss21-May-02 6:31 
GeneralI did it a little different Pin
Jack Handy28-Mar-02 19:14
Jack Handy28-Mar-02 19:14 
GeneralRe: I did it a little different Pin
Dana Holt14-May-02 9:38
Dana Holt14-May-02 9:38 
GeneralRe: I did it a little different Pin
WhiteMouseGary28-Oct-03 17:55
WhiteMouseGary28-Oct-03 17:55 
QuestionHow about displaying the mine locations Pin
7-Oct-01 17:12
suss7-Oct-01 17:12 
AnswerRe: How about displaying the mine locations Pin
Mumtaz Zaheer7-Oct-01 20:12
Mumtaz Zaheer7-Oct-01 20:12 
GeneralRe: How about displaying the mine locations Pin
Jeremy Kimball12-Dec-03 6:05
Jeremy Kimball12-Dec-03 6:05 
GeneralRe: How about displaying the mine locations Pin
maximz20056-Dec-08 14:28
maximz20056-Dec-08 14:28 
GeneralStop the timer in Minesweeper WITHOUT code Pin
VGirish21-Jun-01 0:44
VGirish21-Jun-01 0:44 
GeneralRe: Stop the timer in Minesweeper WITHOUT code Pin
Mumtaz Zaheer21-Jun-01 3:35
Mumtaz Zaheer21-Jun-01 3:35 
GeneralRe: Stop the timer in Minesweeper WITHOUT code Pin
21-Jun-01 15:33
suss21-Jun-01 15:33 
GeneralRe: Stop the timer in Minesweeper WITHOUT code Pin
Mumtaz Zaheer26-Jun-01 23:15
Mumtaz Zaheer26-Jun-01 23:15 
GeneralRe: Stop the timer in Minesweeper WITHOUT code Pin
WhiteMouseGary28-Oct-03 17:48
WhiteMouseGary28-Oct-03 17:48 
QuestionDo we have to write a Hook? Pin
4-Jun-01 0:53
suss4-Jun-01 0:53 
AnswerRe: Do we have to write a Hook? Pin
Mumtaz Zaheer11-Jun-01 23:32
Mumtaz Zaheer11-Jun-01 23:32 
GeneralRe: Do we have to write a Hook? Pin
10-Jul-01 8:14
suss10-Jul-01 8:14 
GeneralRe: Do we have to write a Hook? Pin
10-Apr-02 16:30
suss10-Apr-02 16:30 
GeneralRe: Do we have to write a Hook? Pin
delfin12-Jul-02 23:54
delfin12-Jul-02 23:54 
GeneralGrabing streaming of RealPlayer ! Pin
Kastellanos Nikos23-May-01 10:33
Kastellanos Nikos23-May-01 10:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.