Programm aus einem Programm hersus starten. Wie geht das?



  • Hi,

    ich wollt ein Programm schreiben, in dem ich ein anders Programm starte. Wie geht das? das Programm liegt z.B. in C:\Test\testprogramm.exe
    Kann mir jemand helfen?

    Danke im Vorraus



  • system("c:\\Test\\testprogramm.exe");
    


  • ZuK, du hast da aber den Rest verschwiegen. system() sollte man nicht verwenden,
    sondern auf Betriebsystemspezifische Funktionen zurückgreifen. Das sollte irgendwo
    in den FAQ stehen, ansonsten benutz Google oder MSDN.

    Gruß



  • Wenn es für Windows sein soll...
    Mal folgendes suchen:

    ShellExecute oder CreateProcess für die Anspruchsvollen 🤡



  • Hallo,

    ich habe zu diesem Thema ein bischen gegooglt und folgendes gefunden:
    *******************************************************************************
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <windows.h>
    using namespace std;

    string FullPathToExe;
    string Parameters;

    int ExecuteProcess(string FullPathToExe, string Parameters, int SecondsToWait);
    string getExeName(string strFullPathToExe);

    int main()
    {
    ExecuteProcess("notepad.exe","test.txt",5);

    return 0;
    }
    int ExecuteProcess(string &FullPathToExe, string &Parameters, int SecondsToWait)
    {
    int iMyCounter = 0, iReturnVal = 0;
    DWORD dwExitCode;

    /* - NOTE - You could check here to see if the exe even exists */

    /* Add a space to the beginning of the Parameters */
    if (Parameters.size() != 0 )
    {
    Parameters.insert(0," ");
    }
    /* When using CreateProcess the first parameter needs to be the exe itself /
    Parameters = getExeName(FullPathToExe).append(Parameters);
    /

    The second parameter to CreateProcess can not be anything but a char !!
    Since we are wrapping this C function with strings, we will create
    the needed memory to hold the parameters
    */
    /* Dynamic Char */
    char * pszParam = new char[Parameters.size() + 1];

    /* Verify memory availability */
    if (pszParam == 0)
    {
    /* Unable to obtain (allocate) memory /
    return 1;
    }
    const char
    pchrTemp = Parameters.c_str();
    strcpy(pszParam, pchrTemp);

    /* CreateProcess API initialization */
    STARTUPINFO siStartupInfo;
    PROCESS_INFORMATION piProcessInfo;
    memset(&siStartupInfo, 0, sizeof(siStartupInfo));
    memset(&piProcessInfo, 0, sizeof(piProcessInfo));
    siStartupInfo.cb = sizeof(siStartupInfo);

    /* Execute */
    if (CreateProcess(FullPathToExe.c_str(), pszParam, 0, 0, false,
    CREATE_DEFAULT_ERROR_MODE, 0, 0, &siStartupInfo,
    &piProcessInfo) != false)
    {
    /* A loop to watch the process. Dismissed with SecondsToWait set to 0 */
    GetExitCodeProcess(piProcessInfo.hProcess, &dwExitCode);

    while (dwExitCode == STILL_ACTIVE && SecondsToWait != 0)
    {
    GetExitCodeProcess(piProcessInfo.hProcess, &dwExitCode);
    Sleep(500);
    iMyCounter += 500;

    if (iMyCounter > (SecondsToWait * 1000))
    {
    dwExitCode = 0;
    }
    }
    }
    else
    {
    /* CreateProcess failed. You could also set the return to GetLastError() */
    iReturnVal = 2;
    }
    /* Release handles */
    CloseHandle(piProcessInfo.hProcess);
    CloseHandle(piProcessInfo.hThread);
    /* Free memory */
    delete[]pszParam;
    pszParam = 0;

    return iReturnVal;
    }

    string getExeName(string strFullPathToExe)
    {
    int Position = strFullPathToExe.find_last_of("\");
    strFullPathToExe = strFullPathToExe.erase(0, Position +1);

    return strFullPathToExe;
    }
    *******************************************************************************

    ich bekomme mit Dev-C++ folgenden Fehler:

    [Linker error] undefined reference to `ExecuteProcess(std::string, std::string, int)'

    kann mir bitte jemand sagen wieso???

    Gruß



  • Dieser Thread wurde von Moderator/in HumeSikkins aus dem Forum C++ in das Forum Compiler-Forum verschoben.

    Im Zweifelsfall bitte auch folgende Hinweise beachten:
    C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?

    Dieses Posting wurde automatisch erzeugt.


Anmelden zum Antworten