views:

660

answers:

10

How do I write a program in C++ or C# that launches applications on Windows Vista?

For example launching Dreamweaver CS 4 ("C:\Program Files\Adobe\Adobe Dreamweaver CS4\Dreamweaver.exe) and place it on top with the BringWindowToTop-function?

+8  A: 

In c#

Process.Start("c:\whatever\somefile.exe", <commandline args>);

should do it

Jonas B
Ok, but how to get it on top?
Chris_45
@Chris_45 "Ok, but how to get it on top?" The girl process is usually on top.
No longer a user
we both know I should have gotten it :P
Itay
+2  A: 

In C/C++ there are plenty ways to do that.

system("c:\whatever\somefile.exe");

should fire up the program whatever you want.

progician
On Windows, this is kind of indirect, and it might not match the behavior of a regular process launch in little details like environment, bringing the window to the top, etc. The system call on windows will first find cmd.exe and send the command to that. Using ShellExecute will work more like Explorer.
Adrian McCarthy
A: 

Use CreateProcess function. Or see here an example.

Cătălin Pitiș
+4  A: 

using System; using System.Diagnostics;

namespace Launcher
{
  public static class Program
  {
    public static void Main(string[] args)
    {
      ProcessStartInfo startInfo = new ProcessStartInfo();

      startInfo.FileName = args[0];
      startInfo.WindowStyle = ProcessWindowStyle.Normal;

      Process.Start(startInfo);
    }
  }
}
Brannon
A: 

Install linux and vim...sorry ;)

felix
+3  A: 

The main problem that you'll have is finding out which new window belongs to the just-created process. A simple solution would be to EnumWindows() before and after, and then raise the newly created top-level window. Since your launcher presumably has the focus, it can give that focus to the new window.

MSalters
+2  A: 

Not C++ or C# but, using AutoIt, you can launch the program and either automate the click you make or bring to top the DW4 window, for example (not tested):

AutoItSetOption("WinTitleMatchMode", 2)
Run("C:\Program Files\Adobe\Adobe Dreamweaver CS4\Dreamweaver.exe", "", @SW_MAXIMIZE)
WinSetOnTop("Dreamweaver", "", 1)

and once the script suits your need there's a tool to convert it to an exe (Aut2Exe). Hope that helps :)

RC
Ok, getting somewhere, but this script really takes a hold of the window, if I press the shortcutkey for Visual Studio DW is still in front, I would like the apps to place itself infront when I press the keys?
Chris_45
I don't understand well did you use autoit to have a shortcut key for VS or did you have autoit to launch DW and something else to launch VS?
RC
Yes autit for DW and something else for VS, the problem is that I have to miminize DW then to be able to see something else, and when I maximize it stays "foot" there and sort of "occupy" the screen?
Chris_45
Try replacing `WinSetOnTop("Dreamweaver", "", 1)` by `WinActivate("Dreamweaver")`; But I think what you really need is some hotkey management software like autohotkey[1] or something similar to have in one place all you hotkey+action, action being for DW launch and focus.[1]http://www.autohotkey.com
RC
+3  A: 

I think following should be the simple most way to do that.

ShellExecute(NULL,_T(""), _T("C:\\windows\\notepad.exe"), _T(""), NULL, SW_SHOWMAXIMIZED);

In place of using notepad.exe, you may use runas command.
Refer this link for more details on runas command.

vrrathod
+4  A: 

To launch the program use: Process.Start.

To find the path of the exe file, you may have a look at the registry.

I would recommend to manually search in the registry the actual path of the exe in you computer, and this way try to see where the path is saved.

To bring to window to the front, you can use SetForegroundWindow(int hWnd) as well as FindWindow(string lpClassName, string lpWindowName), as mentioned here:

http://www.dotnetspider.com/resources/5772-Bring-e-window-Front-set-It-Active-window.aspx

Hopes it helps.

Itay
+1  A: 

See this Stack Overflow question.

Geoff