How not to end a proggy??

Posted on 2005-04-26
Last Modified: 2010-04-15

I made a win32 program :

#include "stdafx.h"

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
//my code.....
return 0;

How can I not end it (I know I can make that it never reaches the return by using an "untightened loop" like
But I want to do it more elegant...

Question by:lapijn
    LVL 55

    Expert Comment

    by:Jaime Olivares
    Please explain your intentions to give you the best alternative.....

    Author Comment

    Well, I want to make a program which stays "loaded", and when I press ALT+KEY it just calls  a WinExec with parameters I choosed.
    LVL 1

    Expert Comment

    You could not call a function to terminate the process after WM_CLOSE or WM_DESTROY.

    It is, however, possible to do this same thing (ALT+KEY starts a program) with windows. You can add a shortcut with the desired parameters to a program to your desktop or taskbar and set a hotkey.


    Author Comment

    thanks staurtdekip, but I don't call a function now either (see example in my question). I only have a return....
    I know it can be done in windows, but I'm just curious how I can do that....
    LVL 1

    Accepted Solution

    You just wrote
    // my code
    which does not exclude a process termination call.
    However, since you don't have this call, you probably don't have a GetMessage-loop either, which is the usual way of creating an indefinate loop.

    MSG msg;

    If you just don't respond to the quit messages, this is indefinate...

    ps. it is only fair to show your real name in your account, so that I can greet you too or call you old or something...  :)


    Author Comment

    Thanks a lot, Stuart. This seems to work indeed.

    What is the filosophy of it (I searched the functions on msdn, but I'm only a beginner ...) and what are the advantages over a "while(1){Sleep(1000);}"  ?

    PS. I've lost the hotmail adres with which I made this account long ago, so don't think I can change my profile :(

    Grtz, Hans Devroe.
    LVL 1

    Expert Comment

    The GetMessage function reads a message (obviously) and than it's send to a window in your process after you call DispatchMessage.
    These functions are the most common way to give input to your program.
    The while(1){Sleep(1000);} loop will cause the program to run indefinately, but you can't accept user input like ALT+KEY, in fact, the program will halt.

    BTW, you giving me a grade A for the answer is an acceptable substitute for me calling you Hans Hetvroe or something :)


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now