Solved

Launch a process in a designated monitor

Posted on 2008-10-22
14
1,597 Views
Last Modified: 2012-05-05
I want to be able to launch a process in the Secondary monitor. I am using the code below, but it doesn't seem to work. In fact the result is the same, no matter what value do I use for the hIcon field.

Since notepad does remember its last position, I have also tested with a test application that does not set a default monitor and uses the default position when showing up.
const

  SEE_MASK_HMONITOR = $00200000;  // Not defined in ShellAPI unit
 

procedure TForm1.Button2Click(Sender: TObject);

var

  sei : TShellExecuteInfo;

  SecondaryMonitor : Cardinal;

begin

  // Get the Handle to the secondary monitor

  if Screen.MonitorCount = 1 then

    SecondaryMonitor := 0

  else if Screen.PrimaryMonitor = Screen.Monitors[0] then

    SecondaryMonitor := Screen.Monitors[1].Handle

  else

    SecondaryMonitor := Screen.Monitors[0].Handle;

  // Fill in ShellExecuteEx info

  ZeroMemory(@sei,SizeOf(sei));

  sei.cbSize := SizeOf(sei);

  sei.fMask := SEE_MASK_HMONITOR;

  sei.hIcon := SecondaryMonitor;

  sei.lpVerb := 'open';

  sei.lpFile := 'notepad.exe';

  sei.nShow := SW_SHOWMAXIMIZED;

  ShellExecuteEx(@sei);

end;

Open in new window

0
Comment
Question by:momsoft2
  • 6
  • 3
  • 2
  • +2
14 Comments
 
LVL 28

Expert Comment

by:ciuly
Comment Utility
see this discussion for how to do it properly for your own application: http://stackoverflow.com/questions/206400/start-program-on-a-second-monitor

now, I never played with a multimnonitor system, so you will need to do this yourself and report back if it succeeded or not, dn if not, what errors you get (btw,. did you check what error shellexecuteex returnes?it would help if you would give us the code).

look at this function: http://msdn.microsoft.com/en-us/library/ms534603(VS.85).aspx
you will use this function to get the correct handle of the monitor using the Screen.Monitors[ x ].Left and top as desccribed in the first link. I figure since that works for your own application, getting the handle via monitorfrompoint should return a valid monitor handle which should work fine with shellexecuteex.

but, do not forget to tell us what error code shellexecuteex returns. we kind of need that to debug the issue ;)
0
 

Author Comment

by:momsoft2
Comment Utility
ShellExecuteEx does not return any error whatsoever. It simply ignores the supplied monitor.

I am already getting the monitor handle using the Screen.Monitors[x].Handle. Getting the handle using the procedure you describe seems like going in circles.

Anyway, I have used GetMonitorInfo to check that the handle provided by TScreen is valid (see below). It gives the correct size of my secondary monitor.

Therefore, I suspect that the problem lies in ShellExecuteEx.
procedure TForm1.Button3Click(Sender: TObject);

var

  SecondaryMonitor : Cardinal;

  mi : TMonitorInfo;

begin

  // Determine the monitor handle using Delphi Screen

  if Screen.MonitorCount = 1 then

    SecondaryMonitor := 0

  else if Screen.PrimaryMonitor = Screen.Monitors[0] then

    SecondaryMonitor := Screen.Monitors[1].Handle

  else

    SecondaryMonitor := Screen.Monitors[0].Handle;

  // Check that the handle is valid

  mi.cbSize := SizeOf(mi);

  GetMonitorInfo(SecondaryMonitor,@mi);

  ShowMessage(Format('The monitor rect is: %d,%d %d,%d',

    [mi.rcMonitor.Left,mi.rcMonitor.Top,mi.rcMonitor.Right,mi.rcMonitor.Bottom]));

end;

Open in new window

0
 
LVL 14

Expert Comment

by:SteveBay
Comment Utility
Is the program you are trying to launch one of yours? Did you write it or do you access to the source code?  The reason I ask is that the application your are trying to launch may have specific settings requiring it to launch in the same screen location everytime (i.e. ScreenCenter).
0
 

Author Comment

by:momsoft2
Comment Utility
I have tried many different programs, including a test one that does not set any special positioning, i.e. Position := poDefaultPosOnly.
0
 
LVL 28

Accepted Solution

by:
ciuly earned 200 total points
Comment Utility
steve might be rioght. try a different aproach.

use enumwindows and GetWindowThreadProcessId for each handle and match it to the processid returned by createprocess function (shellexecuteex only returnes a handle which complicates things a bit).

would this be ok for you? I can make a demo, but tomorrow. it's passed midnight here so I'm closing :)
0
 
LVL 28

Expert Comment

by:ciuly
Comment Utility
forgot to mention, after yuo found the handle of the window, use the movewindow function to place it in correct monitor as in my very first link.
0
 

Author Comment

by:momsoft2
Comment Utility
I was precisely thinking along the same line of placing myself the window in the desired monitor.

I prefer to use ShellExecute because I have had many problems in the past with CreateProcess. Getting the process ID is easy, enumerating processes. But I have a question? How do I ensure that ShellExecute does not return until the main window has been created? I have noticed that the function returns immediately.

Another issue is getting the main window once I have the process ID. What do you suggest?
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 6

Expert Comment

by:ChristianWimmer
Comment Utility
How does the main code of your test application look like? Delphi applications have a hidden main window that maybe is positioned on the other monitor instead of the real window. In newer Delphi this problem can be solved by adding "Application.MainFormOnTaskbar := True;" Maybe you/we should test this with a window application created by C++/Delphi without any framework (VCL, MFC, ATL ...).

You can wait for an process until it can process input events by calling WaitForInputIdle (http://msdn.microsoft.com/en-us/library/ms687022(VS.85).aspx)
0
 
LVL 6

Expert Comment

by:ChristianWimmer
Comment Utility
"Maybe you/we should test this with a window application created by C++/Delphi without any framework (VCL, MFC, ATL ...). "
Sorry, I mean the whole problem! I'll check it in C++ when I have time.
0
 
LVL 36

Expert Comment

by:Geert Gruwez
Comment Utility
when you want your app to start on a second monitor,
i suppose you want all your forms to open on that same monitor
or where the app is residing at that moment

when you open a secondary form you need to set it to poOwnerFormCenter
and create your forms with passing the form calling it

procedure TForm1.Button1Click(Sender: TObject);
begin
  Form2 := TForm2.Create(Self);
  try
    // do stuff with Form2
  finally
    FreeAndNil(Form2);
  end;
end;

also when autocreating the forms in the dpr
change the owner of the second, third, etc forms from
Application.CreateForm(TForm1, Form1);
Application.CreateForm(TForm2, Form2);

to
Application.CreateForm(TForm1, Form1);
Form2 := TForm2.Create(Form1);

For the initial form i need to look into code at work
I think i solved this issue once

0
 

Author Comment

by:momsoft2
Comment Utility
I see from the comments received that I have not expressed myself correctly. My needs are to find a general way to launch ANY application and place it in a secondary monitor.

When I found the new functionality in SellExecuteEx, I thought that this was the correct approach, but I have discovered that it is not.

Therefore, the way to go seems to be:

1) Use ShellExecuteEx to launch the application.
2) Obtain the process ID by enumerating all running processes.
3) Use WaitForInputIdle to wait for the application window to be created.
4) Determine the main window of the application using EnumWindows.
5) Manually move the main window to the secondary monitor using MoveWindow.

I will create a test application and post it here for your comments. But so far, your comments are really helping me to solve this problem. Thank you all very much!
0
 
LVL 36

Expert Comment

by:Geert Gruwez
Comment Utility
with CreateProcess you have direct access to the process id

var
  ProcessInfo: TProcessInformation;
  StartupInfo: TStartupInfo;
  BatchFile: string; // example: "notepad.exe"

FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
with StartupInfo do
begin
  cb:= SizeOf(TStartupInfo);
  dwFlags:= STARTF_USESHOWWINDOW;
  wShowWindow:= SW_SHOWNORMAL;
end;

{ Execute batch file as separated process }
CreateProcess(PChar(BatchFile), nil, nil, nil, False, NORMAL_PRIORITY_CLASS,
    nil, nil, StartupInfo, ProcessInfo);

see ProcessInfo.hProcess for the processid
0
 

Author Closing Comment

by:momsoft2
Comment Utility
Your suggestion helped me a lot. I have not yet been able to make a test that works, but I am in the right track. If I get stuck, I'll post a new question. Thank you very much.
0
 

Author Comment

by:momsoft2
Comment Utility
I am working on a complete example, using the suggestions given in your comments. I have not finished it yet but it seems to be the way to go. Thank you all very much.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now