0

I have a vb.net desktop application which is migrated from vb6.

There is a button click on which Shell or Prcess.Start method launch an exe.

Now user resize the exe window.

I want that on clicking of button again will launch or refresh the exe in already existing exe window. No new window should open and size of window remain as it was set by the user.

Used AppActivate method but it only set the focus to existing exe window not refreshing the exe on window.

Is there any way to to do this. Thanks in advance.

user3060430
  • 131
  • 1
  • 4
  • 16

1 Answers1

1

You need to P/Invoke to achieve this.

Here's a snippet. Just tested and seems to work fine.

Imports System.Diagnostics
Imports System.Runtime.InteropServices

Public Class ProcessStuff
    Private _processRunning as Process

    Public Sub StartOrActivate()
       If _processRunning Is Nothing
          _processRunning = ' Arguments to Start the process and retrieve the object
       Else
          ' Activate Process' Main Window
          SetForegroundWindow(_processRunning.MainWindowHandle)
       End If
    End Sub

    <DllImport("user32.dll")>
    Private Shared Function SetForegroundWindow(hWnd As IntPtr) As Boolean
    End Function

End Class

Edit:

As I can understand of your last comment, you want the process to be single instanced.

But: Note that the target application (process) must be prepared for that!

Community
  • 1
  • 1
J.Hudler
  • 1,238
  • 9
  • 26
  • I tested this with process notepad.exe. I opened a notepad and then written it over something. Then again open the notepad but it is not relaunching the notepad but displaying the previous one only. – user3060430 Dec 03 '13 at 13:43
  • Post some code then. How your application worked back in VB6? Also, this sentence is not clear for me: "I want that on clicking of button again will launch or refresh the exe in already existing exe window". – J.Hudler Dec 03 '13 at 16:29
  • I mean if I relaunch the same process again. Then it should not open the new window, but should launch in the existing window with fresh instance. – user3060430 Dec 03 '13 at 19:27