procutil.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include "procutil.h"
  2. namespace bbProcUtil{
  3. #if _WIN32
  4. void terminateChildren( DWORD procid,HANDLE snapshot,int exitCode ){
  5. PROCESSENTRY32 procinfo;
  6. procinfo.dwSize=sizeof( procinfo );
  7. int gotinfo=Process32First( snapshot,&procinfo );
  8. while( gotinfo ){
  9. if( procinfo.th32ParentProcessID==procid ){
  10. // printf("process=%i parent=%i module=%x path=%s\n",procinfo.th32ProcessID,procinfo.th32ParentProcessID,procinfo.th32ModuleID,procinfo.szExeFile);
  11. terminateChildren( procinfo.th32ProcessID,snapshot,exitCode );
  12. HANDLE child=OpenProcess( PROCESS_ALL_ACCESS,0,procinfo.th32ProcessID );
  13. if( child ){
  14. int res=TerminateProcess( child,exitCode );
  15. CloseHandle( child );
  16. }
  17. }
  18. gotinfo=Process32Next( snapshot,&procinfo );
  19. }
  20. }
  21. int TerminateProcessGroup( HANDLE prochandle,int exitCode ){
  22. HANDLE snapshot;
  23. int procid=GetProcessId( prochandle );
  24. snapshot=CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS,0 );
  25. if( snapshot!=INVALID_HANDLE_VALUE ){
  26. terminateChildren( GetProcessId( prochandle ),snapshot,exitCode );
  27. CloseHandle( snapshot );
  28. }
  29. int res=TerminateProcess( prochandle,exitCode );
  30. return res;
  31. }
  32. #else
  33. char **makeargv( const char *cmd ){
  34. int n,c;
  35. char *p;
  36. static char *args,**argv;
  37. if( args ) free( args );
  38. if( argv ) free( argv );
  39. args=(char*)malloc( strlen(cmd)+1 );
  40. strcpy( args,cmd );
  41. n=0;
  42. p=args;
  43. while( (c=*p++) ){
  44. if( c==' ' ){
  45. continue;
  46. }else if( c=='\"' ){
  47. while( *p && *p!='\"' ) ++p;
  48. }else{
  49. while( *p && *p!=' ' ) ++p;
  50. }
  51. if( *p ) ++p;
  52. ++n;
  53. }
  54. argv=(char**)malloc( (n+1)*sizeof(char*) );
  55. n=0;
  56. p=args;
  57. while( (c=*p++) ){
  58. if( c==' ' ){
  59. continue;
  60. }else if( c=='\"' ){
  61. argv[n]=p;
  62. while( *p && *p!='\"' ) ++p;
  63. }else{
  64. argv[n]=p-1;
  65. while( *p && *p!=' ' ) ++p;
  66. }
  67. if( *p ) *p++=0;
  68. ++n;
  69. }
  70. argv[n]=0;
  71. return argv;
  72. }
  73. #endif
  74. }