From: garyusenet on
For this first time today I used the System.Diagnositcs namespace to
launch a program from my c# code. The program launches OK but I have
something which has completely stumped me.

The SetWindowPos method does not work. If I run the code as it is
presented below, app.exe launches in its own window and is displayed at
the top left part of the screen. However it isn't repositioned which is
what the last piece of code should do. HOWEVER if i end my c# program
and leave app.exe open - and then comment out the launchapp() part of
Form1_load and just run my c# program again the window is moved as
expected. So for some reason the SetWindowPos method is only working if
app.exe is open BEFORE I run my c# code, and not when I use my C# code
to open app.exe. Any ideas why this is please?

NB
The original (originalapp.exe) ran in fullscreen. Somebody wrote a
programme app.exe which runs originalapp.exe in a window. This is the
reason why I have to use FindWindow to find the Hwnd of originalapp.exe
from within app.exe and can't just query the process p for it's Hwnd.

Thanks very much for comments,

Gary-



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace WindowsApplication1
{

public partial class Form1 : Form
{

#region static bool SetWindowPos (hWnd, hWndInsertAfter, X, Y,
cx, cy, uFlags) *via imported dll*
[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr
hWndInsertAfter,
int X, int Y, int cx, int cy, uint uFlags);
private const int HWND_TOP = 0;
#endregion

#region static int FindWindow(_ClassName, _WindowName) *via
imported dll*
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError =
true)]
private static extern int FindWindow(string _ClassName,
string _WindowName);
#endregion

#region static void GetWindowText(h, s, nMaxCount) *via
imported dll*
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError =
true)]
public static extern void GetWindowText(int h,
StringBuilder s, int nMaxCount);
#endregion


public Form1()
{
InitializeComponent();
}


private void launchapp()
{
Process p = new Process();
p.StartInfo.FileName = @"C:\Program
Files\app\directory\app.exe";
p.StartInfo.WorkingDirectory = @"C:\Program
Files\app\directory\";
p.StartInfo.WindowStyle =
System.Diagnostics.ProcessWindowStyle.Hidden;
p.Start();
return;
}


private void Form1_Load(object sender, EventArgs e)
{

launchapp();

// window handle, place window at top of Z order, WP left,
WP top, width, height
int handle = FindWindow(null, "Title of Application
Window");
SetWindowPos((IntPtr)handle, (IntPtr)HWND_TOP, 5, 5, 5,
5,0);


}


}
}

From: Dustin Campbell on
Have you tried debugging this in conjunction with Spy++ to see if you are
actually getting the correct window handle? Also, have you checked the return
values of your Win32 API calls to see if they are returning success?

Best Regards,
Dustin Campbell
Developer Express Inc.


From: Dustin Campbell on
> The original (originalapp.exe) ran in fullscreen. Somebody wrote a
> programme app.exe which runs originalapp.exe in a window. This is the
> reason why I have to use FindWindow to find the Hwnd of
> originalapp.exe
> from within app.exe and can't just query the process p for it's Hwnd.
> Thanks very much for comments,

Why not improve your search by using FindWindowEx so that you can narrow
your search to the children of Process.MainWindowHandle?

Best Regards,
Dustin Campbell
Developer Express Inc.


From: garyusenet on
Dustin thankyou, I have queried the return value of my SetWindowPos and
it is returning false when I run it first time, and True when I cancel
my program, comment out the launchapp, and rerun the program. So it's
the SetWindowPos that's failing first time around.

I'll look into spy++ that you mentioned now, and see if it sheds any
light on why this is.

Many Thanks,

Gary-

Dustin Campbell wrote:

> > The original (originalapp.exe) ran in fullscreen. Somebody wrote a
> > programme app.exe which runs originalapp.exe in a window. This is the
> > reason why I have to use FindWindow to find the Hwnd of
> > originalapp.exe
> > from within app.exe and can't just query the process p for it's Hwnd.
> > Thanks very much for comments,
>
> Why not improve your search by using FindWindowEx so that you can narrow
> your search to the children of Process.MainWindowHandle?
>
> Best Regards,
> Dustin Campbell
> Developer Express Inc.

From: garyusenet on
Unfonruntely i don't seem to have spy++ i'm using Visual C# express and
don't think it comes with that tool. I have VS 2005 at home in the
attic i think but i'll have to find it.

In the meantime, i'll try to use that other function you mentioned.

Thanks,

Gary-

garyusenet(a)myway.com wrote:

> Dustin thankyou, I have queried the return value of my SetWindowPos and
> it is returning false when I run it first time, and True when I cancel
> my program, comment out the launchapp, and rerun the program. So it's
> the SetWindowPos that's failing first time around.
>
> I'll look into spy++ that you mentioned now, and see if it sheds any
> light on why this is.
>
> Many Thanks,
>
> Gary-
>
> Dustin Campbell wrote:
>
> > > The original (originalapp.exe) ran in fullscreen. Somebody wrote a
> > > programme app.exe which runs originalapp.exe in a window. This is the
> > > reason why I have to use FindWindow to find the Hwnd of
> > > originalapp.exe
> > > from within app.exe and can't just query the process p for it's Hwnd.
> > > Thanks very much for comments,
> >
> > Why not improve your search by using FindWindowEx so that you can narrow
> > your search to the children of Process.MainWindowHandle?
> >
> > Best Regards,
> > Dustin Campbell
> > Developer Express Inc.