From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Get application name via Windows API or another source

Solved!
Go to solution

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.

0 Kudos
Message 1 of 15
(4,271 Views)
0 Kudos
Message 2 of 15
(4,181 Views)

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.

0 Kudos
Message 3 of 15
(4,175 Views)

If you just want the current application's name:

Application Name.png

 

What is your end goal?

Message 4 of 15
(4,171 Views)

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.

0 Kudos
Message 5 of 15
(4,146 Views)

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); 

 

 

https://stackoverflow.com/questions/2635404/how-to-get-process-name-and-title-of-the-top-window-on-w...

 

 

Message 6 of 15
(4,144 Views)

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 );

0 Kudos
Message 7 of 15
(4,104 Views)

@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:

GetModuleFileNameExA.png

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... 

0 Kudos
Message 8 of 15
(4,097 Views)

I can't seem to drag your image to a blank block diagram. It always comes in as a image.

0 Kudos
Message 9 of 15
(4,073 Views)

@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.

 

  1. Copy the image's link:/t5/image/serverpage/image-id/272987iD312656E0CA750CC/image-size/large?v=1.0&px=999
  2. Remove /image-size...: /t5/image/serverpage/image-id/272987iD312656E0CA750CC
  3. Save this image.

 

Now it should have the embedded VI.

 

Anyway, I showed all names, so it could be replicated as well 😉.

0 Kudos
Message 10 of 15
(4,058 Views)