Click here to Skip to main content
15,906,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,I have a problem.

I running an .exe application from desktop with vb.net,and that application has a MenuItem Header which name is "File" which consist of few MenuItem Commands ex. Copy,Paste,Save,Save As...
How to run command "Save" on that application using a vb.
Save command work with "CTRL+S",but how to run that command on application which is on desktop using my vb application???
Posted

1 solution

Generally, this is quite a bad idea, a dirty field of programming, nothing really reliable or supportable. Some applications develops a feature called "Automation" to be controlled by some external process, but such applications should be specially designed for this and provide appropriate API. To me, automation is still dirty stuff, but it is used sometimes and is considered legitimate.

If this is a general-purpose UI application, and you hard life really forces you into doing bad stuff, you need three things:

1) Run application using System.Diagnostics.Process.Start, see http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx[^].

2) When a process is started, you can use the property Process.MainWindowHandle to manipulate it using raw Windows API such as SetForegroundWindow, SetActiveWindow, GetLastActivePopup, please see:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633507%28v=vs.85%29.aspx[^] (good in case the application executes some dialog boxes, etc., using P/Invoke (see below). Remember that that handle can by zero, which means that it is not yet created, then you should repeat the call to the property a bit later. Please see:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.mainwindowhandle.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646311%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633539%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633507%28v=vs.85%29.aspx[^].

3) You can simulate use input on a low level using raw Windows API SendInput via P/Invoke, please see:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx[^].

In you need to learn P/Invoke, please see:
http://en.wikipedia.org/wiki/P/invoke[^],
http://msdn.microsoft.com/en-us/library/Aa712982[^].

This CodeProject article can also be useful:
Essential P/Invoke[^].

Do you really want all this for the questionable benefit of controlling some stupid UI application? To tell you the truth, I would not bother. :-)

—SA
 
Share this answer
 
Comments
blueye89 16-Feb-12 8:04am    
I am running application ex.notepad:
[code]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim p As New Process
p.StartInfo.UseShellExecute = True
p.StartInfo.Arguments = "C:\other_application.txt"
p.StartInfo.FileName = "notepad.exe"
p.Start()
End Sub
End Class
[/code]
Sergey Alexandrovich Kryukov 16-Feb-12 21:46pm    
Could be simpler, just one Process childProcess = Process.Start(exeName, parameter);
IntPtr mainWindow = childProcess.MainWindowHandle; (may need repeat until not null).
Then activate it using P/Invoke and raw Windows API. Did you try P/Invoke? You can learn it. Then SendInput...
--SA
blueye89 16-Feb-12 8:06am    
How to get his FILE>SAVE function?I don't understand post above.Please some code example..
Sergey Alexandrovich Kryukov 16-Feb-12 21:43pm    
What example? I don't have your UI application which you use as a child process. You activate main form and simulate something like Alt+F, S, using SendInput. You cannot understand it unless you find out how to call each function, and I explained where to get them and what to do. The code is too big to put it here; and it will take too much time. I warned you, this is dirty programming. If you do not understand something specific, please ask, but if you just did not take a labor to read each referenced article to understand what the methods do, you hardly can get help.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900