I need to obtain the Handle to a process using its
Image Name so that I can use it with TerminateProcess
function.
Any help will be appreciated
Farid
Use CreateToolhelp32Snapshot and Process32First/Next to iterate the running processes. Figuring out which one you'd want to abort if there's more than one instance of the process is of course not really possible.
Related
Say I want to write a tail like application for Windows to monitor a bunch of files. Such an application should report when some of the monitored files is updated by any other application.
It can be assumed that the files being monitored are being constantly appended by other processes, but not modified in any other way. Before implementing some pooling solution (that is, iterate through the files to be monitored, seek to the end of each one, record this pointer, compare to previous end etc.) I would appreciate if someone more experienced with the Overlapped IO could tell me if I can make use of it.
For instance, is it possible to write the monitoring application in such a way that it opens all the files that need to be monitored, seek to the end of them, and try to read one byte with ReadFileEx() registering a callback.
Is there a way to make this work so that when another process write to some of the files the proper callback is invoked? Or necessarily the monitoring application will always get an EOF for such a call?
Is this approach a sensible one? Or is it a bad idea?
I would like to enumerate all the instances of an application and determine which instance was created first (oldest). Given a list of HWNDs that belong to the process instances, can I sort the list to determine the order of creation? If not, is there another way?
EDIT 1 : the windows being enumerated are not created by my process, they were created long before my process started execution.
EDIT 2: As mentioned in the comments, I am interested in the creation time of the processed. I need the HWND of he main window of the oldest instance of the application. Not sure how to get the HWND from the process ID.
There are two different ways you can approach this:
Start with the windows
Use EnumWindows(), or a FindWindow/Ex() loop, to find the candidate app windows you are interested in. In the case of EnumWindows(), you can use things like GetClassName() and GetWindowText() in the callback.
Use GetWindowThreadProcessId() to get each window's PID.
Use OpenProcess() to open each PID, and GetProcessTimes() to get its creation time.
Now you can sort the times to get the oldest, and you will know the window(s) that go with the corresponding process.
Start with the processes
Use EnumProcesses(), or a Process32(First|Next)() loop, to find the PIDs of each instance of the app path+filename you are interested in.
Use OpenProcess() and GetProcessTimes() to get their creation times, and then sort them.
Then, with the oldest PID, you can enumerate windows looking for the one(s) that belong to that PID. You can either:
enumerate all windows as above, using GetWindowThreadProcessId() to look for the PID.
use EnumThreadWindows() on each thread of the process. To get the process's thread IDs, you can use a Thread32(First|Next)() loop.
Optionally, assuming the process's main thread is the one creating the window(s) you want, you can limit the window enumeration to just that thread. Enumerate the pricess's threads, using OpenThread() and GetThreadTimes() to find the oldest thread ID, which will be the main thread.
I wanted to know is there any way to retrieve all the handles associated with a process using process ID in VBScript ?
I dont know about VBScript, but the WinAPI Functions to use would be
Thread32First/Thread32Next in order to iterate over all
threads line in this (c++) sample
EnumThreadWindows
I have created a bash script and it runs in the background. It has a PID which is stored in a file, and I can use KILL to pass predefined signals to the process.
From time to time however, I'd like to pass information to the process manually. Preferably what I would like to happen is to be able to pass a string or array of information, which is captured through TRAP, then the forever loop inside the bash file will process the information. Is there an easy way to pass information into a background process?
Thanks
You can create a fifo, have the main process write to it and have the child read from it.
mkfifo link
run_sub < link &
generate_output > link
Have it listen on a socket and implement a protocol to achieve your communication aims, probably a bit much for bash.
Or, have it try to read a particular file on receipt of a particular signal. For example, it is common for programs to re-read their configuration files on receipt of a HUP.
I am changing the architecture of a VB 6 scheduling application from serial execution architecture to parallel execution and I need to do this with as little code changes as possible. Basically, the first instance of the .exe will start a defined amount of additional instances.
One of the changes required is to update the job table with the PID of the instance that is executing the job. I have searched but I have not been able to find a way to get this when multiple instances of the same .exe may be running.
How can I get the process ID of the current process?
Put in this into your code:
Declare Function GetCurrentProcessId Lib "kernel32" Alias "GetCurrentProcessId" () As Long
Do it the same way a program in any other language would do it: Call GetCurrentProcessId.