C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 300,331 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,814 people online right now. Registration is fast and FREE... Join Now!




Is it possible to open a completely foreign software inside your Windo

 

Is it possible to open a completely foreign software inside your Windo

papuccino1

9 Jun, 2009 - 05:32 PM
Post #1

C# Programmer
Group Icon

Joined: 2 Mar, 2008
Posts: 943



Thanked: 33 times
Dream Kudos: 50
My Contributions
I'm having some ideas for my next project and I gotta strike while the iron is hot. Is it possible to have a program open inside your WindowsForm? To be clear, I mean literally a completely different program. Imagine a create a WinForm with two buttons, I click button1 and software X appears inside that WinForms as a child.

Using MDI Form perhaps?

Just wanna know before I start pouring energy into it and then find out it can't be done. tongue.gif

This post has been edited by papuccino1: 9 Jun, 2009 - 05:33 PM

User is offlineProfile CardPM
+Quote Post


bodom658

RE: Is It Possible To Open A Completely Foreign Software Inside Your Windo

9 Jun, 2009 - 05:37 PM
Post #2

Low-Level Extremist, High-Level Opportunist.
Group Icon

Joined: 22 Feb, 2008
Posts: 770



Thanked: 39 times
Dream Kudos: 325
My Contributions
Perhaps if you can write a shell that can contain an external process and start from there?
User is offlineProfile CardPM
+Quote Post

papuccino1

RE: Is It Possible To Open A Completely Foreign Software Inside Your Windo

9 Jun, 2009 - 05:42 PM
Post #3

C# Programmer
Group Icon

Joined: 2 Mar, 2008
Posts: 943



Thanked: 33 times
Dream Kudos: 50
My Contributions
Woah! That went completely over my head. tongue.gif

Would you please elaborate a bit on that, bodom?
User is offlineProfile CardPM
+Quote Post

Core

RE: Is It Possible To Open A Completely Foreign Software Inside Your Windo

9 Jun, 2009 - 05:44 PM
Post #4

Den The Developer
Group Icon

Joined: 8 Dec, 2008
Posts: 2,944



Thanked: 214 times
Dream Kudos: 900
Expert In: C#, VB.NET, .NET Framework

My Contributions
Do you mean it's something like a Virtual Macine or an emulator?
User is online!Profile CardPM
+Quote Post

papuccino1

RE: Is It Possible To Open A Completely Foreign Software Inside Your Windo

9 Jun, 2009 - 05:48 PM
Post #5

C# Programmer
Group Icon

Joined: 2 Mar, 2008
Posts: 943



Thanked: 33 times
Dream Kudos: 50
My Contributions
I mean a different software. Something like this:

IPB Image
User is offlineProfile CardPM
+Quote Post

Core

RE: Is It Possible To Open A Completely Foreign Software Inside Your Windo

9 Jun, 2009 - 05:58 PM
Post #6

Den The Developer
Group Icon

Joined: 8 Dec, 2008
Posts: 2,944



Thanked: 214 times
Dream Kudos: 900
Expert In: C#, VB.NET, .NET Framework

My Contributions
That would require the knowledge of low-level and system programming as you'll have to imlement many of the OS calls and operations to run the software inside your program (basically, that is what such software as Wine and Cedega do on Linux).
User is online!Profile CardPM
+Quote Post

papuccino1

RE: Is It Possible To Open A Completely Foreign Software Inside Your Windo

9 Jun, 2009 - 05:59 PM
Post #7

C# Programmer
Group Icon

Joined: 2 Mar, 2008
Posts: 943



Thanked: 33 times
Dream Kudos: 50
My Contributions
Hm... that's a bit above what I had imagined.

Do you guys have any other ideas on how I could sort of simulate the idea without actually doing it?
User is offlineProfile CardPM
+Quote Post

fremgenc

RE: Is It Possible To Open A Completely Foreign Software Inside Your Windo

9 Jun, 2009 - 08:35 PM
Post #8

D.I.C Head
**

Joined: 15 Nov, 2007
Posts: 119



Thanked: 3 times
My Contributions
create a form with a transparant background and size it based on the target programs coordinates.


To get a foreign window, need to use winAPI library so you have to import it

you might have to do more research but these winApi functions should get you started

CODE


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

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
e

some c# code, you should get the idea
CODE

int hwnd=0;
IntPtr hwndChild=IntPtr.Zero;

//Get a handle for the Calculator Application main window

hwnd=FindWindow(null,"Calculator");
if(hwnd == 0)
{
    if(MessageBox.Show("Couldn't find the calculator" +
                       " application. Do you want to start it?",
                       "TestWinAPI",
                       MessageBoxButtons.YesNo)== DialogResult.Yes)
    {
        System.Diagnostics.Process.Start("Calc");
    }
}
else
{
//Call GetWindowPlacement() here, then place your window around it
}


This post has been edited by fremgenc: 9 Jun, 2009 - 08:36 PM
User is offlineProfile CardPM
+Quote Post

lesPaul456

RE: Is It Possible To Open A Completely Foreign Software Inside Your Windo

10 Jun, 2009 - 07:14 AM
Post #9

D.I.C Head
Group Icon

Joined: 16 Apr, 2009
Posts: 221



Thanked: 31 times
Dream Kudos: 175
My Contributions
QUOTE
I'm having some ideas for my next project and I gotta strike while the iron is hot. Is it possible to have a program open inside your WindowsForm? To be clear, I mean literally a completely different program. Imagine a create a WinForm with two buttons, I click button1 and software X appears inside that WinForms as a child.

Using MDI Form perhaps?

Just wanna know before I start pouring energy into it and then find out it can't be done. tongue.gif


There is actually a pretty easy way to do this.

First, you need to add a panel to your form. This panel will be used to "host" the application.

Next, you need to the "System.Runtime.InteropServices" and the "System.Diagnostics" namespace to your namespaces:
csharp

using System.Diagnostics;
using System.Runtime.InteropServices;


Now, we need setup our WinAPI functions:
csharp

[DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hwndChild, IntPtr hwndNewParent);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, Int32 wParam, Int32 lParam);


Now, inside the button click event, start the process, and set it's parent to the panel. In this example, I will be using notepad:
csharp

// Create a new process
Process proc;

// Start the process
proc = Process.Start("notepad.exe");
proc.WaitForInputIdle();

// Set the panel control as the application's parent
SetParent(proc.MainWindowHandle, this.panel1.Handle);

// Maximize application
SendMessage(proc.MainWindowHandle, 274, 61488, 0);


Hope this helps!

**EDIT** In "SendMessage()", 274 and 61488 are commands used to maximize the application. 274 represetns the "WM_SYSCOMMAND" message, which is the message that is sent to a window when the user chooses a command from the window menu. 61488 represents the "SC_MAXIMIZE" wParam, or the type of system command to use. So, by calling "SendMessage()" with these parameters, you are telling the application to maximize itself. **EDIT**

This post has been edited by lesPaul456: 10 Jun, 2009 - 09:49 AM
User is offlineProfile CardPM
+Quote Post

papuccino1

RE: Is It Possible To Open A Completely Foreign Software Inside Your Windo

10 Jun, 2009 - 07:22 AM
Post #10

C# Programmer
Group Icon

Joined: 2 Mar, 2008
Posts: 943



Thanked: 33 times
Dream Kudos: 50
My Contributions
I'll test out your code lesPaul, that look pretty legit! biggrin.gif

This is great news, I just hope it works how I'm imagining it right now. I'm going to try this out tomorrow. Right now I'm really busy.
User is offlineProfile CardPM
+Quote Post

lesPaul456

RE: Is It Possible To Open A Completely Foreign Software Inside Your Windo

10 Jun, 2009 - 07:29 AM
Post #11

D.I.C Head
Group Icon

Joined: 16 Apr, 2009
Posts: 221



Thanked: 31 times
Dream Kudos: 175
My Contributions
When you first push the button, it may take a couple seconds to load, so be patient smile.gif .
User is offlineProfile CardPM
+Quote Post

fremgenc

RE: Is It Possible To Open A Completely Foreign Software Inside Your Windo

10 Jun, 2009 - 07:51 AM
Post #12

D.I.C Head
**

Joined: 15 Nov, 2007
Posts: 119



Thanked: 3 times
My Contributions
lol thats a much better method than the one I suggested.
User is offlineProfile CardPM
+Quote Post

papuccino1

RE: Is It Possible To Open A Completely Foreign Software Inside Your Windo

10 Jun, 2009 - 08:16 AM
Post #13

C# Programmer
Group Icon

Joined: 2 Mar, 2008
Posts: 943



Thanked: 33 times
Dream Kudos: 50
My Contributions
lesPaul, how the hell do you know this?!
User is offlineProfile CardPM
+Quote Post

lesPaul456

RE: Is It Possible To Open A Completely Foreign Software Inside Your Windo

10 Jun, 2009 - 08:35 AM
Post #14

D.I.C Head
Group Icon

Joined: 16 Apr, 2009
Posts: 221



Thanked: 31 times
Dream Kudos: 175
My Contributions
QUOTE(papuccino1 @ 10 Jun, 2009 - 10:16 AM) *

lesPaul, how the hell do you know this?!


LOL! I've done a good amount of game programming, in both XNA and MDX. In order to host both of these in a windows forms application, you have do do something similar to this (although similar, the method to host XNA and MDX in a winform app is still very different).

This code has proven particularly useful for hosting games created using the DarkGDK (which uses C++) in C# or VB.Net applications.

This post has been edited by lesPaul456: 10 Jun, 2009 - 08:36 AM
User is offlineProfile CardPM
+Quote Post

papuccino1

RE: Is It Possible To Open A Completely Foreign Software Inside Your Windo

10 Jun, 2009 - 10:10 AM
Post #15

C# Programmer
Group Icon

Joined: 2 Mar, 2008
Posts: 943



Thanked: 33 times
Dream Kudos: 50
My Contributions
lesPaul you're fucking kidding me!? This is exactly what I was looking for!

You're my new favorite person. biggrin.gif

Now, I just need a way so the window stays put. Using the code above I can move the window around (but only in my form). Who the hell knew C# was this powerful and didn't tell me?! tongue.gif

Thanks Paul!
User is offlineProfile CardPM
+Quote Post

lesPaul456

RE: Is It Possible To Open A Completely Foreign Software Inside Your Windo

10 Jun, 2009 - 11:22 AM
Post #16

D.I.C Head
Group Icon

Joined: 16 Apr, 2009
Posts: 221



Thanked: 31 times
Dream Kudos: 175
My Contributions
Glad I could help!

I have yet to come up with a solution to prevent the application from moving. I'll look into it later today.

I forgot to mention something earlier; It seems this code doesn't work right with applications created using the .Net Framework, only Win32 applications. I only really use this code, as I mentioned, for games created using the DarkGDK, since they are Win32 apps.

I plan on looking into a solution to hosting .Net applications as well, but this may take some time.

Anyway, if I find a way to keep the window stationary, I'll be sure to post back.
User is offlineProfile CardPM
+Quote Post

lesPaul456

RE: Is It Possible To Open A Completely Foreign Software Inside Your Windo

10 Jun, 2009 - 03:28 PM
Post #17

D.I.C Head
Group Icon

Joined: 16 Apr, 2009
Posts: 221



Thanked: 31 times
Dream Kudos: 175
My Contributions
I figured out how to prevent the user from moving the window.

First, you need to add two more native methods:
csharp

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

[DllImport("user32.dll")]
public static extern bool RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags);


Next, you need to get the windows menu:
csharp

IntPtr hWnd = GetSystemMenu(process.MainWindowHandle, false);


And now we stop the user from moving the window:
csharp

RemoveMenu(hWnd, 0xF010, 0x00000000 | 0x00001000);


That's it! Hope this helps!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 04:22PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month