Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get name of all handles in current process

I need to list all open handles in current process. Since i could not find any function like "EnumHandles", I was thinking of making a loop from 0 to 1000. The question is how i can retrieve the name of each handle? I am using c++ and the OS is Win7 32-bit EDIT: The handle I need name of is a Mutex. By comparing the name of the mutex, i want to get the handle id I seem to have found solution using OpenMutex, but i don't know what to pass on 3rd parameter,

like image 941
WePro2 Avatar asked Jan 03 '12 22:01

WePro2


People also ask

How do I find window process handles?

If you have a process identifier, you can get the process handle by calling the OpenProcess function. OpenProcess enables you to specify the handle's access rights and whether it can be inherited. A process can use the GetCurrentProcess function to retrieve a pseudo handle to its own process object.

What is GetCurrentProcess?

The GetCurrentProcess function retrieves a pseudo-handle for the current process, which is currently defined as (HANDLE)-1 . However, because you should not assume that the value will never change, the GetCurrentProcess function is provided as an alternative to hard-coding the constant into your code.

What is pseudo handle?

A pseudo handle is a special constant, currently (HANDLE)-1, that is interpreted as the current process handle. For compatibility with future operating systems, it is best to call GetCurrentProcess instead of hard-coding this constant value.

What is a handle and how does a process obtain a handle?

A process handle is an integer value that identifies a process to Windows. The Win32 API calls them a HANDLE; handles to windows are called HWND and handles to modules HMODULE. Threads inside processes have a thread handle, and files and other resources (such as registry keys) have handles also.


1 Answers

I believe you have to use the NTDLL.DLL. To my knowledge this is what all tools monitoring processes, handles and other system information, have to use in the end, under Windows. I used it in a small Win32 tool, however never had to list handles.

Check here for a good intro of that library and related to your question. http://forum.sysinternals.com/howto-enumerate-handles_topic18892.html

Also the GetObjectName function in the first post of http://forum.sysinternals.com/enumerate-opened-files_topic3577.html

Accessing this kind of information in Windows may seem to be a lot of work and looks frightening because Microsoft does not want to support it, but you will see that when the 'easy' API is not giving you what you need, you have to dig to NTDLL. This is what tools like ProcessExplorer use in the end. It is not so hard to use: load the DLL, get the right function pointers to fill the structs that you declare yourself with what you will find on the net.

like image 96
fury Avatar answered Oct 31 '22 02:10

fury