免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3864 | 回复: 4
打印 上一主题 下一主题

求助:如何用java检查windows下的服务进程状态? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-07-07 18:30 |只看该作者 |倒序浏览
如何用java检查windows下的服务进程状态?

论坛徽章:
0
2 [报告]
发表于 2004-07-08 08:06 |只看该作者

求助:如何用java检查windows下的服务进程状态?

使用JNI调用Win32 API

论坛徽章:
0
3 [报告]
发表于 2004-07-08 08:22 |只看该作者

求助:如何用java检查windows下的服务进程状态?

有没有例程?Thanks

论坛徽章:
0
4 [报告]
发表于 2004-07-08 10:36 |只看该作者

求助:如何用java检查windows下的服务进程状态?


  1. //  Exam38.cpp
  2. //  12/29/2000  (rk)
  3. //  Modified: 6/16/2003  (rk)
  4. //  Test the routine FIND_PROC_BY_NAME to find a running process

  5. #include <windows.h>;
  6. #include <tlhelp32.h>;
  7. #include <iostream.h>;

  8. int FIND_PROC_BY_NAME(const char *);

  9. int main(int argc, char *argv[])
  10. {
  11. //  Check whether a process is currently running, or not
  12.     char szName[100]="notepad.exe";   // Name of process to find
  13.         int iRes;

  14.         iRes=FIND_PROC_BY_NAME(szName);

  15.     // Note: iRes=0 means process not found, =1 means yes, it is found in memory
  16.         cout << "Result code=" << iRes << endl;
  17.         return 0;
  18. }

  19. int FIND_PROC_BY_NAME(const char *szToFind)
  20. // Created: 12/29/2000  (RK)
  21. // Last modified: 6/16/2003  (RK)
  22. // Please report any problems or bugs to kochhar@physiology.wisc.edu
  23. // The latest version of this routine can be found at:
  24. //     http://www.neurophys.wisc.edu/ravi/software/killproc/
  25. // Check whether the process "szToFind" is currently running in memory
  26. // This works for Win/95/98/ME and also Win/NT/2000/XP
  27. // The process name is case-insensitive, i.e. "notepad.exe" and "NOTEPAD.EXE"
  28. // will both work (for szToFind)
  29. // Return codes are as follows:
  30. //   0   = Process was not found
  31. //   1   = Process was found
  32. //   605 = Unable to search for process
  33. //   606 = Unable to identify system type
  34. //   607 = Unsupported OS
  35. //   632 = Process name is invalid
  36. // Change history:
  37. //  3/10/2002   - Fixed memory leak in some cases (hSnapShot and
  38. //                and hSnapShotm were not being closed sometimes)
  39. //  6/13/2003   - Removed iFound (was not being used, as pointed out
  40. //                by John Emmas)
  41. {
  42.         BOOL bResult,bResultm;
  43.         DWORD aiPID[1000],iCb=1000,iNumProc,iV2000=0;
  44.         DWORD iCbneeded,i;
  45.         char szName[MAX_PATH],szToFindUpper[MAX_PATH];
  46.         HANDLE hProc,hSnapShot,hSnapShotm;
  47.         OSVERSIONINFO osvi;
  48.     HINSTANCE hInstLib;
  49.         int iLen,iLenP,indx;
  50.     HMODULE hMod;
  51.         PROCESSENTRY32 procentry;      
  52.         MODULEENTRY32 modentry;

  53.         // PSAPI Function Pointers.
  54.      BOOL (WINAPI *lpfEnumProcesses)( DWORD *, DWORD cb, DWORD * );
  55.      BOOL (WINAPI *lpfEnumProcessModules)( HANDLE, HMODULE *,
  56.         DWORD, LPDWORD );
  57.      DWORD (WINAPI *lpfGetModuleBaseName)( HANDLE, HMODULE,
  58.         LPTSTR, DWORD );

  59.       // ToolHelp Function Pointers.
  60.       HANDLE (WINAPI *lpfCreateToolhelp32Snapshot)(DWORD,DWORD) ;
  61.       BOOL (WINAPI *lpfProcess32First)(HANDLE,LPPROCESSENTRY32) ;
  62.       BOOL (WINAPI *lpfProcess32Next)(HANDLE,LPPROCESSENTRY32) ;
  63.       BOOL (WINAPI *lpfModule32First)(HANDLE,LPMODULEENTRY32) ;
  64.       BOOL (WINAPI *lpfModule32Next)(HANDLE,LPMODULEENTRY32) ;

  65.         // Transfer Process name into "szToFindUpper" and
  66.         // convert it to upper case
  67.         iLenP=strlen(szToFind);
  68.         if(iLenP<1 || iLenP>;MAX_PATH) return 632;
  69.         for(indx=0;indx<iLenP;indx++)
  70.                 szToFindUpper[indx]=toupper(szToFind[indx]);
  71.         szToFindUpper[iLenP]=0;

  72.         // First check what version of Windows we're in
  73.         osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  74.     bResult=GetVersionEx(&osvi);
  75.         if(!bResult)     // Unable to identify system version
  76.             return 606;

  77.         // At Present we only support Win/NT/2000 or Win/9x/ME
  78.         if((osvi.dwPlatformId != VER_PLATFORM_WIN32_NT) &&
  79.                 (osvi.dwPlatformId != VER_PLATFORM_WIN32_WINDOWS))
  80.                 return 607;

  81.     if(osvi.dwPlatformId==VER_PLATFORM_WIN32_NT)
  82.         {
  83.                 // Win/NT or 2000 or XP

  84.          // Load library and get the procedures explicitly. We do
  85.          // this so that we don't have to worry about modules using
  86.          // this code failing to load under Windows 95, because
  87.          // it can't resolve references to the PSAPI.DLL.
  88.          hInstLib = LoadLibraryA("PSAPI.DLL");
  89.          if(hInstLib == NULL)
  90.             return 605;

  91.          // Get procedure addresses.
  92.          lpfEnumProcesses = (BOOL(WINAPI *)(DWORD *,DWORD,DWORD*))
  93.             GetProcAddress( hInstLib, "EnumProcesses" ) ;
  94.          lpfEnumProcessModules = (BOOL(WINAPI *)(HANDLE, HMODULE *,
  95.             DWORD, LPDWORD)) GetProcAddress( hInstLib,
  96.             "EnumProcessModules" ) ;
  97.          lpfGetModuleBaseName =(DWORD (WINAPI *)(HANDLE, HMODULE,
  98.             LPTSTR, DWORD )) GetProcAddress( hInstLib,
  99.             "GetModuleBaseNameA" ) ;

  100.          if( lpfEnumProcesses == NULL ||
  101.             lpfEnumProcessModules == NULL ||
  102.             lpfGetModuleBaseName == NULL)
  103.             {
  104.                FreeLibrary(hInstLib);
  105.                return 605;
  106.             }
  107.                  
  108.                 bResult=lpfEnumProcesses(aiPID,iCb,&iCbneeded);
  109.                 if(!bResult)
  110.                 {
  111.                         // Unable to get process list, EnumProcesses failed
  112.             FreeLibrary(hInstLib);
  113.                         return 605;
  114.                 }

  115.                 // How many processes are there?
  116.                 iNumProc=iCbneeded/sizeof(DWORD);

  117.                 // Get and match the name of each process
  118.                 for(i=0;i<iNumProc;i++)
  119.                 {
  120.                         // Get the (module) name for this process

  121.                 strcpy(szName,"Unknown");
  122.                         // First, get a handle to the process
  123.                 hProc=OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,FALSE,
  124.                                 aiPID[i]);
  125.                 // Now, get the process name
  126.                 if(hProc)
  127.                         {
  128.                if(lpfEnumProcessModules(hProc,&hMod,sizeof(hMod),&iCbneeded) )
  129.                            {
  130.                   iLen=lpfGetModuleBaseName(hProc,hMod,szName,MAX_PATH);
  131.                            }
  132.                         }
  133.                 CloseHandle(hProc);
  134.                         // Match regardless of lower or upper case
  135.                         if(strcmp(_strupr(szName),szToFindUpper)==0)
  136.                         {
  137.                                 // Process found
  138.                                 FreeLibrary(hInstLib);
  139.                                 return 1;
  140.                         }
  141.                 }
  142.         }

  143.         if(osvi.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS)
  144.         {
  145.                 // Win/95 or 98 or ME
  146.                        
  147.                 hInstLib = LoadLibraryA("Kernel32.DLL");
  148.                 if( hInstLib == NULL )
  149.                         return FALSE ;

  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" ) ;
  160.                 lpfProcess32First=
  161.                         (BOOL(WINAPI *)(HANDLE,LPPROCESSENTRY32))
  162.                         GetProcAddress( hInstLib, "Process32First" ) ;
  163.                 lpfProcess32Next=
  164.                         (BOOL(WINAPI *)(HANDLE,LPPROCESSENTRY32))
  165.                         GetProcAddress( hInstLib, "Process32Next" ) ;
  166.                 lpfModule32First=
  167.                         (BOOL(WINAPI *)(HANDLE,LPMODULEENTRY32))
  168.                         GetProcAddress( hInstLib, "Module32First" ) ;
  169.                 lpfModule32Next=
  170.                         (BOOL(WINAPI *)(HANDLE,LPMODULEENTRY32))
  171.                         GetProcAddress( hInstLib, "Module32Next" ) ;
  172.                 if( lpfProcess32Next == NULL ||
  173.                         lpfProcess32First == NULL ||
  174.                     lpfModule32Next == NULL ||
  175.                         lpfModule32First == NULL ||
  176.                         lpfCreateToolhelp32Snapshot == NULL )
  177.                 {
  178.                         FreeLibrary(hInstLib);
  179.                         return 605;
  180.                 }
  181.                        
  182.                 // The Process32.. and Module32.. routines return names in all uppercase

  183.                 // Get a handle to a Toolhelp snapshot of all the systems processes.

  184.                 hSnapShot = lpfCreateToolhelp32Snapshot(
  185.                         TH32CS_SNAPPROCESS, 0 ) ;
  186.                 if( hSnapShot == INVALID_HANDLE_VALUE )
  187.                 {
  188.                         FreeLibrary(hInstLib);
  189.                         return 605;
  190.                 }
  191.                
  192.         // Get the first process' information.
  193.         procentry.dwSize = sizeof(PROCESSENTRY32);
  194.         bResult=lpfProcess32First(hSnapShot,&procentry);

  195.         // While there are processes, keep looping and checking.
  196.         while(bResult)
  197.         {
  198.                     // Get a handle to a Toolhelp snapshot of this process.
  199.                     hSnapShotm = lpfCreateToolhelp32Snapshot(
  200.                             TH32CS_SNAPMODULE, procentry.th32ProcessID) ;
  201.                     if( hSnapShotm == INVALID_HANDLE_VALUE )
  202.                         {
  203.                                 CloseHandle(hSnapShot);
  204.                             FreeLibrary(hInstLib);
  205.                             return 605;
  206.                         }
  207.                         // Get the module list for this process
  208.                         modentry.dwSize=sizeof(MODULEENTRY32);
  209.                         bResultm=lpfModule32First(hSnapShotm,&modentry);

  210.                         // While there are modules, keep looping and checking
  211.                         while(bResultm)
  212.                         {
  213.                         if(strcmp(modentry.szModule,szToFindUpper)==0)
  214.                                 {
  215.                                         // Process found
  216.                                         CloseHandle(hSnapShotm);
  217.                                         CloseHandle(hSnapShot);
  218.                                         FreeLibrary(hInstLib);
  219.                                         return 1;
  220.                                 }
  221.                                 else
  222.                                 {  // Look for next modules for this process
  223.                                         modentry.dwSize=sizeof(MODULEENTRY32);
  224.                                         bResultm=lpfModule32Next(hSnapShotm,&modentry);
  225.                                 }
  226.                         }

  227.                         //Keep looking
  228.                         CloseHandle(hSnapShotm);
  229.             procentry.dwSize = sizeof(PROCESSENTRY32);
  230.             bResult = lpfProcess32Next(hSnapShot,&procentry);
  231.         }
  232.                 CloseHandle(hSnapShot);
  233.         }
  234.         FreeLibrary(hInstLib);
  235.         return 0;
  236. }
复制代码

这有一个查找进程的例子。kill进程和得到进程状态的函数可以查一下msdn。
用JNI调用一下就可以了。

论坛徽章:
0
5 [报告]
发表于 2004-07-08 10:43 |只看该作者

求助:如何用java检查windows下的服务进程状态?

http://java.sun.com/developer/codesamples/jni.html
这个是JNI的一些例子
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP