From 11:00 PM CST Friday, Apr 11th - 1:30 PM CST Saturday, Apr 12th, ni.com will undergo system upgrades that may result in temporary service interruption.
We appreciate your patience as we improve our online experience.
From 11:00 PM CST Friday, Apr 11th - 1:30 PM CST Saturday, Apr 12th, ni.com will undergo system upgrades that may result in temporary service interruption.
We appreciate your patience as we improve our online experience.
09-04-2020 03:10 PM
I have created a small program that goes out to the Windows API and returns the active window title. This works OK as I can parse the suffix of the window title string that it returns and use a lookup table to tell me what program is running.
I want to take it a step farther. I want to return the application that is running instead of the window title. I've added the GetWindowThreadProcessID which, I think, returns the Thread ID of the active window.
Based on that, is there a way to return the application name based on the window thead ID? I am not very well versed in the Windows API but wondered if someone has tackled this before and may have found a way to get this information.
Solved! Go to Solution.
09-07-2020 11:30 PM
Take a look this example
https://docs.microsoft.com/en-us/windows/win32/psapi/enumerating-all-processes
09-08-2020 02:00 AM
I have written something similar in the past which uses the command line tasklist command to get window titles. This filters all running tasks bu the executable name, but by the looks of it there is a PID filter which would allow you to filter by process ID instead.
09-08-2020 03:24 AM - edited 09-08-2020 03:26 AM
If you just want the current application's name:
What is your end goal?
09-08-2020 09:40 AM
I thought about using your example, but I don't think its that simple.
My end goal is to look up what application is used to run said window and it would return LabVIEW, Excel, Access, etc.
For example, if a Excel workbook is open and you run my example, you'll get what (I think) is the ProcessID for that workbook. I thought I could use that to lookup the PID from the Task Manager but, when I did that, I couldn't find that specific proccess ID. It must be a child window ID.
What I want it to do is to tell me what application it is using to run that workbook, so it would return Excel (or Microsoft Excel). Currently, it gives me the Active Workbook name.
If there isn't a solution, that's fine, I have a lookup table to tell me that a XLSX file is Excel and a VI is LabVIEW, but I wondered if there was another solution that I could eliminate that lookup table. I am not too well versed in the Windows API or if there is a .NET solution (or something else), I don't know what I need to choose.
09-08-2020 10:03 AM
From a hwnd, get the lpdwProcessID like this:
uint lpdwProcessId;
GetWindowThreadProcessId(hWnd, out lpdwProcessId);
From a lpdwProcessID, get it's name like this:
IntPtr hProcess = OpenProcess(0x0410, false, lpdwProcessId);
StringBuilder text = new StringBuilder(1000);
//GetModuleBaseName(hProcess, IntPtr.Zero, text, text.Capacity);
GetModuleFileNameEx(hProcess, IntPtr.Zero, text, text.Capacity);
CloseHandle(hProcess);
09-09-2020 09:25 AM
I'll admit, I am not versed in the Windows API data types at all. Where do I find this information so I can assign them correctly?
DWORD GetModuleFileNameExA( HANDLE hProcess, HMODULE hModule, LPSTR lpFilename, DWORD nSize );
09-09-2020 10:04 AM - edited 09-09-2020 10:06 AM
@Eric1977 wrote:
I'll admit, I am not versed in the Windows API data types at all. Where do I find this information so I can assign them correctly?
DWORD GetModuleFileNameExA( HANDLE hProcess, HMODULE hModule, LPSTR lpFilename, DWORD nSize );
That one is tricky. GetModuleFileNameA is in Kernel32.dll, but that's not the right one. GetModuleFileNameExA is in in Psapi.dll. This kind of information is usually found in the MSDN:
https://docs.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi-getmodulefilenameexa. Under DLL...
Here's a quick and dirty VI:
Error handling should be added (using the return values of the calls).
It will work faster under "any thread".
Tested under 64 bit. 32 bit should work, but might not.
Not sure how and why, but the calling convention doesn't matter. I think they should be "WinAPI", but I get the same results if all calls are set the "C"... Are those nodes smart enough to accept either? This might be a 64 bit thing. Or is LabVIEW overruling my judgement? Something to look into...
09-10-2020 11:49 AM
I can't seem to drag your image to a blank block diagram. It always comes in as a image.
09-11-2020 02:11 AM
@Eric1977 wrote:
I can't seem to drag your image to a blank block diagram. It always comes in as a image.
That's because the forum shrinks the image to save bandwidth. In the shrinking process, it removes the embedded VI section.
Now it should have the embedded VI.
Anyway, I showed all names, so it could be replicated as well 😉.