• Jueves 2 de Mayo de 2024, 14:50

Autor Tema:  Cerrar Aplicaciones Desde C++ Builder  (Leído 3283 veces)

fful

  • Nuevo Miembro
  • *
  • Mensajes: 4
    • Ver Perfil
Cerrar Aplicaciones Desde C++ Builder
« en: Jueves 15 de Julio de 2004, 23:10 »
0
Tengo un problema

necesito cerrar (Matar) desde mi aplicacion, otras aplicaciones abiertas, sacar la lista de todas las aplicaciones que se encuentran corriendo (abiertas por usuario, Word, Excell,..) y en base a esto, Cerrar las aplicaciones.
obviamente que la nuestra no se cierre..

Por su tiempo y al compartir su conocimiento, Gracias

Max_D

  • Miembro MUY activo
  • ***
  • Mensajes: 117
    • Ver Perfil
    • http://sitioteca.spaces.live.com/
Re: Cerrar Aplicaciones Desde C++ Builder
« Respuesta #1 en: Viernes 11 de Agosto de 2006, 19:08 »
0
Yo tambie intento lo mismo, poder cerrar una aplicacion mediante un comando del tipo ShellExecute. He buscado pero no he encontrado nada para C++ Builder.

Max_D

  • Miembro MUY activo
  • ***
  • Mensajes: 117
    • Ver Perfil
    • http://sitioteca.spaces.live.com/
Re: Cerrar Aplicaciones Desde C++ Builder
« Respuesta #2 en: Viernes 11 de Agosto de 2006, 21:09 »
0
He seguido buscando y al final encontre algo en Delphi que pase a C++ Builder y me quedo esto:

Código: Text
  1.  
  2.    HWND valor;
  3.  
  4.    valor = FindWindow(0,"eMule v0.47a");
  5.    if (valor == 0)
  6.       ShowMessage("No encuentre el eMule v0.47a");
  7.    else
  8.       SendMessage(valor,WM_CLOSE,0,0);
  9.  
  10.    Application->Terminate();
  11.  
  12.  
  13.  

En mi caso era para cerrar el emule, si es para otro programa, se debe poner el nombre exacto que aparece en la barra de tareas cuando se esta ejecutando.

wako13

  • Miembro activo
  • **
  • Mensajes: 36
    • Ver Perfil
Re: Cerrar Aplicaciones Desde C++ Builder
« Respuesta #3 en: Sábado 12 de Agosto de 2006, 01:41 »
0
Aqui te dejo un codigo para hacer un killprocess a cualquier aplicación, esta largo pero funcional...

Util para matar alguna tarea rebelde, que inciste en su lucha contra el inevitable final. Espero que le sirva a alguien.

Declaraciones:
Código:
Código: Text
  1. #include <windows.h>
  2. #include <tlhelp32.h>
  3. #include <iostream.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6.  
  7. int KILL_PROC_BY_NAME(const char *);  //Declaracion de la funcion
  8.  
  9.  
Ejemplo de utilizacion:
Código:

  /
Código: Text
  1. /  Terminar un proceso activo
  2.    char szName[100]="iexplore.exe";   // Nombre del proceso a liquidar
  3.    int iRes;
  4.  
  5.    iRes=KILL_PROC_BY_NAME(szName);
  6.  
  7.    cout << "Result code=" << iRes << endl;
  8.  

La funcion:
Código:
Código: Text
  1. int KILL_PROC_BY_NAME(const char *szToTerminate)
  2. {
  3.    BOOL bResult,bResultm;
  4.    DWORD aiPID[1000],iCb=1000,iNumProc,iV2000=0;
  5.    DWORD iCbneeded,i,iFound=0;
  6.    char szName[MAX_PATH],szToTermUpper[MAX_PATH];
  7.    HANDLE hProc,hSnapShot,hSnapShotm;
  8.    OSVERSIONINFO osvi;
  9.     HINSTANCE hInstLib;
  10.    int iLen,iLenP,indx;
  11.     HMODULE hMod;
  12.    PROCESSENTRY32 procentry;    
  13.    MODULEENTRY32 modentry;
  14.  
  15.    // Transfer Process name into "szToTermUpper" and
  16.    // convert it to upper case
  17.    iLenP=strlen(szToTerminate);
  18.    if(iLenP<1 || iLenP>MAX_PATH) return 632;
  19.    for(indx=0;indx<iLenP;indx++)
  20.       szToTermUpper[indx]=toupper(szToTerminate[indx]);
  21.    szToTermUpper[iLenP]=0;
  22.  
  23.      // PSAPI Function Pointers.
  24.      BOOL (WINAPI *lpfEnumProcesses)( DWORD *, DWORD cb, DWORD * );
  25.      BOOL (WINAPI *lpfEnumProcessModules)( HANDLE, HMODULE *,
  26.         DWORD, LPDWORD );
  27.      DWORD (WINAPI *lpfGetModuleBaseName)( HANDLE, HMODULE,
  28.         LPTSTR, DWORD );
  29.  
  30.       // ToolHelp Function Pointers.
  31.       HANDLE (WINAPI *lpfCreateToolhelp32Snapshot)(DWORD,DWORD)&#59;
  32.       BOOL (WINAPI *lpfProcess32First)(HANDLE,LPPROCESSENTRY32)&#59;
  33.       BOOL (WINAPI *lpfProcess32Next)(HANDLE,LPPROCESSENTRY32)&#59;
  34.       BOOL (WINAPI *lpfModule32First)(HANDLE,LPMODULEENTRY32)&#59;
  35.       BOOL (WINAPI *lpfModule32Next)(HANDLE,LPMODULEENTRY32)&#59;
  36.  
  37.    // First check what version of Windows we're in
  38.    osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  39.     bResult=GetVersionEx(&osvi);
  40.    if(!bResult)     // Unable to identify system version
  41.        return 606;
  42.  
  43.    // At Present we only support Win/NT/2000/XP or Win/9x/ME
  44.    if((osvi.dwPlatformId != VER_PLATFORM_WIN32_NT) &&
  45.       (osvi.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS))
  46.       return 607;
  47.  
  48.     if(osvi.dwPlatformId==VER_PLATFORM_WIN32_NT)
  49.    {
  50.         // Win/NT or 2000 or XP
  51.  
  52.          // Load library and get the procedures explicitly. We do
  53.          // this so that we don't have to worry about modules using
  54.          // this code failing to load under Windows 9x, because
  55.          // it can't resolve references to the PSAPI.DLL.
  56.          hInstLib = LoadLibraryA("PSAPI.DLL");
  57.          if(hInstLib == NULL)
  58.             return 605;
  59.  
  60.          // Get procedure addresses.
  61.          lpfEnumProcesses = (BOOL(WINAPI *)(DWORD *,DWORD,DWORD*))
  62.             GetProcAddress( hInstLib, "EnumProcesses" )&#59;
  63.          lpfEnumProcessModules = (BOOL(WINAPI *)(HANDLE, HMODULE *,
  64.             DWORD, LPDWORD)) GetProcAddress( hInstLib,
  65.             "EnumProcessModules" )&#59;
  66.          lpfGetModuleBaseName =(DWORD (WINAPI *)(HANDLE, HMODULE,
  67.             LPTSTR, DWORD )) GetProcAddress( hInstLib,
  68.             "GetModuleBaseNameA" )&#59;
  69.  
  70.          if(lpfEnumProcesses == NULL ||
  71.             lpfEnumProcessModules == NULL ||
  72.             lpfGetModuleBaseName == NULL)
  73.             {
  74.                FreeLibrary(hInstLib);
  75.                return 700;
  76.             }
  77.        
  78.       bResult=lpfEnumProcesses(aiPID,iCb,&iCbneeded);
  79.       if(!bResult)
  80.       {
  81.          // Unable to get process list, EnumProcesses failed
  82.             FreeLibrary(hInstLib);
  83.          return 701;
  84.       }
  85.  
  86.       // How many processes are there?
  87.       iNumProc=iCbneeded/sizeof(DWORD);
  88.  
  89.       // Get and match the name of each process
  90.       for(i=0;i<iNumProc;i++)
  91.       {
  92.          // Get the (module) name for this process
  93.  
  94.            strcpy(szName,"Unknown");
  95.          // First, get a handle to the process
  96.            hProc=OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,FALSE,
  97.             aiPID[i]);
  98.            // Now, get the process name
  99.            if(hProc)
  100.          {
  101.                if(lpfEnumProcessModules(hProc,&hMod,sizeof(hMod),&iCbneeded) )
  102.             {
  103.                   iLen=lpfGetModuleBaseName(hProc,hMod,szName,MAX_PATH);
  104.             }
  105.          }
  106.            CloseHandle(hProc);
  107.  
  108.             if(strcmp(strupr(szName),szToTermUpper)==0)
  109.  
  110.          {
  111.             // Process found, now terminate it
  112.             iFound=1;
  113.             // First open for termination
  114.             hProc=OpenProcess(PROCESS_TERMINATE,FALSE,aiPID[i]);
  115.             if(hProc)
  116.             {
  117.                if(TerminateProcess(hProc,0))
  118.                {
  119.                   // process terminated
  120.                   CloseHandle(hProc);
  121.                         FreeLibrary(hInstLib);
  122.                   return 0;
  123.                }
  124.                else
  125.                {
  126.                   // Unable to terminate process
  127.                   CloseHandle(hProc);
  128.                         FreeLibrary(hInstLib);
  129.                   return 602;
  130.                }
  131.             }
  132.             else
  133.             {
  134.                // Unable to open process for termination
  135.                     FreeLibrary(hInstLib);
  136.                return 604;
  137.             }
  138.          }
  139.       }
  140.    }
  141.  
  142.    if(osvi.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS)
  143.    {
  144.       // Win/95 or 98 or ME
  145.          
  146.       hInstLib = LoadLibraryA("Kernel32.DLL");
  147.       if( hInstLib == NULL )
  148.          return 702;
  149.  
  150.       // Get procedure addresses.
  151.       // We are linking to these functions of Kernel32
  152.       // explicitly, because otherwise a module using
  153.       // this code would fail to load under Windows NT,
  154.       // which does not have the Toolhelp32
  155.       // functions in the Kernel 32.
  156.       lpfCreateToolhelp32Snapshot=
  157.          (HANDLE(WINAPI *)(DWORD,DWORD))
  158.          GetProcAddress( hInstLib,
  159.          "CreateToolhelp32Snapshot" )&#59;
  160.       lpfProcess32First=
  161.          (BOOL(WINAPI *)(HANDLE,LPPROCESSENTRY32))
  162.          GetProcAddress( hInstLib, "Process32First" )&#59;
  163.       lpfProcess32Next=
  164.          (BOOL(WINAPI *)(HANDLE,LPPROCESSENTRY32))
  165.          GetProcAddress( hInstLib, "Process32Next" )&#59;
  166.       lpfModule32First=
  167.          (BOOL(WINAPI *)(HANDLE,LPMODULEENTRY32))
  168.          GetProcAddress( hInstLib, "Module32First" )&#59;
  169.       lpfModule32Next=
  170.          (BOOL(WINAPI *)(HANDLE,LPMODULEENTRY32))
  171.          GetProcAddress( hInstLib, "Module32Next" )&#59;
  172.       if( lpfProcess32Next == NULL ||
  173.          lpfProcess32First == NULL ||
  174.           lpfModule32Next == NULL ||
  175.          lpfModule32First == NULL ||
  176.          lpfCreateToolhelp32Snapshot == NULL )
  177.       {
  178.          FreeLibrary(hInstLib);
  179.          return 703;
  180.       }
  181.          
  182.       // The Process32.. and Module32.. routines return names in all uppercase
  183.  
  184.       // Get a handle to a Toolhelp snapshot of all the systems processes.
  185.  
  186.       hSnapShot = lpfCreateToolhelp32Snapshot(
  187.          TH32CS_SNAPPROCESS, 0 )&#59;
  188.       if( hSnapShot == INVALID_HANDLE_VALUE )
  189.       {
  190.          FreeLibrary(hInstLib);
  191.          return 704;
  192.       }
  193.        
  194.         // Get the first process' information.
  195.         procentry.dwSize = sizeof(PROCESSENTRY32);
  196.         bResult=lpfProcess32First(hSnapShot,&procentry);
  197.  
  198.         // While there are processes, keep looping and checking.
  199.         while(bResult)
  200.         {
  201.           // Get a handle to a Toolhelp snapshot of this process.
  202.           hSnapShotm = lpfCreateToolhelp32Snapshot(
  203.              TH32CS_SNAPMODULE, procentry.th32ProcessID)&#59;
  204.           if( hSnapShotm == INVALID_HANDLE_VALUE )
  205.          {
  206.             CloseHandle(hSnapShot);
  207.              FreeLibrary(hInstLib);
  208.              return 704;
  209.          }
  210.          // Get the module list for this process
  211.          modentry.dwSize=sizeof(MODULEENTRY32);
  212.          bResultm=lpfModule32First(hSnapShotm,&modentry);
  213.  
  214.          // While there are modules, keep looping and checking
  215.          while(bResultm)
  216.          {
  217.               if(strcmp(modentry.szModule,szToTermUpper)==0)
  218.             {
  219.                 // Process found, now terminate it
  220.                 iFound=1;
  221.                 // First open for termination
  222.                 hProc=OpenProcess(PROCESS_TERMINATE,FALSE,procentry.th32ProcessID);
  223.                 if(hProc)
  224.                {
  225.                    if(TerminateProcess(hProc,0))
  226.                   {
  227.                       // process terminated
  228.                      CloseHandle(hSnapShotm);
  229.                      CloseHandle(hSnapShot);
  230.                      CloseHandle(hProc);
  231.                          FreeLibrary(hInstLib);
  232.                       return 0;
  233.                   }
  234.                    else
  235.                   {
  236.                       // Unable to terminate process
  237.                      CloseHandle(hSnapShotm);
  238.                      CloseHandle(hSnapShot);
  239.                      CloseHandle(hProc);
  240.                          FreeLibrary(hInstLib);
  241.                       return 602;
  242.                   }
  243.                }
  244.                 else
  245.                {
  246.                    // Unable to open process for termination
  247.                   CloseHandle(hSnapShotm);
  248.                   CloseHandle(hSnapShot);
  249.                      FreeLibrary(hInstLib);
  250.                    return 604;
  251.                }
  252.             }
  253.             else
  254.             {  // Look for next modules for this process
  255.                modentry.dwSize=sizeof(MODULEENTRY32);
  256.                bResultm=lpfModule32Next(hSnapShotm,&modentry);
  257.             }
  258.          }
  259.  
  260.          //Keep looking
  261.          CloseHandle(hSnapShotm);
  262.             procentry.dwSize = sizeof(PROCESSENTRY32);
  263.             bResult = lpfProcess32Next(hSnapShot,&procentry);
  264.         }
  265.       CloseHandle(hSnapShot);
  266.    }
  267.    if(iFound==0)
  268.    {
  269.       FreeLibrary(hInstLib);
  270.       return 603;
  271.    }
  272.    FreeLibrary(hInstLib);
  273.    return 0;
  274. }
  275.  


Informacion adicional: http://www.neurophys.wisc.edu/ravi/software/