Results 1 to 18 of 18

Thread: Open file with external exe file inside VB form

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Posts
    14

    Open file with external exe file inside VB form

    Hi,

    I'm a newbie here and I have a problem which I which to share. I wish to open a file inside a form using an external program.

    I have looked on the web for a solution but I am having only partial success. Here's the code I am using below:

    vb Code:
    1. Public Class Form1
    2.     Declare Auto Function SetParent Lib "user32.dll" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
    3.     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
    4.     Private Const WM_SYSCOMMAND As Integer = 274
    5.     Private Const SC_MAXIMIZE As Integer = 61488
    6.     Dim proc As Process
    7.     Dim AppPath As String
    8.     Dim strPath As String
    9.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    10.         AppPath = "C:\Users\User\Documents\Visual Studio 2010\Projects\Z26.exe"
    11.         strPath = ""C:\Users\User\Documents\Visual Studio 2010\Projects\3d_tictac.bin"
    12.         proc = Process.Start(AppPath)
    13.         proc.WaitForInputIdle()
    14.  
    15.         SetParent(proc.MainWindowHandle, Panel1.Handle)
    16.         SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
    17.     End Sub
    18. End Class

    If I run the code as above, the exe opens in my form which is what I want. However if I change the line, proc = Process.Start(AppPath) to, proc = Process.Start(AppPath,strPath) so that I open the BIN file, the file is not opened within the form but instead in a new window. If anyone could help me with this problem that would be awesome.
    Thanks
    Last edited by Siddharth Rout; Jul 22nd, 2012 at 06:01 PM. Reason: Added Code tags

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Open file with external exe file inside VB form

    What is the normal behaviour of Z26.exe? If you opened it from the command prompt, for example, would it open a new window for the file?

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Posts
    14

    Re: Open file with external exe file inside VB form

    Quote Originally Posted by dunfiddlin View Post
    What is the normal behaviour of Z26.exe? If you opened it from the command prompt, for example, would it open a new window for the file?
    If I run it from command prompt a message box comes up with the software version of the Z26.exe. A file is opened in a new window. only one file at a time can be opened.

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Open file with external exe file inside VB form

    Ok, so you'll have to parent the new window in addition to the application (or just live with it). You'll probably need the FindWindow API function.

  5. #5
    Junior Member
    Join Date
    Jul 2012
    Location
    Here
    Posts
    18

    Re: Open file with external exe file inside VB form

    I did this for Winzip, let me know if it helps.

    Dim PathWinZip As String, FileNameZip As String
    Dim ShellStr As String, FolderName As String

    PathWinZip = "C:\program files\winzip\"
    'This will check if this is the path where WinZip is installed.
    If Dir(PathWinZip & "winzip32.exe") = "" Then
    MsgBox "Please find your copy of winzip32.exe and try again"
    Exit Sub
    End If

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Posts
    14

    Re: Open file with external exe file inside VB form

    Quote Originally Posted by dunfiddlin View Post
    Ok, so you'll have to parent the new window in addition to the application (or just live with it). You'll probably need the FindWindow API function.
    Dunfiddlin, could you please give me an example of how you would write that extra bit of code? It's the first time that I'm implementing this code and an example would be handy. Thanks

  7. #7
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Open file with external exe file inside VB form

    VB Code:
    1. Declare Auto Function FindWindow Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    2.  
    3. 'other stuff
    4. 'set WindowText to the window title eg. "Untitled - Notepad"
    5.  
    6. SetParent(FindWindow(vbNullString, WindowText), Panel1.Handle)

    You may need to tweak some stuff. vbNullString usually works but try the application name (minus extension) if it doesn't. I don't think it applies in this case but if the second window is already a child, you'll need to use FindWindowEx instead (it has a few extra parameters so look it up on MSDN and/or Pinvoke).

  8. #8

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Posts
    14

    Re: Open file with external exe file inside VB form

    Quote Originally Posted by dunfiddlin View Post
    VB Code:
    1. Declare Auto Function FindWindow Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    2.  
    3. 'other stuff
    4. 'set WindowText to the window title eg. "Untitled - Notepad"
    5.  
    6. SetParent(FindWindow(vbNullString, WindowText), Panel1.Handle)

    You may need to tweak some stuff. vbNullString usually works but try the application name (minus extension) if it doesn't. I don't think it applies in this case but if the second window is already a child, you'll need to use FindWindowEx instead (it has a few extra parameters so look it up on MSDN and/or Pinvoke).
    I tried using Findwindow but that was not working. I instead used FindWindowByCaption and its now coming with this error message: "Unable to find an entry point named 'FindWindowByCaption' in DLL 'user32.dll'."

    Public Class Form1
    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
    Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr

    Private Const WM_SYSCOMMAND As Integer = 274
    Private Const SC_MAXIMIZE As Integer = 61488
    Dim proc As Process
    Dim AppPath As String
    Dim strPath As String
    Dim hwnd As IntPtr
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    AppPath = "C:\Users\User\Documents\Visual Studio 2010\Projects\Z26.exe"
    strPath = ""C:\Users\User\Documents\Visual Studio 2010\Projects\3d_tictac.bin"
    proc = Process.Start(AppPath, strPath)
    hwnd = FindWindowByCaption(IntPtr.Zero, strPath) 'value in strpath is the window title.
    If hwnd.Equals(IntPtr.Zero) Then
    MessageBox.Show("Got null handle") ' the message comes up when I run the program
    End If
    SetParent(hwnd, Panel1.Handle)
    SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
    End Sub
    End Class

  9. #9
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Open file with external exe file inside VB form

    I suspect your problem is that you are trying to add the emulator window to your panel before the emulator window is created. i.e. its handle is null.

    The following is a little proof of concept. Although this works for me, given the nature of what you are trying to do, there is no guarantee it will work on your system.

    If it does work, then note that the window handle returned from the API is the same as that returned from proc.MainWindowHandle. (also note, that for the parameters of the Process.Start method, I added a -v10 switch which you may or may not need).


    at the top of your code page:
    Code:
    Imports System.Runtime.InteropServices

    then:
    vb.net Code:
    1. Declare Auto Function SetParent Lib "user32.dll" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
    2. 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
    3. Private Const WM_SYSCOMMAND As Integer = 274
    4. Private Const SC_MAXIMIZE As Integer = 61488
    5.  
    6. Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    7.  
    8. <DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True, CharSet:=CharSet.Auto)> _
    9. Private Shared Function FindWindowByCaption( _
    10.  ByVal zero As IntPtr, _
    11.  ByVal lpWindowName As String) As IntPtr
    12. End Function
    13.  
    14.  
    15.  
    16. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    17.     Dim proc As Process
    18.     Dim AppPath As String
    19.     Dim strPath As String
    20.     Dim quotes = ControlChars.Quote
    21.  
    22.     '   RUN EMULATED GAME
    23.     AppPath = "C:\Users\User\Documents\Visual Studio 2010\Projects\Z26.exe"
    24.     strPath = "C:\Users\User\Documents\Visual Studio 2010\Projects\3d_tictac.bin"
    25.     proc = Process.Start(AppPath, " -v10 " & quotes & strPath & quotes)
    26.  
    27.     '    NAUGHTY NOO-NOO (blocks UI thread)
    28.     Me.Cursor = Cursors.WaitCursor
    29.     Threading.Thread.Sleep(2000)
    30.     Me.Cursor = Cursors.Default
    31.  
    32.     '    API
    33.     Dim hwnd = FindWindowByCaption(IntPtr.Zero, strPath) 'value in strpath is the window title.
    34.  
    35.     '    DISPLAY RESULTS
    36.     If hwnd.Equals(IntPtr.Zero) Then
    37.         MessageBox.Show("Got null handle") ' the message 'shouldn't' come up now when you run the program
    38.     Else
    39.         MessageBox.Show("FindWindowByCaption   " & hwnd.ToString("X8") & Environment.NewLine & _
    40.                         "proc.MainWindowHandle " & proc.MainWindowHandle.ToString("X8"))
    41.     End If
    42.  
    43.     SetParent(proc.MainWindowHandle, Me.Handle)
    44.     SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
    45.  
    46. End Sub

  10. #10

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Posts
    14

    Re: Open file with external exe file inside VB form

    Inferrd,I tried your code. I no longer get the "Got null handle" msg. However I still have the emulator window opening outside the form.

  11. #11
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Open file with external exe file inside VB form

    The point being.... adding a pause gives the emulator a chance to load the ROM image, initialise itself and create its window. Then you can access and use its window handle.

    Did you try adding a pause to your original code? I can certainly get the emulator window to sit inside a panel on a form using your original code and pausing before setting the window's parent. However, there were unpredictable (at least to me) results on trying to move the form afterwards.

    The following works for me, it might for you. Then the fun begins...

    (I commented out your resize code as it messed things up for me)

    Code:
    Imports System.Runtime.InteropServices

    Set your panel size to around 360x360 for testing.
    And note the use of switches again in the call to Process.Start

    vb.net Code:
    1. Declare Auto Function SetParent Lib "user32.dll" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
    2. 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
    3. Private Const WM_SYSCOMMAND As Integer = 274
    4. Private Const SC_MAXIMIZE As Integer = 61488
    5.  
    6.  
    7. <DllImport("user32.dll")> _
    8. Private Shared Function GetWindowRect(ByVal hWnd As IntPtr, ByRef lpRect As RECT) As Boolean
    9. End Function
    10.  
    11. <DllImport("user32.dll")> _
    12. Public Shared Function MoveWindow(ByVal hWnd As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal bRepaint As Boolean) As Boolean
    13. End Function
    14.  
    15. Structure RECT
    16.     Dim Left As Integer '       // x position of upper-left corner
    17.     Dim Top As Integer '        // y position of upper-left corner
    18.     Dim Right As Integer '      // x position of lower-right corner
    19.     Dim Bottom As Integer '     // y position of lower-right corner
    20. End Structure
    21.  
    22.  
    23. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    24.     Dim proc As Process
    25.     Dim AppPath As String
    26.     Dim strPath As String
    27.  
    28.     AppPath = "C:\Users\User\Documents\Visual Studio 2010\Projects\Z26.exe"
    29.     strPath = "C:\Users\User\Documents\Visual Studio 2010\Projects\3d_tictac.bin"
    30.     proc = Process.Start(AppPath, " -v10 -M0 " & Chr(34) & strPath & Chr(34))
    31.     proc.WaitForInputIdle()
    32.  
    33.     Me.Cursor = Cursors.WaitCursor
    34.     Threading.Thread.Sleep(2000)
    35.     Me.Cursor = Cursors.Default
    36.  
    37.     SetParent(proc.MainWindowHandle, Panel1.Handle)
    38.  
    39.     Dim wRect As RECT = New RECT
    40.     GetWindowRect(proc.MainWindowHandle, wRect)
    41.  
    42.     MoveWindow(proc.MainWindowHandle, 0, 0, wRect.Right - wRect.Left, wRect.Bottom - wRect.Top, True)
    43.  
    44.     ' SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
    45. End Sub

  12. #12

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Posts
    14

    Re: Open file with external exe file inside VB form

    Inferrd I have tried your code and I no longer get the "got null handle" message. However The emulator is opening as a window and not within the form

  13. #13
    Hyperactive Member
    Join Date
    Apr 2011
    Location
    England
    Posts
    421

    Re: Open file with external exe file inside VB form

    Hi, maybe this post could help with what you are trying to do...

    http://www.vbforums.com/showthread.php?t=492260

  14. #14

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Posts
    14

    Re: Open file with external exe file inside VB form

    Still no luck. Dunfiddlin suggested to use to FindWindowEx and I think the window opened by the emulator is a child window. However I am unsure how to use this function and an example using e.g notepad would help me understand what parameter needs to be used with the function.

  15. #15
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Open file with external exe file inside VB form

    Could you please let me know what version of Z26 you're using and the file you're running with it or better yet bundle 'em up in a zip and attach them here (don't think there are any distribution issues). I'll have a hands-on go at this, see if we can sort it out.

  16. #16

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Posts
    14

    Re: Open file with external exe file inside VB form

    Quote Originally Posted by dunfiddlin View Post
    Could you please let me know what version of Z26 you're using and the file you're running with it or better yet bundle 'em up in a zip and attach them here (don't think there are any distribution issues). I'll have a hands-on go at this, see if we can sort it out.
    Here are the emulator program... Hope you have better luck than I.
    Attached Files Attached Files

  17. #17
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Open file with external exe file inside VB form

    So there's good(ish) news and not so good news. I can get a grip on the window thusly ....

    VB Code:
    1. Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    2.         Process.Start("C:\Emulator\Games\Z26.exe", "C:\Emulator\Games\3d_tic.bin")
    3.         SetParent(FindWindow("SDL_App", "3d_tic.bin"), Panel1.Handle)
    4.     End Sub

    ... but ...

    When the parenting is sealed that (for some completely unknown reason) triggers full screen mode in the game thereby defeating the whole point of the exercise. Pretty certain there's no way round that. Sorry.

  18. #18

    Thread Starter
    New Member
    Join Date
    Jul 2012
    Posts
    14

    Re: Open file with external exe file inside VB form

    thanks for your help anyway.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width