diff --git a/native/envvar-cmdline.cpp b/native/envvar-cmdline.cpp index 977a14e..f8f4425 100644 --- a/native/envvar-cmdline.cpp +++ b/native/envvar-cmdline.cpp @@ -169,17 +169,20 @@ jstring getCmdLineAndEnvVars( // There is a risk that somebody starts the 32bit DLL in x64 process, but within WinP JAR it must not happen. // TODO: Consider adding defensive logic just in case BOOL procIsWow64 = FALSE; - if (!IsWow64Process(hProcess, &procIsWow64)) + if (fnIsWow64Process != NULL) { - reportError(pEnv, "Failed to determine if the process is a 32bit or 64bit executable"); - return NULL; - } - - if (!procIsWow64) { - // We are trying to query a 64-bit process from a 32-bit DLL - sprintf_s(errorBuffer, "Process with pid=%d is not a 32bit process (or it is not running). Cannot query it from a 32bit library", pid); - reportErrorWithCode(pEnv, 2, errorBuffer); - return NULL; + if (!fnIsWow64Process(hProcess, &procIsWow64)) + { + reportError(pEnv, "Failed to determine if the process is a 32bit or 64bit executable"); + return NULL; + } + + if (!procIsWow64) { + // We are trying to query a 64-bit process from a 32-bit DLL + sprintf_s(errorBuffer, "Process with pid=%d is not a 32bit process (or it is not running). Cannot query it from a 32bit library", pid); + reportErrorWithCode(pEnv, 2, errorBuffer); + return NULL; + } } #endif @@ -194,7 +197,17 @@ jstring getCmdLineAndEnvVars( // from there to PEB PEB ProcPEB; if(!ReadProcessMemory(hProcess, ProcInfo.PebBaseAddress, &ProcPEB, sizeof(ProcPEB), &sRead)) { - reportError(pEnv, "Failed to read PEB"); +#ifndef _WIN64 + if (fnIsWow64Process == NULL) { + // We are unable to determine it, no API call available + reportError(pEnv, "Failed to read PEB. Probably the process is 64bit, which cannot be read from the 32bit WinP DLL"); + } + else { +#endif + reportError(pEnv, "Failed to read PEB"); +#ifndef _WIN64 + } +#endif return NULL; } diff --git a/native/runtime.cpp b/native/runtime.cpp index f22eec9..b292bb8 100644 --- a/native/runtime.cpp +++ b/native/runtime.cpp @@ -1,7 +1,12 @@ #include "stdafx.h" +#include "winp.h" + +LPFN_ISWOW64PROCESS fnIsWow64Process; extern "C" BOOL WINAPI _DllMainCRTStartup(HANDLE hDllHandle, DWORD dwReason, LPVOID lpreserved) { + fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress( + GetModuleHandle(TEXT("kernel32")), "IsWow64Process"); return TRUE; } diff --git a/native/winp.h b/native/winp.h index 0c1feb6..1c197c5 100644 --- a/native/winp.h +++ b/native/winp.h @@ -9,12 +9,14 @@ void error(JNIEnv* env, const char* file, int line, const char* msg); // // Kernel32.dll // + BOOL WINAPI KillProcessEx(IN DWORD dwProcessId, IN BOOL bTree); + // https://msdn.microsoft.com/en-us/library/ms684139.aspx -extern "C" BOOL WINAPI IsWow64Process(HANDLE, PBOOL); -// https://msdn.microsoft.com/en-us/library/ms683189(VS.85).aspx -//BOOL WINAPI GetExitCodeProcess(HANDLE, LPDWORD); -//VOID WINAPI SetLastError(DWORD); +typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL); +// Reference to the IsWow64Process method. +// It is being handled via the reference, because the method is not available for the non-desktop-app mode (e.g. Windows service or AppVeyor build) +extern LPFN_ISWOW64PROCESS fnIsWow64Process; // // NTDLL functions