SoloCodigo

Programación General => C/C++ => Mensaje iniciado por: pacogonzalez61 en Domingo 22 de Febrero de 2004, 12:45

Título: Llamar A Un Programa Ejecutable En Windows
Publicado por: pacogonzalez61 en Domingo 22 de Febrero de 2004, 12:45
Como desde un programa en  o C++ en Windows puedo llamar a otro programa
ejecutable tambien de Windows . Por ejemplo en MSDOS cuando quiero llamar a
un progrma ejecutables escribo la función system "PROGRAMA.EXE".
Como soy nuevo en c** en Winddow. como puedo hacerlo.
Gracias.
Título: Re: Llamar A Un Programa Ejecutable En Windows
Publicado por: Ruben3d en Domingo 22 de Febrero de 2004, 13:56
Hola.

Lo que pides puedes hacerlo de dos formas: mediante CreateProcess o mediante spawn. Aqui te dejo un ejemplo de cada uno, sacado de MSDN:

Creating a process in Windows using CreateProcess
Código: Text
  1.  
  2. #include <windows.h>
  3. #include <process.h>
  4. #include <stdio.h>
  5.  
  6. void main()
  7. {
  8.     STARTUPINFO    si;
  9.     PROCESS_INFORMATION  pi;
  10.  
  11.     GetStartupInfo(&si);
  12.  
  13.     printf("Running Notepad with CreateProcess\n");
  14.     CreateProcess(NULL, "notepad",  // Name of app to launch
  15.   NULL,      // Default process security attributes
  16.   NULL,      // Default thread security attributes
  17.   FALSE,      // Don't inherit handles from the parent
  18.   0,      // Normal priority
  19.   NULL,      // Use the same environment as the parent
  20.   NULL,      // Launch in the current directory
  21.   &si,      // Startup Information
  22.   &pi);      // Process information stored upon return
  23.  
  24.     printf("Done.\n");
  25.     exit(0);
  26. }
  27.  
  28.  

Creating a process in Windows using spawn
Código: Text
  1.  
  2. #include <windows.h>
  3. #include <process.h>
  4. #include <stdio.h>
  5.  
  6. void main()
  7. {
  8.     printf("Running Notepad with spawnlp\n");
  9.     _spawnlp( _P_NOWAIT, "notepad", "notepad", NULL );
  10.  
  11.     printf("Done.\n");
  12.     exit(0);
  13. }
  14.  
  15.  

Espero que te sirva.

Un saludo.

Ruben3d