Loading application w/in MDI form

G

Guest

I've searched around for this and have found some VB examples but 1) I'm not
sure that they're VB.NET and 2) If they are VB.NET, then they're not working
once ported to C#. I'll be doing this in .NET 2.0 w/ VS.NET 2k5

Below is my code:

#region WinAPI Functions
[DllImport("user32.dll")]
public static extern IntPtr GetParent(IntPtr hwnd);

[DllImport("user32.dll")]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr
hWndNewParent);

[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string
lpWindowName);

[DllImport("user32.dll")]
public static extern IntPtr GetWindow(IntPtr hwnd, int wCmd);

[DllImport("user32.dll")]
public static extern int GetWindowText(IntPtr hwnd, string lpString, int
cch);

[DllImport("user32.dll")]
public static extern long GetWindowThreadProcessId(IntPtr hwnd, long
lpdwProcessId);

[DllImport("user32.dll")]
public static extern bool LockWindowUpdate(IntPtr hwndLock);

[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();

[DllImport("user32.dll")]
public static extern bool DestroyWindow(IntPtr hwnd);

[DllImport("user32.dll", EntryPoint = "SetFocus")]
public static extern IntPtr ApiSetFocus(IntPtr hwnd);

[DllImport("kernel32.dll")]
public static extern bool TerminateProcess(IntPtr hProcess, int
uExitCode);

[DllImport("kernel32.dll")]
public static extern IntPtr GetCurrentProcess();
#endregion

Those functions were listed in a VB solution as the ones to use. Then for a
btnLaunch in my MDI form:

private void btnLaunch_Click(object sender, EventArgs e)
{
if (ofdApp.ShowDialog(this) == DialogResult.OK)
{
string appCommand = ofdApp.FileName;

m_process = new System.Diagnostics.Process();
m_process.StartInfo = new
System.Diagnostics.ProcessStartInfo(appCommand);
Program.LockWindowUpdate(Program.GetDesktopWindow());
if (m_process.Start())
{
Program.SetParent(m_process.Handle, this.Handle);
}
Program.LockWindowUpdate(this.Handle);
}
}


If anybody's got an idea as to how to do this in C# 2.0 or has an example
please let me know. Thanks!
 
G

Guest

I've been hacking away at this today and have made a little bit of progress...

Here is what I have so far:

private void btnLaunch_Click(object sender, EventArgs e)
{
if (ofdApp.ShowDialog(this) == DialogResult.OK)
{
Process m_proc = new Process();
m_proc.StartInfo = new ProcessStartInfo(ofdApp.FileName);
m_proc.StartInfo.Domain = AppDomain.CurrentDomain.FriendlyName;
m_proc.StartInfo.ErrorDialogParentHandle = this.Handle;
m_proc.StartInfo.UseShellExecute = true;
m_proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal;

if (m_proc.Start())
{
m_proc.EnableRaisingEvents = true;
m_proc.Exited += new EventHandler(proc_Exited);
m_subProcesses.Add(m_proc);
}

btnSetParents.PerformClick();
}
}

private void btnSetParents_Click(object sender, EventArgs e)
{
Program.LockWindowUpdate(Program.GetDesktopWindow());
foreach (Process p in m_subProcesses)
{
Program.SetParent(p.MainWindowHandle, this.Handle);
}

Program.LockWindowUpdate(IntPtr.Zero);
}


Now here's the weird part. This works just fine for putting cmd.exe and
notepad.exe within my MDI form. However, when I try to run more "intesive"
programs like a custom app we have (100k) or internet explorer, this won't
function. But as soon as I put a thread.sleep as the first line in the
btnSetParents, I can then load my custom app but still not internet explorer.
If i set the sleep for even longer, eventually IE will get grabbed by this
call. If this call is made automatically, and I then click the SetParents
button on my form, nothing happens (I guess because the parent has already
been set to my MDI form, but just didn't reflect the change).

If there's a way to "know" when a System.Process is done loading and ready
for the parent to be changed, that's essentially what I'm looking for.

Thanks for any help!
--
He who dies with the most toys wins.


Brandon said:
I've searched around for this and have found some VB examples but 1) I'm not
sure that they're VB.NET and 2) If they are VB.NET, then they're not working
once ported to C#. I'll be doing this in .NET 2.0 w/ VS.NET 2k5

Below is my code:

#region WinAPI Functions
[DllImport("user32.dll")]
public static extern IntPtr GetParent(IntPtr hwnd);

[DllImport("user32.dll")]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr
hWndNewParent);

[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string
lpWindowName);

[DllImport("user32.dll")]
public static extern IntPtr GetWindow(IntPtr hwnd, int wCmd);

[DllImport("user32.dll")]
public static extern int GetWindowText(IntPtr hwnd, string lpString, int
cch);

[DllImport("user32.dll")]
public static extern long GetWindowThreadProcessId(IntPtr hwnd, long
lpdwProcessId);

[DllImport("user32.dll")]
public static extern bool LockWindowUpdate(IntPtr hwndLock);

[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();

[DllImport("user32.dll")]
public static extern bool DestroyWindow(IntPtr hwnd);

[DllImport("user32.dll", EntryPoint = "SetFocus")]
public static extern IntPtr ApiSetFocus(IntPtr hwnd);

[DllImport("kernel32.dll")]
public static extern bool TerminateProcess(IntPtr hProcess, int
uExitCode);

[DllImport("kernel32.dll")]
public static extern IntPtr GetCurrentProcess();
#endregion

Those functions were listed in a VB solution as the ones to use. Then for a
btnLaunch in my MDI form:

private void btnLaunch_Click(object sender, EventArgs e)
{
if (ofdApp.ShowDialog(this) == DialogResult.OK)
{
string appCommand = ofdApp.FileName;

m_process = new System.Diagnostics.Process();
m_process.StartInfo = new
System.Diagnostics.ProcessStartInfo(appCommand);
Program.LockWindowUpdate(Program.GetDesktopWindow());
if (m_process.Start())
{
Program.SetParent(m_process.Handle, this.Handle);
}
Program.LockWindowUpdate(this.Handle);
}
}


If anybody's got an idea as to how to do this in C# 2.0 or has an example
please let me know. Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top