Is there a way to list all open file handles for a certain process using the pid?

I know there are tools that do that but can I do with C++ (or C/C#/VB) in order to be able to add the feature to my program?

Hello,

I just found this one. Might help you:

#include <iostream>
#include <windows.h>

// struct unit with loop inside....

using namespace std;

int main(int argc, char *argv[])
{

HWND h = ::GetTopWindow(0 );
while ( h )
{
  DWORD pid;
  DWORD dwTheardId = ::GetWindowThreadProcessId( h,&pid);



         if ( pid == /*your process id*/ )
         {
    // here h is the handle to the window
              break;
         }
         h = ::GetNextWindow( h , GW_HWNDNEXT);
}
}

Hello,

I just found this one. Might help you:

#include <iostream>
#include <windows.h>

// struct unit with loop inside....

using namespace std;

int main(int argc, char *argv[])
{

HWND h = ::GetTopWindow(0 );
while ( h )
{
  DWORD pid;
  DWORD dwTheardId = ::GetWindowThreadProcessId( h,&pid);



         if ( pid == /*your process id*/ )
         {
    // here h is the handle to the window
              break;
         }
         h = ::GetNextWindow( h , GW_HWNDNEXT);
}
}

I can't run this code. Am I missing something?

Nope. Just look through that link and see if anything jumps out at you. I don't even know if it contains anything useful.

This is how to get a list of all running processes and their thread IDs (MS-Windows doesn't use PIDs). I don't have time to research this right now, but there might be a windows function to enumerate the open file handles given the process ID.

Edited 6 Years Ago by Ancient Dragon: n/a

Jimmy,

I did not add the pid there on line 19. Add it. And once it finds the process id, h will be the handle.

Or please post the error you come across.

Jimmy,

I did not add the pid there on line 19. Add it. And once it finds the process id, h will be the handle.

Or please post the error you come across.

Yes I know I've noticed that. I find the pid of a known process and put it there but I get the following errors:

(I am using Visual Studio 2008)


1>main.obj : error LNK2028: unresolved token (0A000276) "extern "C" struct HWND__ * __stdcall GetTopWindow(struct HWND__ *)" (?GetTopWindow@@$$J14YGPAUHWND__@@PAU1@@Z) referenced in function "int __cdecl main(int,char * * const)" (?main@@$$HYAHHQAPAD@Z)
1>main.obj : error LNK2028: unresolved token (0A000312) "extern "C" unsigned long __stdcall GetWindowThreadProcessId(struct HWND__ *,unsigned long *)" (?GetWindowThreadProcessId@@$$J18YGKPAUHWND__@@PAK@Z) referenced in function "int __cdecl main(int,char * * const)" (?main@@$$HYAHHQAPAD@Z)
1>main.obj : error LNK2028: unresolved token (0A000314) "extern "C" struct HWND__ * __stdcall GetWindow(struct HWND__ *,unsigned int)" (?GetWindow@@$$J18YGPAUHWND__@@PAU1@I@Z) referenced in function "int __cdecl main(int,char * * const)" (?main@@$$HYAHHQAPAD@Z)
1>main.obj : error LNK2019: unresolved external symbol "extern "C" struct HWND__ * __stdcall GetWindow(struct HWND__ *,unsigned int)" (?GetWindow@@$$J18YGPAUHWND__@@PAU1@I@Z) referenced in function "int __cdecl main(int,char * * const)" (?main@@$$HYAHHQAPAD@Z)
1>main.obj : error LNK2019: unresolved external symbol "extern "C" unsigned long __stdcall GetWindowThreadProcessId(struct HWND__ *,unsigned long *)" (?GetWindowThreadProcessId@@$$J18YGKPAUHWND__@@PAK@Z) referenced in function "int __cdecl main(int,char * * const)" (?main@@$$HYAHHQAPAD@Z)
1>main.obj : error LNK2019: unresolved external symbol "extern "C" struct HWND__ * __stdcall GetTopWindow(struct HWND__ *)" (?GetTopWindow@@$$J14YGPAUHWND__@@PAU1@@Z) referenced in function "int __cdecl main(int,char * * const)" (?main@@$$HYAHHQAPAD@Z)

Well.. Anyone to point to me my mistakes for the code above?

This article has been dead for over six months. Start a new discussion instead.