Error Code 87
-
Hello. I Know this isnt quite right forum to ask. The App is C#, however i am using Native WinAPI. I am trying to list all process, and get full path. PROCESSENTRY32 in 95/98 used full path for exe. But not on newer system. MODULEENTRY32 Have szExePath. I wanted to use this to get full path. I Used CreateToolhelp32Snapshot method to emurate process. (The only method that gave me good results). But to use Module32First() i need to open process, but i get error 87. On MSDN i found it is ERROR_INVALID_PARAMETER. Is there a whay to fix this problem, or a diffrent method of getting full path. And no, no searching files through HDD.(I am intrested in the first solution) Thanks in advance.
int hWnd = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 pe = new PROCESSENTRY32();
pe.dwSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(PROCESSENTRY32));int first = Process32First(hWnd, ref pe);
int error = GetLastError();
if (first == 0)
throw new ProcessListException("Can not get process list");do
{
ProcessInfo pi = new ProcessInfo();
pi.iProcessID = (int)pe.th32ProcessID;
pi.sFileName = pe.szExeFile;
SetLastError(0); // To Reset any Error Codes
int handle = OpenProcess(PROCESS_ALL_ACCESS, true, pi.iProcessID);
// 87 = GetLastError();
MODULEENTRY32 me = new MODULEENTRY32();
me.dwSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(MODULEENTRY32));
bool t = Module32First(handle, ref me);
} while (Process32Next(hWnd, ref pe) != 0);
CloseHandle(hWnd);Edit: Styling
-
Hello. I Know this isnt quite right forum to ask. The App is C#, however i am using Native WinAPI. I am trying to list all process, and get full path. PROCESSENTRY32 in 95/98 used full path for exe. But not on newer system. MODULEENTRY32 Have szExePath. I wanted to use this to get full path. I Used CreateToolhelp32Snapshot method to emurate process. (The only method that gave me good results). But to use Module32First() i need to open process, but i get error 87. On MSDN i found it is ERROR_INVALID_PARAMETER. Is there a whay to fix this problem, or a diffrent method of getting full path. And no, no searching files through HDD.(I am intrested in the first solution) Thanks in advance.
int hWnd = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 pe = new PROCESSENTRY32();
pe.dwSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(PROCESSENTRY32));int first = Process32First(hWnd, ref pe);
int error = GetLastError();
if (first == 0)
throw new ProcessListException("Can not get process list");do
{
ProcessInfo pi = new ProcessInfo();
pi.iProcessID = (int)pe.th32ProcessID;
pi.sFileName = pe.szExeFile;
SetLastError(0); // To Reset any Error Codes
int handle = OpenProcess(PROCESS_ALL_ACCESS, true, pi.iProcessID);
// 87 = GetLastError();
MODULEENTRY32 me = new MODULEENTRY32();
me.dwSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(MODULEENTRY32));
bool t = Module32First(handle, ref me);
} while (Process32Next(hWnd, ref pe) != 0);
CloseHandle(hWnd);Edit: Styling
Hi, I'm no interopability expert, but error 87 means "INVALID_PARAMETER". I wonder if the C# Bool in
OpenProcess(PROCESS_ALL_ACCESS, true, pi.iProcessID);
translates to a Microsoft C TRUE which is a preprocessor define on 1. Maybe you should try this instead
OpenProcess(PROCESS_ALL_ACCESS, 1, pi.iProcessID);
So long, Stefan
-
Hello. I Know this isnt quite right forum to ask. The App is C#, however i am using Native WinAPI. I am trying to list all process, and get full path. PROCESSENTRY32 in 95/98 used full path for exe. But not on newer system. MODULEENTRY32 Have szExePath. I wanted to use this to get full path. I Used CreateToolhelp32Snapshot method to emurate process. (The only method that gave me good results). But to use Module32First() i need to open process, but i get error 87. On MSDN i found it is ERROR_INVALID_PARAMETER. Is there a whay to fix this problem, or a diffrent method of getting full path. And no, no searching files through HDD.(I am intrested in the first solution) Thanks in advance.
int hWnd = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 pe = new PROCESSENTRY32();
pe.dwSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(PROCESSENTRY32));int first = Process32First(hWnd, ref pe);
int error = GetLastError();
if (first == 0)
throw new ProcessListException("Can not get process list");do
{
ProcessInfo pi = new ProcessInfo();
pi.iProcessID = (int)pe.th32ProcessID;
pi.sFileName = pe.szExeFile;
SetLastError(0); // To Reset any Error Codes
int handle = OpenProcess(PROCESS_ALL_ACCESS, true, pi.iProcessID);
// 87 = GetLastError();
MODULEENTRY32 me = new MODULEENTRY32();
me.dwSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(typeof(MODULEENTRY32));
bool t = Module32First(handle, ref me);
} while (Process32Next(hWnd, ref pe) != 0);
CloseHandle(hWnd);Edit: Styling
In addition to Stefan's reply... First, you shouldn't be calling GetLastError() unless a return value indicates an error actually occurred. If no error occurred, the return value of GetLastError() is undefined in many cases. SetLastError() may have no effect if you're calling a Windows API right after you use it. You have no idea how many times the error code for the thread may change during that one API call. Second, what do your marshaled structs look like? Are you marshaling them properly? Third, you should be using IntPtr types for handles instead of int. Mark
-
In addition to Stefan's reply... First, you shouldn't be calling GetLastError() unless a return value indicates an error actually occurred. If no error occurred, the return value of GetLastError() is undefined in many cases. SetLastError() may have no effect if you're calling a Windows API right after you use it. You have no idea how many times the error code for the thread may change during that one API call. Second, what do your marshaled structs look like? Are you marshaling them properly? Third, you should be using IntPtr types for handles instead of int. Mark
Mark Salsbery wrote:
First, you shouldn't be calling GetLastError() unless a return value indicates an error actually occurred. If no error occurred, the return value of GetLastError() is undefined in many cases. SetLastError() may have no effect if you're calling a Windows API right after you use it. You have no idea how many times the error code for the thread may change during that one API call.
I Used SetlastError(0) to clear error from before. It was error 1008. And i called it after OpenProcess, i got error 87. Yust to make sure where it fails. And in a loop, the OpenProcess Failed all the time.
Mark Salsbery wrote:
Second, what do your marshaled structs look like? Are you marshaling them properly?
I didnt use it properly.
Mark Salsbery wrote:
Third, you should be using IntPtr types for handles instead of int.
Changed it. And now it works. Thank you