Windows Process Detection


SteveMyres

Recommended Posts

There isn't a way to do it with a built in DAQFactory function, but you can write some C code to do it (in a DLL) and then call it from DAQFactory, or you can figure out which Windows DLL holds the FindWindow() function and load it directly using extern() and avoid writing any C code. The general format is simply:

HWND hWnd = ::FindWindow("windowName", NULL);

If hWnd != NULL, then its open, otherwise its closed (couldn't be found). Looks like its contained in User32.dll. See: http://msdn.microsoft.com/en-us/library/windows/desktop/ms633499(v=vs.85).aspx. You can use the second parameter to find the window by title (what is displayed in the top bar of the application window), but the title is often variable, such as displaying the currently open document in DAQFactory, or a * to indicate an edited document. Using the window name is more reliable, but often the window name isn't what you'd think it was. It often has something like "MainDlgClass" or similar at the end. It all depends on the application. I typically use Spy++ or similar window browsing app to figure it out. That tool is part of Visual C++.

Link to comment
Share on other sites

Well, I really want something to be able to enumerate processes rather than windows, because I'm hoping I can get the other program running hidden. I tried running it from DAQ Factory which would take care of running hidden as well as knowing it's running, but then if you quit DAQ Factory, the other program locks up, so I put it in HKLM instead. I realize in an orderly shutdown, you could terminate the other program first.

Link to comment
Share on other sites

You can still find windows that are hidden. Pretty much every user app in Windows runs in a window, even if its hidden. BTW: if you can find the app using ::FindWindow, you can use the returned handle and send the other app a message to close:

::PostMessage(hWnd, WM_CLOSE, 0, 0)

Of course you'll need to figure out what WM_CLOSE maps to. I'm just giving you C++ code from inside DF.

Link to comment
Share on other sites

  • 5 weeks later...

I'm having trouble building the prototype. If I call

extern("C:\WINDOWS\system32\user32.dll", "HWND FindWindow(string[256], string[256])", "FindWindow", "stdcall")[/CODE]

it says "Invalid argument specified.: Line 1", but if I call

[CODE]extern("C:\WINDOWS\system32\user32.dll", "FindWindow(string[256], string[256])", "FindWindow", "stdcall")[/CODE]

it says "Pointer not allowed as a return type.: Line 1"

I also tried

[CODE]System.ShellExecute("Tasklist /fi " + chr(34) + "imagename eq MyProcess.exe" + chr(34) + " > " + chr(34) + "C:\tasklist.txt" + chr(34))[/CODE]

Then checking the size of C:\tasklist.txt, which will be zero if the process isn't running, which works, but seems like a kludge, and slow. I haven't figured out anyway to pipe the output directly back to DAQ Factory. Any other ideas, or guidance on the FindWindow() prototype?

Link to comment
Share on other sites

HWND isn't a valid return type. Its defined somewhere in the windows header files, but DAQFactory doesn't know that. The return types must be one of the types listed in the DF docs for extern(). Technically HWND is usually just void *, which to DAQFactory can basically be treated as an unsigned 32 bit integer since DF is running as a 32 bit app.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.