简体   繁体   中英

I have try to open process in cpp by using psapi library but *some process not openning* and I displaying it in webpage using tomcat server and jni

Problem 1

I try to open process in cpp by using psapi library but some SYSTEM process not opening and display it in webpage using tomcat server. i am using jni to transfer data between cpp to java (servlet).

process in task manager (fig 1)

process listed in webpage (fig 2)

In (fig 1) task manager is displaying all process details but in (fig 2) webpage not displaying because cpp unable to open these process.

cpp code snippet

    #include <psapi.h>

    // processID is process id
    HANDLE process = OpenProcess(PROCESS_ALL_ACCESS , FALSE , processID );
     if (NULL != process )
    {
        //do something
     }
     else{
        //process not opening 
     }

In the above snippet some process not opening and variable process became NULL

I used EnumProcesses() function to get All process id. this giving all process id. but OpenProcess() not opening some process.

I tried the below code to open process but not working.

HANDLE process = OpenProcess(READ_CONTROL | PROCESS_ALL_ACCESS | PROCESS_CREATE_PROCESS | PROCESS_CREATE_THREAD | PROCESS_DUP_HANDLE | PROCESS_QUERY_INFORMATION  | PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_SET_INFORMATION | PROCESS_SET_QUOTA |  PROCESS_SUSPEND_RESUME | PROCESS_TERMINATE | PROCESS_VM_OPERATION  | PROCESS_VM_READ | PROCESS_VM_WRITE | SYNCHRONIZE , FALSE , processID );

Another thing

First, no process is opened. Then I thought that tomcat does not have permission to read the process. So, I was changed tomcat settings in Tomcat 10.0/bin/Tomcat10W.exe

Tomcat Setting (fig 3)

I changed the radio button to (local system account) and checked (Allow service to interact with desktop) Box option as in (fig 3).

Then after changing the above settings only All process displayed for me. Except some process.

Error code

After these. I try to read error code using GetLastError() function.

HANDLE process = OpenProcess(PROCESS_ALL_ACCESS , FALSE , processID );
DWORD error_code =  GetLastError();

web page with error code

Below are the meaning of the error code

ERROR_SUCCESS
0 (0x0)
The operation completed successfully.

ERROR_ACCESS_DENIED
5 (0x5)
Access is denied.

ERROR_INVALID_HANDLE
6 (0x6)
The handle is invalid.

ERROR_INVALID_PARAMETER
87 (0x57)
The parameter is incorrect.

It showing these error.

  1. Some process show (error code 5) access denied.
  2. For process id: 1356. shows (error code 6) the handle is invalid (process opened but have some problem).
  3. For process id: 0. shows (error code 87) the parameter is incorrect.

Task manager process id: (0) and process id: (-)

process id (-) not read by EnumProcesses() and process id (0) not opened by OpenProcess .

How can I open these processes.

I think Tomcat need some more special permission to read those process. I am not sure.

Problem 2

I am reading the name of all process.

TCHAR processName[MAX_PATH];
GetModuleBaseName( process, NULL , processName , sizeof(processName ) );

After getting the name of the process. I am using env->NewStringUTF(processName) this line to convert TCHAR to java.lang.String because then only we can able to pass data from cpp to java using jni.

All process Name are displaying perfectly except one process.

Process name in task manager (fig 4)

In above (fig 4) image process id 1356 showing process name "Lsalso.exe" correctly.

Process name in webpage (fig 5)

Process name in webpage after refreshing (fig 6)

but in webpage(fig 5)(fig 6) not showing the correct name. the name of that process in web page changing randomly while refresh.

I try to print that name in console. It showing like this

  1. \n 6
  2. CÂç
  3. f} à�

I tried 6 times. Each time it shows different random text which does not have meaning.

Error code

I try to read error code using GetLastError() function.

GetModuleBaseName( process, NULL , processName , sizeof(processName ) );
DWORD error_code =  GetLastError();

It shows error code (998).

ERROR_NOACCESS
998 (0x3E6)
Invalid access to memory location.

This process shows error code (6) while opening process as I already said above.

I thing GetModuleBaseName() function not reading the name properly or conversion from TCHAR to java.lang.String env->NewStringUTF(processName) is problem.

I don't know why

Answering a part of your problem: the TCHAR to Java string conversion depends on how the program is being compiled. If -DUNICODE is set, TCHAR is a wchar_t , otherwise it is a unsigned char . You seem to assume the latter in every case.

Lucky for you, the fix is fairly simple:

jstring fromTCHAR(JNIEnv *env, TCHAR *in) {
#ifdef UNICODE
  return env->NewString(in);
#else
  return env->NewStringUTF(in);
#endif
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM