Click here to Skip to main content
15,072,311 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,

I have some opened directories and application appear in taskbar. How to get path of them?

For example: There are 1 opened directory: C:\Windows and 2 application D:\Data\Word.doc and D:\Data\text.txt.

I want to get path of them by their Window Handle by calling EnumWindow

Do you have any solution?

Thank you
Posted

I can help get you started:

Start by querying for the processID that owns the window you are querying

GetWindowThreadProcessId[^]

Then open a handle with PROCESS_QUERY_INFORMATION access to the process:

OpenProcess[^]

With the process handle, you can get the name of the application that is running the process:

GetProcessImageFileName[^]

To actually get the path of some open file, it will most likely be program dependent for your information to be reliable. Possibly Explorer.exe changes the working directory for the current folder, but I dont know for sure. Document based programs you could possibly look at the open file handles for that process, but there is no guarantee for that either.

For Word and MS Office based apps specifically, I would investigate their scripting objects. They will most likely be able to report which document they are using for each process instance.
   
Comments
SVPro 13-Nov-11 0:16am
   
Thank Paul,

but there is a linking error: [Linker error] undefined reference to 'GetProcessImageFileName' (Dev-C)

I included "psapi.h" but above error still appear.
Paul M Watt 13-Nov-11 0:47am
   
In that case you will just need to add the .lib file into your linker settings on the properties dialog for your project, linker tab, input settings.
Here are the names listed based on the operating system you are using on the reference page for the API:

Kernel32.lib on Windows 7 and Windows Server 2008 R2;
Psapi.lib if PSAPI_VERSION=1 on Windows 7 and Windows Server 2008 R2;
Psapi.lib on Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP/2000

Good Luck
SVPro 13-Nov-11 1:18am
   
Where's Psapi.lib?
I added Psapi.dll from system32 but there's nothing, it's the same error
Paul M Watt 13-Nov-11 1:44am
   
What compiler are you using?

You will need to search for psapi.lib on your machine, and make sure that directory is included in your compiler linker settings. Then you should just be able to reference the llib file. Linking against the dll will not work.

If you are using a compiler that does not have psapi.lib, you can still dynamically load the psapi.dll, and then user GetProcAddress to load the functions and call them that way. linking is easier if you can find the lib file.
SVPro 13-Nov-11 1:56am
   
I'm using Dev-C
Paul M Watt 13-Nov-11 2:16am
   
Have you downloaded and installed the Windows Development SDK? The lib file should be in that set of header files and libraries.
If you still have troubles finding that link library, here is the code (I have not compiled or tested, but adapted from another sample I have) to dynamically load and link to the function you are after:

typedef DWORD (WINAPI *FPGetProcessImageFileName)(HANDLE,LPTSTR,DWORD);

HINSTANCE hinstLib;
FPGetProcessImageFileName fpGetProcessImageFileName = NULL;

// Get a handle to the DLL module.
hinstLib = ::LoadLibrary(TEXT("psapi.dll"));
if (hinstLib != NULL)
{
fpGetProcessImageFileName = (FPGetProcessImageFileName) ::GetProcAddress(hinstLib, _T("GetProcessImageFileName"));

// If the function address is valid, call the function.
if (NULL != fpGetProcessImageFileName)
{
// The function loaded successfully.
// You know have a function pointer that points to that function in the loaded DLL.
// You can call it like this:
HANDLE hProcess; // Initialized from the previous calls
...

TCHAR buffer[_MAX_PATH];

fpGetProcessImageFileName(hProcess, buffer, _MAX_PATH);
}
}

...
// Free the DLL when you are done.
::FreeLibrary(hinstLib);
You probably getting an error because you didn't specify version of windows api you want.

Example to enable windows 7 level api:

#define WINVER 0x0601
#include <windows.h>



Source:http://msdn.microsoft.com/en-us/library/6sehtctf.aspx
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900