How can I close an application if I write it only using WinApi, without windows?

    5 answers 5

    PostMessage(handle,WM_QUIT,0,0); 

    UPD

    It can be even simpler:

     application.terminate; 
    • 3
      OP wanted:> no windows And it is completely incomprehensible that the mythical " application identifier ". - karmadro4
    • 2
      @ karmadro4, what kind of noob are you? Stop clinging to all people! Troll all right and left ... calm down. On this occasion, I’ll probably turn to adminintratsiyu. After all, there is no benefit from you on the forum, but the rating, by your antics, is unreasonably taken away. And, by the way, I’m re-telling you that you should learn WinApi, since you don’t even know what an "application identifier" is. - AseN
    • 3
      That is, the great guru , which brings hundreds of benefits to the forum, cannot clearly explain to the noob what an application identifier is ? And do not forget to write in Sportloto ;-)> unreasonably take away It's amazing where you get your rating from, if you write to the question with the mark ā€œI write only with the use of WinApiā€: ā€œapplication.terminateā€. - karmadro4
    • 3
      @ karmadro4 - what's the value of discussing it? And in general - if you can answer better, be so kind - answer. - Zowie
    • 2
      @Asen, if you type the name of the function PostMessage in Google, then suddenly it turns out that its first parameter is the identifier of the window, which is missing for the task condition :) - insolor
    • Nobody, by the way, mentioned a graceful way to exit the application (compared to ExitProcess ), which is to use the PostQuitMessage function .

    The difference between PostQuitMessage and PostQuitMessage is that the second method assumes a smoother output from the point of view of the Message Pump .

    The WM_QUIT message is virtual, which ideologically corresponds to a low priority message for a pull'a from the queue. From a practical point of view, this allows your application to process all non-virtual (that is, higher priority) messages before exiting the program.

    I did not encounter it in practice, but I think that potentially calling ExitProcess may well lead to visual artifacts and some other undesirable behavior'у .

    • A good article on the topic can be read here.

      The easiest

       ExitProcess(0) 

      Argument = 0, meaning "without error."

        You can even halt, but I do not advise to abuse it, because it is difficult to debug.

        • halt - not recommended ... bad style is considered. - AseN
        • As for the finalization, you are wrong, the call chain works out before the program ends. - karmadro4
        • > As for the finalization, you are wrong, the call chain works out before the program ends. Yes, you are right, I will correct the answer. > halt - not recommended ... bad style is considered. Bad, I agree, I did not write that good, even explained why the method is questionable. - AlexAndR
        • Personally, I see nothing wrong with Halt . You can probably offer something better, but it is difficult to do without seeing the code of this "WinAPI, no windows." Perhaps Break from the cycle is ideal - karmadro4

        Get the isRunning boolean variable and check it in the message loop. If it is false, then stop the loop. Thus, the program will end automatically. This will be more correct than calling ExitProcess, because all objects will be correctly deleted.