CreateProcess in Dienst/Service (war: Dienst, Service, die 2.)



  • Hallo Forum,

    habe wieder eine Frage. Der folgende Code funktioniert in Programmen mit grafischer Oberfläche einwandfrei. Diesen Code habe ich in meinem Dienst eingebaut. Er soll ein Kommandozeilenprogramm inklusive Parameter starten. Leider bringt er mir hier eine Fehlermeldung. GetLastError() liefert Fehler 0, GetExitCodeProcess liefert Fehler 1.

    Könnt ihr mir mal auf die Sprünge helfen? Was muß ich ändern, damit der folgende Code auch in einem Dienst funktioniert?

    Vielen Dank fue eure Antworten

    //Kommandozeilenprogramm inklusive Parameter
    asMsg = "d:\programme\borland\interbase\bin\gbak.exe  ";
    asMsg += "-backup -user XXXXXX -pa xxxxxxx -ignore -v -z ";
    asMsg += "e:\audamupsii_basis.gdb ";
    asMsg += "e:\audamupsii_basis.gbk ";
    asMsg += "-y e:\audamupsii_basis_backuplog.txt";
    
    CreateProcess(
      NULL,                                               	
      asMsg.c_str(),                                    	
      NULL,                                               	
      NULL,                                               	
      false,                                             	
      CREATE_DEFAULT_ERROR_MODE | NORMAL_PRIORITY_CLASS,	
      NULL,                                               	
      NULL,                                               	
      &stgStartupInfo,                                    	
      &stgProcessInfo);
    

    Edit:
    Bitte aussagekräftige Überschriften wählen. Danke!



  • Du solltest vielleicht mal alle "\" zu "\" ändern.



  • Hi F98,

    die doppelten Backslashes sind im Original drin. 🤡



  • 😉

    Und was steht in stgStartupInfo und stgProcessInfo?



  • int __fastcall TSparkyThread::StarteProgramm(AnsiString asProgramm, AnsiString asParameter,
    																				bool bEnvironment, int iCmdShow)
    {
    	AnsiString asMsg	= "";
    	bool bOrdner		= false;
    	DWORD ExitCode		= 1;					//für den ExitCode
    	int iUser    		= 0;					//Rückgabewert für Umgebungs-Variable isc_user
    	int iPassword		= 0;     			//Rückgabewert für Umgebungs-Variable isc_password
    	TStartupInfo stgStartupInfo;			//für CreateProcess()
    	TProcessInformation stgProcessInfo;	//für CreateProcess()
    
    	asProgramm	= LowerCase(asProgramm);
    	asParameter	= LowerCase(asParameter);
    
    	//iCmdShow steuert die Sichtbarbeit des Programms
    	//iCmdShow = SW_HIDE	:	verbirgt das Programm (isql, gbak)
    	//iCmdShow = SW_SHOW	:	zeigt das Programm (Comparer.exe, blob.exe, prjVorlagen.exe)
        setmem(&stgStartupInfo, sizeof(TStartupInfo), 0);
        stgStartupInfo.cb             = sizeof(TStartupInfo); //.cb  : Specifies the size,
                                                              // in bytes, of the structure.
        stgStartupInfo.dwFlags     = STARTF_USESHOWWINDOW;//.dwFlags  : This is a bit field
                                                          // that determines whether certain
                                                          // STARTUPINFO members are used when
                                                          // the process creates a window. Any
                                                          // combination of the following values
                                                          // can be specified:
        stgStartupInfo.wShowWindow    = iCmdShow; //.wShowWindow   : Ignored unless dwFlags
                                                  // specifies STARTF_USESHOWWINDOW.
                                                  // The wshowWindow member can be any of the
                                                  // SW_ constants defined in WINUSER.H.
                                                  // For GUI processes, wShowWindow specifies
                                                  // the default value the first time ShowWindow
                                                  // is called.
                                                  // The nCmdShow parameter of ShowWindow is
                                                  // ignored. In subsequent calls to ShowWindow,
                                                  // the wShowWindow member is used if the
                                                  // nCmdShow parameter of ShowWindow is set to
                                                  // SW_SHOWDEFAULT.
    	//Auszuführendes Programm (Prozeß)
    	asMsg = asProgramm;
    	//Wenn Kommandozeilen-Parameter vorhanden sind, dann anhängen
    	if(asParameter != "")
    	{
    		asMsg += "  " + asParameter;
    	}
    
    	//Pfad einstellen, falls das Programm noch zusätzliche Dateien braucht
    	bOrdner = SetCurrentDir(ExtractFilePath(asProgramm));
    
    	CreateProcess(NULL,                                               //LPCTSTR lpApplicationName							//pointer to name of executable module
    					  asMsg.c_str(),                                    	//LPTSTR lpCommandLine									//pointer to command line string
    					  NULL,                                               //LPSECURITY_ATTRIBUTES lpProcessAttributes		//pointer to process security attributes
    					  NULL,                                               //LPSECURITY_ATTRIBUTES lpThreadAttributes		//pointer to thread security attributes
    					  false,                                             	//BOOL bInheritHandles									//handle inheritance flag
    					  CREATE_DEFAULT_ERROR_MODE | NORMAL_PRIORITY_CLASS,	//DWORD dwCreationFlags									//creation flags
    					  NULL,                                               //LPVOID lpEnvironment									//pointer to new environment block
    					  NULL,                                               //LPCTSTR lpCurrentDirectory							//pointer to current directory name
    					  &stgStartupInfo,                                    //LPSTARTUPINFO lpStartupInfo							//pointer to STARTUPINFO
    					  &stgProcessInfo);                                   //LPPROCESS_INFORMATION lpProcessInformation		//pointer to PROCESS_INFORMATION
    
    	//Auf das Programmende warten
    	WaitForSingleObject(stgProcessInfo.hProcess, INFINITE);
    	//Das Programm wurde beendet...
    
    	//If the function succeeds, the return value is nonzero.
    	//If the function fails, the return value is zero.
    	//Rückgabewert auslesen
    	GetExitCodeProcess(stgProcessInfo.hProcess, &ExitCode);
    
    	//Ressourcen freigeben
    	CloseHandle(stgProcessInfo.hProcess);
    
    	//ExitCode == 0 entspricht "Fehlerfrei"
    	if(ExitCode == 0)
    	{
    		//Fehlerfreier Programmstart
    		Protokoll("Befehl ausgeführt:\t" + asMsg);
    	}
    	else
    	{
    		//Programmstart fehlgeschlagen
    		Protokoll("Befehl fehlgeschlagen:\t" + asMsg);
    		Protokoll("Befehl fehlgeschlagen:\tExitCode: " + FormatFloat("#,##0", ExitCode));
    	} // else if(ExitCode == 0)
    }
    //---------------------------------------------------------------------------
    

    Edit:
    Bitte halbwegs sinnvolle Zeilenumbrüche verwenden. Danke!



  • Hab das Problem gefunden. Die Eigenschaft Interactive stand auf false. Nach dem setzen der Eigenschaft auf true funktionierts jetzt. Vielen Dank an all die netten Leute, die mir geholfen haben.

    Schönes Wochenende.


Anmelden zum Antworten