Externe Anwendung mit Parameter



  • Aus der MSDN unter ShellExecute:

    HINSTANCE ShellExecute(
        HWND hwnd, 
        LPCTSTR lpOperation,
        LPCTSTR lpFile, 
        LPCTSTR lpParameters, 
        LPCTSTR lpDirectory,
        INT nShowCmd
    );
    

    Du benutzt diese Funktion, um C:\WINDOWS\cdimage.exe zu starten und übergibts damit auch die Parameter. Wenn ich die MSDN richtig verstanden habe:

    ShellExecute(NULL, "open", "cdimage.exe", "deine Parameter", "C:\\WINDOWS\\", SW_SHOW);
    

    Am besten aber wird es sein, wenn du dir das ganze nochmal selbst anschaust und nachliest. Dabei lernt man am meisten 🙂

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/functions/shellexecute.asp

    MfG,

    42th



  • Das hilft mir weiter, allerdings bekomme ich ne fehlermeldung, dass ShellExecute undeklariert sei, müssen evtl. noch irgentwelche Header hinzugefügt werden?

    gruß Rad



  • windows.h eingebunden?!



  • jo das Problem hab ich nu gelöst und schon taucht ein neues auf:
    Der Parameter zum Aufruf soll char sein, alerdings gebe isch ihm string, wie Konvertiere ich den Typ nach char??

    gruß Rad

    ps:

    40 D:\Dev-Cpp\main.cpp cannot convert `std::string' to `const CHAR*' for argument `4' to `HINSTANCE__* ShellExecuteA(HWND__*, const CHAR*, const CHAR*, const CHAR*, const CHAR*, INT)'
    


  • Indem du die Member-Methode c_str() aufrufst 🙂



  • ich komm einfach net weiter.
    Ich poste mal den SourceCode in der Hoffnung einen Lösung zu finden:

    #include <cstdlib>
    #include <iostream>
    #include <string>
    #include <math.h>
    #include <windows.h>
    //#include <shellapi.h>
    #include <stdio.h>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        string volumelabel;
        string sourceroot;
        string isofile;
        char now;
    
        cout << "Name der Disc? (Keine Spaces!): "; cin >> volumelabel; cout << endl;    
    
        cout << "Sicherungspfad, muss existieren (keine Spaces!): "; cin >> sourceroot; cout << endl;    
    
        cout << "Zieldatei (ohne Endung!): "; cin >> isofile; cout << endl;
    
        isofile = isofile + ".iso";    
    
        cout << "Daten:" << endl << "\tLabel:\t\t\t" << volumelabel << endl;
    
        cout << "\tSicherungspfad:\t\t" << sourceroot << endl;
    
        cout << "\tZieldatei:\t\t" << isofile << endl << endl;    
    
        cout << "Starten?" << endl << "(Y|N) ";    
    
        cin >> now;    
    
        if (now == 'y' || now == 'Y') {
    
           string parameter;
    
           parameter = "-l" + volumelabel + " -h -n -d -k -m " + sourceroot + " " + isofile;
    
           cout << parameter << endl;       
    
           ShellExecute(NULL, "open", "cdimage.exe", parameter, "C:\\WINDOWS\\", SW_SHOW);
    
        }
    
        system("PAUSE");
    
        return EXIT_SUCCESS;
    }
    

    gruß Rad



  • Wenn dann brauchst du char-Arrays. Aber du kannst auch einfach weiterhin String benutzen und dann wenn du einen char-Pointer brauchst einfach c_str() aufrufen.

    String sourceroot;
    // [...]
    // dort wo ein char* verlangt wird dann einfach sourceroot.c_str()
    

    Hoffe, ich verzapfe keinen Mist, sollte aber zumindest so in der Art gehen 🤡



  • Nein leider funktioniert das nicht.

    jetziger SourceCode (einfach mal testen?):

    #include <cstdlib>
    #include <iostream>
    #include <string>
    #include <math.h>
    #include <windows.h>
    //#include <shellapi.h>
    #include <stdio.h>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        string volumelabel;
        string sourceroot;
        string isofile;
        char now;
    
        cout << "Name der Disc? (Keine Spaces!): "; cin >> volumelabel; cout << endl;    
    
        cout << "Sicherungspfad, muss existieren (keine Spaces!): "; cin >> sourceroot; cout << endl;    
    
        cout << "Zieldatei (ohne Endung!): "; cin >> isofile; cout << endl;
    
        isofile = isofile + ".iso";    
    
        cout << "Daten:" << endl << "\tLabel:\t\t\t" << volumelabel << endl;
    
        cout << "\tSicherungspfad:\t\t" << sourceroot << endl;
    
        cout << "\tZieldatei:\t\t" << isofile << endl << endl;    
    
        cout << "Starten?" << endl << "(Y|N) ";    
    
        cin >> now;    
    
        if (now == 'y' || now == 'Y') {
    
           string parameter;
    
           parameter = "-l" + volumelabel + " -h -n -d -k -m " + sourceroot + " " + isofile;
    
           cout << parameter << endl;       
    
           parameter.c_str();
    
           ShellExecute(NULL, "open", "cdimage.exe", parameter, "C:\\WINDOWS\\", SW_SHOW);
    
        }
    
        system("PAUSE");
    
        return EXIT_SUCCESS;
    }
    

    Die dazugehörige Fehlermedlung:

    48 D:\Dev-Cpp\main.cpp cannot convert `std::string' to `const CHAR*' for argument `4' to `HINSTANCE__* ShellExecuteA(HWND__, const CHAR, const CHAR*, const CHAR*, const CHAR*, INT)'



  • Nicht

    parameter.c_str();
    ShellExecute(NULL, "open", "cdimage.exe", parameter, "C:\\WINDOWS\\", SW_SHOW);
    

    sondern

    ShellExecute(NULL, "open", "cdimage.exe", parameter.c_str(), "C:\\WINDOWS\\", SW_SHOW);
    

    hatte ich gemeint 😉



  • Danke, klappt perfekt!!!



  • 🙂

    Ich wollte noch was hinzufügen, fürs allgemeine, weil ich in der richtung immer zurecht gestutzt wurde ;):

    Man sollte den Ausgabepuffer zu wenig wie möglich flushen. Demnach lieber

    std::cout << '\n';
    

    als

    std::cout << endl;
    

    Korrigiert mich, wenn ich falsch liege.

    MfG,

    42th


Anmelden zum Antworten