Restarter für zwei .exe



  • Hi Ich möchte gerne einen Restarter für 2 EXE Dateien schreiben.
    Das heißt, ganz am Anfang sollen beide starten und wenn eine geschlossen wird soll sie wieder gestartet werden.

    Ich hab c++ schon ne Weile in gebrauch, aber mit WinApi gehts gerade erst los...

    Hier der Code:

    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    
    /*  Make the class name into a global variable  */
    char szClassName[ ] = "CodeBlocksWindowsApp";
    
    int WINAPI WinMain (HINSTANCE hThisInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR lpszArgument,
                         int nCmdShow)
    {
        HWND hwnd;               /* This is the handle for our window */
        MSG messages;            /* Here messages to the application are saved */
        WNDCLASSEX wincl;        /* Data structure for the windowclass */
    
        /* The Window structure */
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
        wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
        wincl.cbSize = sizeof (WNDCLASSEX);
    
        /* Use default icon and mouse-pointer */
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;                 /* No menu */
        wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
        wincl.cbWndExtra = 0;                      /* structure or the window instance */
        /* Use Windows's default colour as the background of the window */
        wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    
        /* Register the window class, and if it fails quit the program */
        if (!RegisterClassEx (&wincl))
            return 0;
    
        /* The class is registered, let's create the program*/
        hwnd = CreateWindowEx (
               0,                   /* Extended possibilites for variation */
               szClassName,         /* Classname */
               "Philles Restarter",       /* Title Text */
               WS_OVERLAPPEDWINDOW, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               200,                 /* The programs width */
               200,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
               );
    
        /* Make the window visible on the screen */
        ShowWindow (hwnd, nCmdShow);
    
        /* Run the message loop. It will run until GetMessage() returns 0 */
        while (GetMessage (&messages, NULL, 0, 0))
        {
            /* Translate virtual-key messages into character messages */
            TranslateMessage(&messages);
            /* Send message to WindowProcedure */
            DispatchMessage(&messages);
        }
    
        /* The program return-value is 0 - The value that PostQuitMessage() gave */
        return messages.wParam;
    }
    
    /*  This function is called by the Windows function DispatchMessage()  */
    
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    static HWND hButton, hButton2, hButton3;
        switch (message)                  /* handle the messages */
        {
            case WM_CREATE:
            {
                hButton = CreateWindow(  "button",
                                      "Beenden",
                                      WS_CHILD | WS_VISIBLE,
                                      0, 0, 0, 0,
                                      hwnd,
                                      NULL,
                                      ((LPCREATESTRUCT) lParam) -> hInstance,
                                      NULL);
                hButton2 = CreateWindow(  "button",
                                      "2.exe",
                                      WS_CHILD | WS_VISIBLE,
                                      0, 0, 0, 0,
                                      hwnd,
                                      NULL,
                                      ((LPCREATESTRUCT) lParam) -> hInstance,
                                      NULL);
                hButton3 = CreateWindow(  "button",
                                      "1.exe",
                                      WS_CHILD | WS_VISIBLE,
                                      0, 0, 0, 0,
                                      hwnd,
                                      NULL,
                                      ((LPCREATESTRUCT) lParam) -> hInstance,
                                      NULL);
                return 0;
            }
            case WM_SIZE:
            {
                MoveWindow(hButton, 15, 120, 160, 25, TRUE);
                MoveWindow(hButton2, 15, 30, 160, 25, TRUE);
                MoveWindow(hButton3, 15, 70, 160, 25, TRUE);
                return 0;
            }
    
            case WM_COMMAND:
            {
                if (lParam == (LPARAM)hButton)
                {
                    if (HIWORD(wParam) == BN_CLICKED)
                    cout << "Beenden";
                    SendMessage(hwnd, WM_CLOSE, 0, 0);
                }
    
                if (lParam == (LPARAM)hButton2) {
                    if (HIWORD(wParam) == BN_CLICKED)
                        cout << "reamld.exe" << endl;
                        system("start 1.exe"); //Das hier muss irgendwie umgeändert werden
                }
    
                if (lParam == (LPARAM)hButton3) {
                    if (HIWORD(wParam) == BN_CLICKED)
                        cout << "mangosd.exe" << endl;
                        system("start 2.exe"); // Und das!
                }
                return 0;
            }
            case WM_DESTROY:
                PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
                break;
            default:                      /* for messages that we don't deal with */
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    
        return 0;
    }
    

    Ich hoffe jemand kann helfen

    MfG Phille



  • Virus?



  • Hallo Phille

    Zu dem Problem mit dem system..

    system("C:\\ordner\\nocheinordner\\blub\\deinedatei.exe");
    

    und zu dem neustarten :

    mach doch einfach eine endlosschleife (am besten mit sleep( >= 5000))
    die mit findwindow oder ähnlichen funktionen abfragt ob dein prozess noch läuft.

    if (!FindWindow(0, hierdernamedesfenstersdeinerexe))
    

    und dann startest du die einfach neu (mit system oder CreateProcess)

    Virus? schrieb:

    Virus?

    ich denke nicht dass das Neustarten eines Prozesses sehr nach einem Virus
    klingt. (Damit das Klappt müsste der virus ein fenster haben 😮 )



  • Phille könnte die beiden Exen auch einfach mit CreateProcess starten.

    da hat er dann auch gleich die Prozess-Handles und kann mit WaitForMultipleObjects
    auf das Beenden einer der beiden reagieren.
    hat den Vorteil, dass man diese Technik auch bei Zielanwendungen ohne Fenster nutzen kann.



  • Vielen dank...
    Ne is nicht für Virus... Warum sollte ich das in ne Aplication machen?
    Das ist für einen Server.

    Nochmal vielen dank

    Phille



  • Ich habs ein bisschen ausprobiert.
    Aber ich kanns nicht so richtig realisieren.
    Wenn die Endlosschleife läuft soll man ja auch auf einen Button drücken können...
    Wie soll ich das machen?



  • Threads!



  • @Phille: pass auf, dass Blizzard sich nich bei dir meldet 😉



  • Ich hab den Thread mal bei Blizzard gemeldet.



  • was soll das hier? ich weiss nicht was dümmer ist, der code mit dem system müll oder die ratschläge, die ihr hier erteilt. wie wärs mal mit CreateProcess und WaitForSingleObject. lol system, als nächstes kommt der eine hier wieder mit WinExec an oder wsa? 👎



  • hallo rofler



  • ! hey, ich hab gesagt CreateProcess und außerdem solltest du beachten, dass es 2 Programe sind,
    also bitte: WaitForMultipleObjects !!!



  • 9KTH9r comment3,


Anmelden zum Antworten