Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Control to run window application in on a form 3

Status
Not open for further replies.

huggyboy

Programmer
Feb 7, 2003
108
0
0
GB
I am looking for a suitable control to place on my form to run a windows application (.exe) in, working in a similar way to the webbrowser object 'runs' IE inside it.

I have a feeling I may be missing something obvious here.

Anybody got any starting points? Is some kind of custom control the way to do it?

Any help would be appreciated.
 
If you could be a little more clear on what you want exactly? Maybe this will kind of answer your question. We use a program called a termanl emulator. If you don't know that that is think of it as telnet but a little more functionality. It has 2 .dll files one of which represents the screen as an ActiveX control and because of this it can be added to a form. If you mean embed another vb.net program then that is something else and I'm not 100% sure about.

If you mean actually embed a 3rd party exe into a form then I would suggest doing a google search "vb.net embed exe". That isn't a simple thing to do and the few examples I had time to check actually didn't work so have fun with that.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Thanks Sorwen for your reply - i will describe in a bit more detail.

I have been asked to create a form with buttons down the left hand side with an area on the right - depending which button is clicked different things appear on the right eg internet, display pdf document (can do those with a webbrowser control) or run an .exe program in the space - this has been written by someone else in the company (dont know what it is written in ).

So i suppose 'embed' is the correct terminology - i will have a look if a search comes up with anything
 
On the 8th page of Google results i managed to find the answer - start the program as a process then set a panel control on the form to its parent - the example below runs windows calculator and doesnt need the sleep however when i created a .net windows app and called it instead the process handle was coming back as zero until i put the sleep in
Code:
    Private Const WM_SYSCOMMAND As Integer = 274
    Private Const SC_MAXIMIZE As Integer = 61488
    Declare Auto Function SetParent Lib "user32.dll" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
    Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) ......

        Dim proc As Process
        proc = Process.Start("calc.exe") 
        proc.WaitForInputIdle()
        Sleep(500)        
        SetParent(proc.MainWindowHandle, Me.Panel1.Handle)
        SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
 
That will work for windows programs, but some said it wouldn't work for 3rd party programs. If you find out differently or a solution let us know. I don't have a use for something like that, but I'm curious if it actually can be done. I've always just used the process.start and let the program run on its own. I'm sure there are uses for the other way I just don't really see them though.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
A simple windows form application written in vb.net seemed to work ok - havent got the actual program yet - it is a program that monitors & controls machinery using a dedicated panel but runs under windows.
As long as the program get get a windows handle from the application then it should work ... i think!

I am in the process of writing the actual program then test it on the actual kit.

Thanks again for your input.
 
Just a follow-up to the previous post ... I eventually got it to work after solving a few small wrinkles ...

the application i was running was expecting to be run in its own folder (to check some kind of registration / licence file) so i had to create the process then
Process.StartInfo.FileName = "executable"
Process.StartInfo.WorkingDirectory = "working directory"
before doing the Process.Start() then ...

the process called another process itself then closes so that the process we have the link to isnt the one we eventually want so it had to find the process by
Code:
Do Until boolFound
   Sleep(200)
   Dim procList() As Process = Process.GetProcesses()
   Dim i As Integer
   For i = 0 To procList.Length - 1
        strProcName = procList(i).ProcessName
        strfilename = procList(i).MainWindowTitle
        If strfilename="window name" Then
            boolFound = True
            workProcess = procList(i)         
            SetParent(workprocess.MainWindowHandle,Me.Panel1.Handle)
        End If
    Next
Loop
(probably can be made more efficient (possibility of infinite loop) - found this bit on the net somewhere cant remember where - apology to original author)
ie loop around all the processes run looking for the window title then make the panel on my form its parent also ...

there is a quirk with windows calculator that if you change the view from standard to scientific it then can be moved out of the panel ie seems like a new process has been started and/or handle changed - havent solved that one.

Thought this might be of interest to somebody

 
I do it just slightly different when looking for a process.
Code:
    Private Sub CheckInstances()
        Dim myProcesses() As Process
        Dim instance As Process

        myProcesses = Process.GetProcessesByName("notepad")

        If myProcesses.Length > 0 Then
            For Each instance In myProcesses
                If instance.MainWindowTitle = "testnotepad.txt - Notepad" Then
                    MsgBox(instance.MainWindowTitle)
                End If
            Next
        End If
    End Sub

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Good point - i discovered that way of doing it just after i posted.

Thanks for your input - i will tweak the program when i have a moment.
 
Brilliant !!!
thanks for asking and solving the question. I just had to try it and it works great - now just to wait untill i need it :).
 
Also take a look at the shell command. I've used it in the past. Sometimes seems easier than starting a new process, etc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top