|
-
April 3rd, 2005, 02:29 AM
#1
How to find the running program and send some message to it?
Hi!
I need such:
When my program starting it must find ether other copy of the program presents in the system or no.
If it presents the program must maximize it and exit.
How can I do so?
Thanks for help,
Alex.
-
April 3rd, 2005, 12:30 PM
#2
Re: How to find the running program and send some message to it?
-Use the System.Diagnostics.Process class to get a list of running processes by name, handle, or window handle. Or if you prefer, inspect all window titles to find your process.
-Look out to catch System.Diagnostics.Win32Exception
-Determine which instance of your program to send messages to by inspecting the process start time. (Check the 'Ticks' property of DateTime. That should be more accurate)
-Use Process.CloseMainWindow() or Process.Kill() to exit whichever instance you need to exit.
Last edited by kin2pete; April 3rd, 2005 at 12:47 PM.
-
April 4th, 2005, 11:23 AM
#3
Re: How to find the running program and send some message to it?
OK, all excellent, but I cannot remove other problem:
1) I have hidden program (Form.Hide())
2) I have found it
3) I try show it by
int hWnd = localByName[nIndex].MainWindowHandle.ToInt32();
Win32API.SetForegroundWindow(hWnd);
Win32API.ShowWindow(hWnd, Win32API.SW_SHOW);
where all is simple calling of API functions.
But it works only on minimized window, not hidden.
It looks as it try but the form know that it is hidden and hides again.
How can I set the form of other programm to Show()?
Thanks,
Alex.
-
April 4th, 2005, 12:43 PM
#4
Re: How to find the running program and send some message to it?
I'm not familiar with the Win32 API so I really can't help (at least now). If I come across a solution I will be sure to post it.
But for one, don't convert obj.MainWindowHandle to int. Your Win32 API function should have a prototype as
[DllImport("User32.dll")]
<whatever goes here> SetForegroundWindow(IntPtr hWnd);
The marshalling type for HWND is System.IntPtr
Same goes for ShowWindow(IntPtr hWnd......);
Last edited by kin2pete; April 4th, 2005 at 01:04 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|