in my code for what happens when I click on my MAIN BUTTON runs my function light hack.
Inside my lighthack function is a for(;;) {} forever loop. Inside this forever loop
is a break when a condition is met. The problem I am having is when I click on this button
my program goes none-responding until the condition in the forever loop has been met.
Upon this time nothing else on my program works. How can I have this loop running
and the program still respond to events?

case IDC_MAIN_BUTTON:
                {

                LightHack();  // Inside this function is a for(;;) loop
                ShowWindow(*phwnd,SW_HIDE);
                };

Im reading Petzolds book, and multiple online tutorials as well. Here let me post all of my code.

#include <windows.h>
#include <Winuser.h>
#include <iostream>
#include <dos.h>
#include <fstream>
#include <WinInet.h>
#include "DransikFuncs.h"



using namespace std;


#define IDC_MAIN_BUTTON 102
#define IDC_MAIN_BUTTON2 103
#define IDC_MAIN_EDIT 104


HWND hEdit;
HWND* phwnd;

int OnOff=1;



LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,int nShowCmd)
{
    WNDCLASSEX wClass;
    ZeroMemory(&wClass,sizeof(WNDCLASSEX));
    wClass.cbClsExtra=NULL;
    wClass.cbSize=sizeof(WNDCLASSEX);
    wClass.cbWndExtra=NULL;
    wClass.hbrBackground=(HBRUSH)COLOR_WINDOW;
    wClass.hCursor=LoadCursor(NULL,IDC_ARROW);
    wClass.hIcon=NULL;
    wClass.hIconSm=NULL;
    wClass.hInstance=hInst;
    wClass.lpfnWndProc=(WNDPROC)WinProc;
    wClass.lpszClassName="Window Class";
    wClass.lpszMenuName=NULL;
    wClass.style=CS_HREDRAW|CS_VREDRAW;

    if(!RegisterClassEx(&wClass))
    {
        int nResult=GetLastError();
        MessageBox(NULL,
            "Window class creation failed\r\n",
            "Window Class Failed",
            MB_ICONERROR);
    }

    HWND hWnd=CreateWindowEx(NULL,
            "Window Class",
            "LIGHT HACK BY MANEGARD",
            WS_OVERLAPPEDWINDOW,
            200,
            200,
            400,
            480,
            NULL,
            NULL,
            hInst,
            NULL);

    if(!hWnd)
    {
        int nResult=GetLastError();

        MessageBox(NULL,
            "Window creation failed\r\n",
            "Window Creation Failed",
            MB_ICONERROR);
    }

    ShowWindow(hWnd,nShowCmd);

    //DRANSIK WINDOW HANDLE SET
HWND hwndDC = FindWindow(NULL,"Dransik (c)opyright 2013 Iron Will Games, LLC. ");
        if(hwndDC == 0)
        {
    MessageBox(0,"DRANSIK NOT OPEN  - PROGRAM CLOSING","Error",0);
    SendMessage(hWnd,0,0,WM_CLOSE);
    PostQuitMessage(0);
        }
HDC hdcDC = GetDC(hwndDC);

AuthCheck();





    MSG msg;
    ZeroMemory(&msg,sizeof(MSG));

    while(GetMessage(&msg,NULL,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);

    };

    return 0;
}

LRESULT CALLBACK WinProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
    switch(msg)
    {

        case WM_CREATE:
        {
            // Create an edit box
            hEdit=CreateWindowEx(WS_EX_CLIENTEDGE,
                "EDIT",
                "",
                WS_CHILD|WS_VISIBLE|
                ES_MULTILINE|ES_AUTOVSCROLL|ES_AUTOHSCROLL,
                80,
                350,
                200,
                100,
                hWnd,
                (HMENU)IDC_MAIN_EDIT,
                GetModuleHandle(NULL),
                NULL);
            HGDIOBJ hfDefault=GetStockObject(DEFAULT_GUI_FONT);
            SendMessage(hEdit,
                WM_SETFONT,
                (WPARAM)hfDefault,
                MAKELPARAM(FALSE,0));
            SendMessage(hEdit,
                WM_SETTEXT,
                NULL,
                (LPARAM)"LIGHT HACK by: MANEGARD");

            // LIGHT HACK BUTTON
            HWND hWndButton=CreateWindowEx(NULL,
                "BUTTON",
                "START LIGHT HACK SCAN",
                WS_TABSTOP|WS_VISIBLE|
                WS_CHILD|BS_DEFPUSHBUTTON,
                50, // closer to the left side of window x paramter
                10,  // y parameter
                300, //width of field
                24, // height of field
                hWnd,
                (HMENU)IDC_MAIN_BUTTON,
                GetModuleHandle(NULL),
                NULL);
                // HWND pointer to button
                phwnd = &hWndButton;

                //POISON DISEASE ButtON
        HWND hWndButton2=CreateWindowEx(NULL,
        "BUTTON",
        "Autocure ON-OFF",
        WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON,
        120,
        50,
        150,
        50,
        hWnd,
        (HMENU)IDC_MAIN_BUTTON2,
        GetModuleHandle(NULL),
        NULL);

        }
        break;

        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case IDC_MAIN_BUTTON:
                {

                 //////////LIGHT HACK///////////////////////////
   hwndDC = FindWindow(NULL,"Dransik (c)opyright 2013 Iron Will Games, LLC. ");
   MessageBox(hWnd,"STARTING LIGHT HACK","SCANNER",0);
   unsigned long pID;
   GetWindowThreadProcessId(hwndDC,&pID);
   HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE,pID);
   int x = 2207289488u;
   unsigned int x2;
   ofstream out;
   out.open("c:\\timer.txt");
   out<<3444;
   out.close();
   int i = 0;
   for(;;)
   {

    i++;
    //MessageBox(hWnd,"FOUND IT","LIGHT HACK",0);
    DWORD LH = 0x0000000+i;
    ReadProcessMemory(hProcess, (LPVOID)LH, &x2, 4, NULL);
    if(x2 == 2198867081u)
            {

     out.open("c:\\timer.txt");
     out<<"1";
     out.close();
     WriteProcessMemory(hProcess, (LPVOID)LH, &x, 4, NULL);
     Beep(1000,1000);
     Beep(1000,1000);Beep(1000,1000);Beep(1000,1000);Beep(1000,1000);
     MessageBox(hWnd,"FOUND IT","LIGHT HACK",0);
    break;
            };


    }


                };
                break;

                case IDC_MAIN_BUTTON2:
                    {
                    TurnOffReuse();
                    for(;;)
                    {
                     CheckBody();
                     CheckChatBox();
                    };
                    };
                    break;
            };
            break;

        case WM_DESTROY:
        {
            PostQuitMessage(0);
            return 0;
        };
        break;
    };

    return DefWindowProc(hWnd,msg,wParam,lParam);
};

Edited 3 Years Ago by CodyOebel

You need to add a message pump (see code below) within that loop so that the program can process messages. Same thing as lines 98-103 of the code you just posted.

Another way to do it is to run that loop in anothe thread so that your main program is not blocked while it is running.

MSG msg;
while(GetMessage(&msg, hwnd, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);

    // do stuff
}

Edited 3 Years Ago by Ancient Dragon

When I remove the code inside the button case it runs fine. If I remove the for(;;) it runs fine. When the for(;;) has met it's condition and breaks... it then runs fine as in it goes responsive again.
While the for loop is running it goes none-responding. So without using threading which I'm too novice to be programming stable threads. Is there something I can do to make it not hang while that for loop runs?

case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case IDC_MAIN_BUTTON:
                {

                 //////////LIGHT HACK///////////////////////////
   hwndDC = FindWindow(NULL,"Dransik (c)opyright 2013 Iron Will Games, LLC. ");
   MessageBox(hWnd,"STARTING LIGHT HACK","SCANNER",0);
   unsigned long pID;
   GetWindowThreadProcessId(hwndDC,&pID);
   HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE,pID);
   int x = 2207289488u;
   unsigned int x2;
   ofstream out;
   out.open("c:\\timer.txt");
   out<<3444;
   out.close();
   int i = 0;
   for(;;)
   {

    i++;
    //MessageBox(hWnd,"FOUND IT","LIGHT HACK",0);
    DWORD LH = 0x0000000+i;
    ReadProcessMemory(hProcess, (LPVOID)LH, &x2, 4, NULL);
    if(x2 == 2198867081u)
            {

     out.open("c:\\timer.txt");
     out<<"1";
     out.close();
     WriteProcessMemory(hProcess, (LPVOID)LH, &x, 4, NULL);
     Beep(1000,1000);
     Beep(1000,1000);Beep(1000,1000);Beep(1000,1000);Beep(1000,1000);
     MessageBox(hWnd,"FOUND IT","LIGHT HACK",0);
    break;
            };


    }


                };

** I JUST LOVE SEEING YOUR REPLIES ANCIENT DRAGON :) **

Thanks for the information.. I tried adding the message loop, but I did
it differently let me try what your suggesting, and see how it goes. I'm not too savvy with threads, the last ones I coded seemed to fail every now and then out of no where.

The loop should look something like this: You might want to put that message pump in a separate function so that it can be called from anywhere in your program without duplicating the code.

for(;;)
{
    MSG msg;
    while(GetMessage(&msg, hwnd, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    // rest of loop goes here
}

Edited 3 Years Ago by Ancient Dragon

This article has been dead for over six months. Start a new discussion instead.