testimp.cpp

来自「I always wanted to play around with JNI.」· C++ 代码 · 共 644 行 · 第 1/2 页

CPP
644
字号
        // this so that we don't have to worry about modules using
        // this code failing to load under Windows 9x, because
        // it can't resolve references to the PSAPI.DLL.
        hInstLib = LoadLibraryA("PSAPI.DLL");
        if(hInstLib == NULL)
		{
			env->ReleaseStringUTFChars(_szToTerminate, szToTerminate);
			return NULL;
		}

        // Get procedure addresses.
        lpfEnumProcesses = (BOOL(WINAPI *)(DWORD *,DWORD,DWORD*)) GetProcAddress( hInstLib, "EnumProcesses" ) ;
        lpfEnumProcessModules = (BOOL(WINAPI *)(HANDLE, HMODULE *, DWORD, LPDWORD)) GetProcAddress( hInstLib, "EnumProcessModules" ) ;
        lpfGetModuleBaseName = (DWORD (WINAPI *)(HANDLE, HMODULE, LPTSTR, DWORD )) GetProcAddress( hInstLib, "GetModuleBaseNameA" ) ;

        if( lpfEnumProcesses == NULL || lpfEnumProcessModules == NULL || lpfGetModuleBaseName == NULL)
        {
            FreeLibrary(hInstLib);
			env->ReleaseStringUTFChars(_szToTerminate, szToTerminate);
            return NULL;
        }

		bResult = lpfEnumProcesses(aiPID,iCb,&iCbneeded);
		if(! bResult)
		{
			// Unable to get process list, EnumProcesses failed
			FreeLibrary(hInstLib);
			env->ReleaseStringUTFChars(_szToTerminate, szToTerminate);
			return NULL;
		}

		// How many processes are there?
		iNumProc = iCbneeded/sizeof(DWORD);
		array = env->NewObjectArray(iNumProc, cl, 0);
		array1 = env->NewObjectArray(1, cl, 0);

		if(array == 0)
		{
			env->ReleaseStringUTFChars(_szToTerminate, szToTerminate);
			return NULL;
		}

		if(array1 == 0)
		{
			env->DeleteLocalRef(array);
			env->ReleaseStringUTFChars(_szToTerminate, szToTerminate);
			return NULL;
		}

		int count = 0;
		// Get and match the name of each process
		for(DWORD i = 0; i < iNumProc; i++)
		{
			// Get the (module) name for this process
			strcpy(szName,"Unknown");
			// First, get a handle to the process
			hProc = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,FALSE, aiPID[i]);
			// Now, get the process name
			if(hProc)
			{
				if(lpfEnumProcessModules(hProc,&hMod,sizeof(hMod), &iCbneeded))
				{
					iLen = lpfGetModuleBaseName(hProc, hMod, szName, MAX_PATH);

					if(_printOutput)
					{
						printf("Name: %s\n", szName);
					}

					string = env->NewStringUTF(szName);
					if(string == NULL)
					{
						env->DeleteLocalRef(array);
						env->DeleteLocalRef(array1);
						env->ReleaseStringUTFChars(_szToTerminate, szToTerminate);
						return NULL;
					}

					env->SetObjectArrayElement(array, count, string);
					env->SetObjectArrayElement(array1, 0, string);
					env->DeleteLocalRef(string);
					count++;
				}
				
				CloseHandle(hProc);
			}
			

			// We will match regardless of lower or uppercase
			if(strcmp(_strupr(szName), szToTermUpper) == 0)
			{
				if(_killIt)
				{
					hProc = OpenProcess(PROCESS_TERMINATE,FALSE,aiPID[i]);
					if(hProc)
					{
						if(TerminateProcess(hProc, 1))
						{
							CloseHandle(hProc);
							FreeLibrary(hInstLib);
							env->DeleteLocalRef(array);
							env->ReleaseStringUTFChars(_szToTerminate, szToTerminate);
							return array1;
						}
						else
						{
							CloseHandle(hProc);
							FreeLibrary(hInstLib);
							env->DeleteLocalRef(array);
							env->DeleteLocalRef(array1);
							env->ReleaseStringUTFChars(_szToTerminate, szToTerminate);
							return NULL;
						}
					}
				}
				else
				{
					FreeLibrary(hInstLib);
					env->DeleteLocalRef(array);
					env->ReleaseStringUTFChars(_szToTerminate, szToTerminate);
					return array1;
				}
			}
		} // for

		FreeLibrary(hInstLib);
		env->DeleteLocalRef(array1);
		env->ReleaseStringUTFChars(_szToTerminate, szToTerminate);

		// Fix the array
		if(env->GetArrayLength(array) > count)
		{
			array = reallocateStringArray(env, array, count);
		}
		return array;
	}

	env->ReleaseStringUTFChars(_szToTerminate, szToTerminate);
	return NULL;
}

// Exported function
JNIEXPORT jint JNICALL Java_Test_startProcess(JNIEnv * env, jobject obj, jstring _commandLine, jstring _workingDir)
{
	const char *commandLine = env->GetStringUTFChars(_commandLine, 0);
	const char *workingDir = env->GetStringUTFChars(_workingDir, 0);

	STARTUPINFO si; 
	PROCESS_INFORMATION pi;
	ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

	if(CreateProcess(NULL,
			(LPTSTR) (LPCTSTR) commandLine,      // pointer to command line string
			NULL, NULL, FALSE, 
			NORMAL_PRIORITY_CLASS, // creation flags
			NULL,
			workingDir, // pointer to current directory name
			&si, &pi) != 0)
	{
		env->ReleaseStringUTFChars(_commandLine, commandLine);
		env->ReleaseStringUTFChars(_workingDir, workingDir);
		CloseHandle(pi.hProcess);
		CloseHandle(pi.hThread);

		return (jint) 0;
	}

	DWORD error = GetLastError();
	env->ReleaseStringUTFChars(_commandLine, commandLine);
	env->ReleaseStringUTFChars(_workingDir, workingDir);
	CloseHandle(pi.hProcess);
	CloseHandle(pi.hThread);

	return (jint) error;
}

// Exported function
JNIEXPORT jboolean JNICALL Java_Test_waitForFileMask(JNIEnv * env, jobject obj, jstring _directory, jstring _fileMask)
{
	WIN32_FIND_DATA FindFileData;
	HANDLE hFind = NULL;
	const char *directory = env->GetStringUTFChars(_directory, 0);
	const char *fileMask = env->GetStringUTFChars(_fileMask, 0);
	char pth[512] = {0};
	strcpy(pth, directory);
	strcat(pth, "\\");
	strcat(pth, fileMask);	

    const unsigned long dwFlags =	FILE_NOTIFY_CHANGE_FILE_NAME;//  |
									//FILE_NOTIFY_CHANGE_DIR_NAME   |
									//FILE_NOTIFY_CHANGE_ATTRIBUTES |
									//FILE_NOTIFY_CHANGE_SIZE       |
									//FILE_NOTIFY_CHANGE_LAST_WRITE;

	HANDLE hNotify = FindFirstChangeNotification(directory, false, dwFlags);
	env->ReleaseStringUTFChars(_directory, directory);
	env->ReleaseStringUTFChars(_fileMask, fileMask);

    // Get the handle to the event object
    if (hNotify == INVALID_HANDLE_VALUE)
	{
		return (jboolean) false;
	}

	int oldCount = 0;
	if((hFind = FindFirstFile(pth, &FindFileData)) != INVALID_HANDLE_VALUE)
	{
		oldCount++;

		while(FindNextFile(hFind, &FindFileData))
		{
			oldCount++;
		}

		FindClose(hFind);
	}

	while(true) 
    {
		// call to WaitForSingleObject has a 5 second timeout
		unsigned long stat = WaitForSingleObject(hNotify, 5000);
      
		// If it got signalled
		if (stat == WAIT_OBJECT_0) 
		{
			int count = 0;
			if((hFind = FindFirstFile(pth, &FindFileData)) != INVALID_HANDLE_VALUE)
			{
				count++;

				while(FindNextFile(hFind, &FindFileData))
				{
					count++;
				}

				FindClose(hFind);
			}

			if(count > oldCount)
			{
				// Close our old handle
				if (hNotify != INVALID_HANDLE_VALUE)
				{
					FindCloseChangeNotification(hNotify);
				}

				return (jboolean) true;
			}

			// Renew the handle
			if (! FindNextChangeNotification(hNotify))
			{
				return (jboolean) false;
			}
		}
	}

	
}


// Exported function
JNIEXPORT jboolean JNICALL Java_Test_waitForAnyFile(JNIEnv * env, jobject obj, jstring _directory)
{
	const char *directory = env->GetStringUTFChars(_directory, 0);

    const unsigned long dwFlags =	FILE_NOTIFY_CHANGE_FILE_NAME;//  |
									//FILE_NOTIFY_CHANGE_DIR_NAME   |
									//FILE_NOTIFY_CHANGE_ATTRIBUTES |
									//FILE_NOTIFY_CHANGE_SIZE       |
									//FILE_NOTIFY_CHANGE_LAST_WRITE;

	HANDLE hNotify = FindFirstChangeNotification(directory, false, dwFlags);
	env->ReleaseStringUTFChars(_directory, directory);

    // Get the handle to the event object
    if (hNotify == INVALID_HANDLE_VALUE)
	{
		return (jboolean) false;
	}

	while(true) 
    {
		// call to WaitForSingleObject has a 5 second timeout
		unsigned long stat = WaitForSingleObject(hNotify, 5000);
      
		// If it got signalled
		if (stat == WAIT_OBJECT_0) 
		{
			return (jboolean) true;

			// Renew the handle
			if (! FindNextChangeNotification(hNotify))
			{
				return (jboolean) false;
			}
		}
	}

	
}



// Entry point
BOOL APIENTRY DllMain(HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
{
	switch(ul_reason_for_call)
	{
		case DLL_PROCESS_ATTACH:
			return TRUE;

		case DLL_PROCESS_DETACH:
			return TRUE;
	}

    return TRUE;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?