SteveMyres Posted November 5, 2012 Share Posted November 5, 2012 Is there a way to get a list of running Windows processes, or check if one with a known name is running? There is an associated exe that needs to be running or DAQ Factory shouldn't do certain things. Link to comment Share on other sites More sharing options...
AzeoTech Posted November 6, 2012 Share Posted November 6, 2012 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 More sharing options...
SteveMyres Posted November 6, 2012 Author Share Posted November 6, 2012 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 More sharing options...
AzeoTech Posted November 6, 2012 Share Posted November 6, 2012 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 More sharing options...
SteveMyres Posted December 6, 2012 Author Share Posted December 6, 2012 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 More sharing options...
AzeoTech Posted December 6, 2012 Share Posted December 6, 2012 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 More sharing options...
AzeoTech Posted December 6, 2012 Share Posted December 6, 2012 BTW: make sure you save before you actually run your script. If you do extern() with the wrong parameters and then try and call the function you just imported, you very likely will crash DAQFactory hard. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.