Kommandozeilen Parameter Übergabe an Applikation



  • Hallo,

    ich möchte über den Sheduler von Windows eine Routine meines MFC-Programms automatisch starten ohne das ich den Start-Button betätigen muss.

    Auf dringende Hilfe wär ich sehr dankbar.

    MfG

    Karl



  • tach...

    Du brauchst nur am Anfang (InitInstance / OnInitDialog) den Startparameter __argc abfragen und dann in __argv nachgucken...
    ungefähr so:

    if ((__argc == 2) && (!strncmp(__argv[1], "Dein Parameterwert")))
    {
        DeineFunktion();
    }
    

    Ich hoffe, dass ist verständlich.... 🙂



  • Hallo,

    erstmal vielen Dank.

    Aber wie bekomme ich diese beiden Parameter in Onitdialog rein. Im Debug-Modus erhalte ich die Meldung bei den Variablen das diese Symbole nicht gefunden werden können.

    Wie übergebe ich über den Sheduler die Parameter ?

    MfG



  • Versuchs mal mit GetProfileString oder GetPrivateProfileString.
    Ich bastle auch grad an sowas und meine Kollegen meinten damit würde es gehen. Habs aber selber noch nicht probiert.

    Teddy



  • Alles Klar danke erstmal für deine Hilfe

    MfG



  • Get(Private)ProfileString ist für das auslesen von Ini Dateien gedacht.

    Eventuell hilft Dir GetCommandLine mehr.

    GetCommandLine
    The GetCommandLine function retrieves the command-line string for the current process.

    LPTSTR GetCommandLine(VOID);
    Parameters
    This function has no parameters.

    Return Values
    The return value is a pointer to the command-line string for the current process.

    Remarks
    ANSI console processes written in C can use the argc and argv arguments of the main function to access the command-line arguments. ANSI GUI applications can use the lpCmdLine parameter of the WinMain function to access the command-line string, excluding the program name. The reason that main and WinMain cannot return Unicode strings is that argc, argv, and lpCmdLine use the LPSTR data type for parameters, not the LPTSTR data type. The GetCommandLine function can be used to access Unicode strings, because it uses the LPTSTR data type.

    To convert the command line to an argv style array of strings, call the CommandLineToArgvW function.

    Note The name of the executable in the command line that the operating system provides to a process is not necessarily identical to that in the command line that the calling process gives to the CreateProcess function. The operating system may prepend a fully qualified path to an executable name that is provided without a fully qualified path.

    Requirements
    Windows NT/2000/XP: Included in Windows NT 3.1 and later.
    Windows 95/98/Me: Included in Windows 95 and later.
    Header: Declared in Winbase.h; include Windows.h.
    Library: Use Kernel32.lib.
    Unicode: Implemented as Unicode and ANSI versions on all platforms.



  • Hallo hier hab ich die Lösung. Besonderer Vorteil sehr flexibel, denn Du kannst auch als Parameter eine Zeichenkette eingeben.

    *************************** Quellcode ******************************************
    z.B. Als Parameterübergabe im Sheduler "C:\Test\test.exe" /show
    Bekommt man im Programm als "C:\Test\test.exe /show" im Debugmodus geliefert über GetCommandLine.

    eigentlicher Quellcode in OnInitDialog:
    ---------------------------------------
    CString temp_cmdLine, cmdLine = GetCommandLine();

    int pos = cmdLine.Find( '/'); // letztes '/' finden
    temp_cmdLine = cmdLine.Left(pos); // Anzahl der Zeichen bis zum '/' Zeichen
    cmdLine.Delete(0,pos+1); // Pfad abtrennen incl. '/' zeichen

    if(cmdLine == "show")
    ShowWindow(); // Beispiel als Funktionsaufruf

    vielen Dank nochmal für die Hilfe

    MfG



  • aber was, wenn kein Argument angegeben wird? Dann hab ich kein / und somit dürfte es doch hier

    int pos = cmdLine.Find( '/'); // letztes '/' finden
    temp_cmdLine = cmdLine.Left(pos); // Anzahl der Zeichen bis zum '/' Zeichen
    cmdLine.Delete(0,pos+1); // Pfad abtrennen incl. '/' zeichen
    

    knallen, oder?

    Teddy



  • Hallo,

    den Parameter gibt man im Scheduler direkt nach den / an. Wenn man keinen Parameter angibt, dann braucht man nur in der Routine abfragen ob nachfolgend ein Parameter erwartet wird oder nicht (mit einer if-else-Anweisung) schon ist das Problem behoben :-).

    Der Knackpunjt des ganzen ist das man einen String als Parameter übergeben kann. Wie dann nun jeder mit dem weiterarbeiten will, hängt ganz vom Wunsch des Programmierers ab.

    In der MSDN-Library steht für Find von CString

    The zero-based index of the first character in this CString object that matches the requested substring or characters; -1 if the substring or character is not found.

    Also kann man diese -1 abfangen und auswerten im Programm.

    MfG


Anmelden zum Antworten