Console Do in Do - schleife & Programm ausführen mit Parameter?!



  • Hallo Community 🙂
    Ich habe seid ein paar Tagen mit C++ angefangen und ich muss sagen es gefällt mir 🙂
    Frage1:
    Ich wollte ein Programm schreiben was in der Console ein Programm starten kann, dies in einer Do-Schleife.
    Ich weiß jetzt aber nicht, wie ich es machen kann das wenn man case2 ausgewählt hat , entweder wieder von neu anfängt oder eine neue Abfrage.

    Frage2:
    Bei Case1: soll das Programm gestartet werden ,was im gleichen Ordner ist.
    Den Befehl für den Ordner wo die C++ anwendung drin ist habe ich schon, aber ich wüsste nicht wie ich es mit ShellExecute machen könnte.
    Das Programm was ich noch starten will sollte mit den Parametern "22 22" gestartet werden (:

    Mit freundlichen Grüßen

    Ich wäre sehr glücklich wenn mir jemand helfen könnte 🙂

    // main.cpp : Defines the entry point for the console application.
    //
    #include "stdafx.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    # include<shellapi.h> 
    #include <direct.h> // for getcwd
    #include <conio.h>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    // function to return the current working directory
    // this is generally the application path
    void GetCurrentPath(char* buffer)
    {
    getcwd(buffer, _MAX_PATH);
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {	
    	/*Variablen & String's !
    	int Zahl = 300 ; 
    	cout << Zahl ;
    	//*************************
    	string Vorname;
    	cout << "Vornamen eingeben: " ;
        cin  >> Vorname;
    	cout << "\n\n\n" << "Hallo " << Vorname << "!" ;*/
    
    	// Allgemein
    	int x;
    	SetConsoleTitleA("Console by Alexander");
    	cout << "Programm Name\n" ;
    	cout << "******************************************************************\n\n" ;
    	cout << "Welcome, don't forget to make a donation if you like this tool.\n" ;
    	cout << "This tool is free DO NOT SELL IT ON EBAY!!\n" ;
    	cout << "Write me an E-mail at  info[at]the-franky.de\n\n" ;
    	cout << "******************************************************************\n" ;
    	cout << "Do you want so start it ?\n\n";
    	cout << "1: Starting the Programm !\n";
    	cout << "2: Your Help !\n";
    	cout << "0: Exit.\n\n";
    
    	do
    		{
    		cout << "Please Select: ";
    		cin >> x;
    		}while((x<0)||(x>2)); // x != 3
    
    	switch(x)
    		{
    			case 1: system("cls");	
    				// _MAX_PATH is the maximum length allowed for a path
    				char CurrentPath[_MAX_PATH];
    				// use the function to get the path
    				GetCurrentPath(CurrentPath);
    				// display the path for demo purposes only
    				char temp[_MAX_PATH];
    				cout << CurrentPath << endl;
    				cin.getline(temp,_MAX_PATH);
    				cout << "\n******************************************************************\n" ;
    				cout << "              Warning the programm is now running !" ;
    				cout << "\n******************************************************************\n" ;
    				cout << "\nSuccessfully.\n" ;
    				cout << "\nPush any Key to EXIT the Programm, be carefull.";
    				// 
    				ShellExecute(
    				NULL,
    				_T("open"),                     
    				_T("Programm.exe"), // Ist im gleichen Ordner
    				_T("22 22"), // Programm mit 22 22 ausführen                          
    				_T(" C:\ "), 
    				SW_SHOW);
    				break ;
    			case 2: system("cls");
    					cout << "******************************************************************" << endl;
    					cout << "                            Your Help !" << endl;
    					cout << "******************************************************************" << endl;
    					cout << "Write an E-mail with 'Help Me' at  info[at]the-franky.de" << endl;
    					cout << "Thank you for your understanding\n" << endl;
    					cout << "******************************************************************\n\n" ;
    					cout << "Do you want so start it now?\n\n";
    					cout << "1: Starting the Programm !\n";
    					cout << "0: Exit.\n\n";
    					int y;
    						do
    						{
    							cout << "Please Select: ";
    							cin >> y;
    						}while((x<0)||(x>1));
    							switch(y)
    						{
    							case 0:
    								cout << "DIE NULL";
    							case 1:
    								cout << "DIE EINS";
    						}
    			case 0: exit (1);		
    		}	
    	//system("pause");
    	_getch();
    	return 0;
    }
    


  • 0x33 schrieb:

    Hallo Community 🙂
    Ich habe seid ein paar Tagen mit C++ angefangen und ich muss sagen es gefällt mir 🙂
    Frage1:
    Ich wollte ein Programm schreiben was in der Console ein Programm starten kann, dies in einer Do-Schleife.
    Ich weiß jetzt aber nicht, wie ich es machen kann das wenn man case2 ausgewählt hat , entweder wieder von neu anfängt oder eine neue Abfrage.

    do {
    ...
    } while (x);
    

    0x33 schrieb:

    Frage2:
    Bei Case1: soll das Programm gestartet werden ,was im gleichen Ordner ist.
    Den Befehl für den Ordner wo die C++ anwendung drin ist habe ich schon, aber ich wüsste nicht wie ich es mit ShellExecute machen könnte.
    Das Programm was ich noch starten will sollte mit den Parametern "22 22" gestartet werden (:

    Mit freundlichen Grüßen

    Ich wäre sehr glücklich wenn mir jemand helfen könnte 🙂

    Warum übergibst du dann für lpDirectory "C:\"?
    Das mal durchlesen: http://msdn.microsoft.com/en-us/library/bb762153%28v=vs.85%29.aspx

    Eigenartig, dass du dich gleich auf die WinAPI stürzt anstatt erstmal die Grundlagen von C++ zu lernen.



  • Hey erstmal danke für die antwort 🙂

    Naja Zu Frage1 , ich habe ja

    Do {
    ...
    } while (x);
    

    Nur Bei Case2: Habe ich dort nochmal eine Do-Schleife die nicht funktioniert.
    Warum ?

    Regards



  • 0x33 schrieb:

    int y;
    do
    {
        cout << "Please Select: ";
        cin >> y;
    }while((x<0)||(x>1));
    

    Es sollte wahrscheinlich while ((y<0) || (y>1)) heissen.



  • Ich habs geändert zu 'while ((y<0) || (y>1)' , dass programm schmiert trozdem ab.
    Hier mal der jetzige Code.

    Mit freundlichen Grüßen

    // ipod.cpp : Defines the entry point for the console application.
    //
    #include "stdafx.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    # include<shellapi.h> 
    #include <direct.h> // for getcwd
    #include <conio.h>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    // function to return the current working directory
    // this is generally the application path
    void GetCurrentPath(char* buffer)
    {
    getcwd(buffer, _MAX_PATH);
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {	
    	/*Variablen & String's !
    	int Zahl = 300 ; 
    	cout << Zahl ;
    	//*************************
    	string Vorname;
    	cout << "Vornamen eingeben: " ;
        cin  >> Vorname;
    	cout << "\n\n\n" << "Hallo " << Vorname << "!" ;*/
    
    	// Allgemein
    	int x;
    	SetConsoleTitleA("Console by Alexander");
    	cout << "Programm Name\n" ;
    	cout << "******************************************************************\n\n" ;
    	cout << "Welcome, don't forget to make a donation if you like this tool.\n" ;
    	cout << "This tool is free DO NOT SELL IT ON EBAY!!\n" ;
    	cout << "Write me an E-mail at  info[at]the-franky.de\n\n" ;
    	cout << "******************************************************************\n" ;
    	cout << "Do you want so start it ?\n\n";
    	cout << "1: Starting the Programm !\n";
    	cout << "2: Your Help !\n";
    	cout << "0: Exit.\n\n";
    
    	do
    		{
    		cout << "Please Select: ";
    		cin >> x;
    		}while((x<0)||(x>2)); // x != 3
    
    	switch(x)
    		{
    			case 1: system("cls");	
    				// _MAX_PATH is the maximum length allowed for a path
    				char CurrentPath[_MAX_PATH];
    				// use the function to get the path
    				GetCurrentPath(CurrentPath);
    				// display the path for demo purposes only
    				char temp[_MAX_PATH];
    				cout << CurrentPath << endl;
    				cin.getline(temp,_MAX_PATH);
    				cout << "\n******************************************************************\n" ;
    				cout << "              Warning the programm is now running !" ;
    				cout << "\n******************************************************************\n" ;
    				cout << "\nSuccessfully.\n" ;
    				cout << "\nPush any Key to EXIT the Programm, be carefull.";
    				// 
    				ShellExecute(
    				NULL,
    				_T("open"),                     
    				_T("Programm.exe"), // Ist im gleichen Ordner
    				_T("22 22"), // Programm mit 22 22 ausführen                          
    				_T(" C:\ "), 
    				SW_SHOW);
    				break ;
    			case 2: system("cls");
    					cout << "******************************************************************" << endl;
    					cout << "                            Your Help !" << endl;
    					cout << "******************************************************************" << endl;
    					cout << "Write an E-mail with 'Help Me' at  info[at]the-franky.de" << endl;
    					cout << "Thank you for your understanding\n" << endl;
    					cout << "******************************************************************\n\n" ;
    					cout << "Do you want so start it now?\n\n";
    					cout << "1: Starting the Programm !\n";
    					cout << "0: Exit.\n\n";
    					int y;
    						do
    						{
    							cout << "Please Select: ";
    							cin >> y;
    						}while((y<0)||(y>1));
    							switch(y)
    						{
    							case 0:
    								cout << "DIE NULL";; break;
    							case 1:
    								cout << "DIE EINS";; break;
    						}
    			case 0: exit (1);		
    		}	
    	//system("pause");
    	_getch();
    	return 0;
    }
    


  • die schleife funktioniert, aber für die case anweisung fehlt noch ein break, denn ohne das break wird sofort zum nächsten case gesprungen und da dieser exit ist "schmiert" das programm ab

    do
                            {
                                cout << "Please Select: ";
                                cin >> y;
                            }while((y<0)||(y>1));
                                switch(y)
                            {
                                case 0:
                                    cout << "DIE NULL";; break;
                                case 1:
                                    cout << "DIE EINS";; break;
                            }
                            break; // hier fehlts
                case 0: exit (1);
    


  • Hallo
    Ein riesen Dankeschön , es klappt wunderbar 🙂
    Das mit Shellexecute habe ich nun auch geschafft (=

    Nur wenn sich mein Programm beenden , beendet sich "Das andere Programm" nicht.
    Wie könnte ich machen , dass es sich beendet?

    Ich habe es schon mit ...

    HWND spiel=FindWindow(0, TEXT("Window Title"));
    SendMessage(spiel, WM_CLOSE, 0, 0);
    

    probiert, da es aber eine CMD anwendung ist, ist der Title = Der Pfad, klappte nicht !

    So habe ich es auch probiert...

    void GetCurrentPath(char* buffer)
    {
    getcwd(buffer, _MAX_PATH);
    }
    // _MAX_PATH is the maximum length allowed for a path
    char CurrentPath[_MAX_PATH];
    // use the function to get the path
    GetCurrentPath(CurrentPath);
    // display the path for demo purposes only
    char temp[_MAX_PATH];
    cout << CurrentPath << endl;
    cin.getline(temp,_MAX_PATH);
    
    HWND spiel=FindWindow(0, TEXT(CurrentPath));
    SendMessage(spiel, WM_CLOSE, 0, 0);
    

    klappt aber auch nicht.

    Frage: Wie könnte ich es machen , dass sich das externe CMD Programm sich sofort beendet , wenn sich mein programm beendet ?

    Regards



  • du öffnest mit deinem programm nur ein anderes und es besteht keine abhängigkeit.

    hier ein beispiel wie man es mit der WinAPI machen kann:
    http://www.tutorials.de/c-c/181021-windows-prozess-killen-mit-c.html



  • Danke für den Link, es klappt aber nicht.
    Hier mal der Code 🙂

    // ipod.cpp : Defines the entry point for the console application.
    //
    #include "stdafx.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <tlhelp32.h>
    #include <windows.h>
    # include<shellapi.h> 
    #include <direct.h> // for getcwd
    #include <conio.h>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    // function to return the current working directory
    // this is generally the application path
    void GetCurrentPath(char* buffer)
    {
    getcwd(buffer, _MAX_PATH);
    }
    void printError( TCHAR* msg );
    BOOL KillProcessByName(char *szProcessToKill);
    
    int _tmain(int argc, _TCHAR* argv[])
    {	
    	/*Variablen & String's !
    	int Zahl = 300 ; 
    	cout << Zahl ;
    	//*************************
    	string Vorname;
    	cout << "Vornamen eingeben: " ;
        cin  >> Vorname;
    	cout << "\n\n\n" << "Hallo " << Vorname << "!" ;*/
    
    	// Allgemein
    	int x;
    	SetConsoleTitleA("Console by Alexander");
    	cout << "Programm Name\n" ;
    	cout << "******************************************************************\n\n" ;
    	cout << "Welcome, don't forget to make a donation if you like this tool.\n" ;
    	cout << "This tool is free DO NOT SELL IT ON EBAY!!\n" ;
    	cout << "Write me an E-mail at  info[at]the-franky.de\n\n" ;
    	cout << "******************************************************************\n" ;
    	cout << "Do you want so start it ?\n\n";
    	cout << "1: Starting the Programm !\n";
    	cout << "2: Your Help !\n";
    	cout << "0: Exit.\n\n";
    
    	do
    		{
    		cout << "Please Select: ";
    		cin >> x;
    		}while((x<0)||(x>2)); // x != 3
    
    	switch(x)
    		{
    			case 1: system("cls");	
    				// _MAX_PATH is the maximum length allowed for a path
    				char CurrentPath[_MAX_PATH];
    				// use the function to get the path
    				GetCurrentPath(CurrentPath);
    				// display the path for demo purposes only
    				char temp[_MAX_PATH];
    				cout << CurrentPath << endl;
    				cin.getline(temp,_MAX_PATH);
    				cout << "\n******************************************************************\n" ;
    				cout << "              Warning the programm is now running !" ;
    				cout << "\n******************************************************************\n" ;
    				cout << "\nSuccessfully.\n" ;
    				cout << "\nPush any Key to EXIT the Programm, be carefull.";
    				// 
    				ShellExecute(
    				NULL,
    				_T("open"),                     
    				_T("Externes Programm.exe"), // Ist im gleichen Ordner
    				_T("22 22"), // Programm mit 22 22 ausführen                          
    				NULL, 
    				SW_MINIMIZE);
    				break ;
    			case 2: system("cls");
    					cout << "******************************************************************" << endl;
    					cout << "                            Your Help !" << endl;
    					cout << "******************************************************************" << endl;
    					cout << "Write an E-mail with 'Help Me' at  info[at]the-franky.de" << endl;
    					cout << "Thank you for your understanding\n" << endl;
    					cout << "******************************************************************\n\n" ;
    					cout << "Do you want so start it now?\n\n";
    					cout << "1: Starting the Programm !\n";
    					cout << "0: Exit.\n\n";
    					int y;
    						do
    						{
    							cout << "Please Select: ";
    							cin >> y;
    						}while((y<0)||(y>1));
    							switch(y)
    						{
    							case 0:
    								cout << "DIE NULL";; break;
    							case 1:
    								cout << "DIE EINS";; break;
    						}
    							break ; // Für die andere Schleife
    			case 0: exit (1);		
    		}	
    	//system("pause");
    	KillProcessByName("Externes Programm.exe");      // Anstatt Icq.exe halt "Test.exe" eingeben!
    	_getch();
    	return 0;
    }
    
    BOOL KillProcessByName(char *szProcessToKill)
    {
    HANDLE hProcessSnap;
    HANDLE hProcess;
    PROCESSENTRY32 pe32;
    DWORD dwPriorityClass;
    
    hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
    
    if( hProcessSnap == INVALID_HANDLE_VALUE )
      {
        printError( "CreateToolhelp32Snapshot (of processes)" );
        return( FALSE );
      }
    
    pe32.dwSize = sizeof( PROCESSENTRY32 );
    
    if( !Process32First( hProcessSnap, &pe32 ) )
      {
        printError( "Process32First" );  
        CloseHandle( hProcessSnap );     
        return( FALSE );
      }
    
    do{
        if(!strcmp(pe32.szExeFile,szProcessToKill))
        {
        printf("Prozess: %s \n",pe32.szExeFile);
        printf("PID: %d \n",pe32.th32ProcessID );  
        hProcess = OpenProcess(PROCESS_TERMINATE,0, pe32.th32ProcessID);
        TerminateProcess(hProcess,0);
        CloseHandle(hProcess);
        } 
      } while( Process32Next(hProcessSnap,&pe32) );
    
    CloseHandle( hProcessSnap );
    return( TRUE );
    }
    
    void printError( TCHAR* msg )
    {
      DWORD eNum;
      TCHAR sysMsg[256];
      TCHAR* p;
    
      eNum = GetLastError( );
      FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
             NULL, eNum,
             MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
             sysMsg, 256, NULL );
    
      // Trim the end of the line and terminate it with a null
      p = sysMsg;
      while( ( *p > 31 ) || ( *p == 9 ) )
        ++p;
      do { *p-- = 0; } while( ( p >= sysMsg ) &&
                              ( ( *p == '.' ) || ( *p < 33 ) ) );
    
      // Display the message
      printf( "\n  WARNING: %s failed with error %d (%s)", msg, eNum, sysMsg );
    }
    

Anmelden zum Antworten