Befehle im Hintergrund ausführen?
-
Hallo,
Ich würde gerne ein paar externe Befehle im Hintergrund ausführen.
system ("dir"); CreateProcess( NULL, // No module name (use command line) "dir", // Command line NULL, // Process handle not inheritable NULL, // Thread handle not inheritable FALSE, // Set handle inheritance to FALSE 0, // No creation flags NULL, // Use parent's environment block NULL, // Use parent's starting directory &si, // Pointer to STARTUPINFO structure &pi )Helfen mir da nicht wirklich, denn es "poppt" immer ein Konsolenfenster auf, was für den Programmfluss natürlich sehr lästig und unschön ist. Gibt es keine Möglichkeit, dass ich Befehle im Hintergrund ausführen kann, so dass der User nicht durch aufpoppende Konsolenfenster genervt wird?
Danke für die Hilfe
-
dir ist nunmal ein consolenprogramm.
aber warum ließt du das verzeichnis nicht mit winapi befehlen aus, anstatt auf ein externes Programm zuzugreifen
-
wie würde das mit winapi befehlen aussehen?
-
FindFirstFile
FindNextFile
-
also ich hab es jetzt nicht getestet, ob die konsole die durch System("dir") aufgerufen wird, nicht aufpopt, aber
über createprocess kannst du startparameter übergeben, die das weis ich sicher den zu startenden process beeinflussen. unter anderem eben auch ob der zu startende process als Fenster angezeigt wird oder nicht.
versuch es mal so,STARTUPINFO sInfo; ZeroMemory(&sInfo,sizeof(sInfo)); sInfo.cb = sizeof(sInfo); sInfo.dwFlags = STARTF_USESHOWWINDOW;//Flag Damit das Mitglied von STARTUPINFO wShowWindow genutzt werden kann sInfo.wShowWindow = SW_HIDE;//Zeigt Fenster an oder nichtSW_SHOW system ("dir"); CreateProcess( NULL, // No module name (use command line) "dir", // Command line NULL, // Process handle not inheritable NULL, // Thread handle not inheritable FALSE, // Set handle inheritance to FALSE 0, // No creation flags NULL, // Use parent's environment block NULL, // Use parent's starting directory &sInfo, // Pointer to STARTUPINFO structure &pi );
-
pi wird nicht erkannt weil nicht definiert?
-
Habs jetzt so umgebastelt:
int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil) { STARTUPINFO sInfo; PROCESS_INFORMATION pi; ZeroMemory(&sInfo,sizeof(sInfo)); sInfo.cb = sizeof(sInfo); ZeroMemory( &pi, sizeof(pi) ); sInfo.dwFlags = STARTF_USESHOWWINDOW;//Flag Damit das Mitglied von STARTUPINFO wShowWindow genutzt werden kann sInfo.wShowWindow = SW_HIDE;//Zeigt Fenster an oder nichtSW_SHOW system ("dir"); CreateProcess( NULL, // No module name (use command line) "", // Command line NULL, // Process handle not inheritable NULL, // Thread handle not inheritable FALSE, // Set handle inheritance to FALSE 0, // No creation flags NULL, // Use parent's environment block NULL, // Use parent's starting directory &sInfo, // Pointer to STARTUPINFO structure &pi ); // Wait until child process exits. WaitForSingleObject( pi.hProcess, INFINITE ); // Close process and thread handles. CloseHandle( pi.hProcess ); CloseHandle( pi.hThread ); }=> Das Konsolenfenster kommt leider immer noch

-
was eigendlich auch kein wunder ist, denn du übergibst ja an lpApplicationName = NULL.
laut msdn ruft der befehl system die cmd.exe im system32 ordner auf.
also setzt mal unter lpApplicationName die Pfadangabe anstatt NULL.CreateProcess( "C:\\WINDOWS\\system32\\cmd.exe", "dir", ...und verzichte auf den aufruf system("dir") denn das erledigst du jetzt mit createprocess
-
Probier mal, CreateProcess mit DETACHED_PROCESS als creation flag aufzurufen.
-
Konsolenfenster werden auch nicht geöffnet wenn die entsprechende Input und Output Pipes umgelenkt werden.