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:
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
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
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"
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
Re: Open file with external exe file inside VB form
Originally Posted by dunfiddlin
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.
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
Re: Open file with external exe file inside VB form
Originally Posted by dunfiddlin
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
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).
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
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:
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 Const WM_SYSCOMMAND As Integer = 274
Private Const SC_MAXIMIZE As Integer = 61488
Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
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:
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 Const WM_SYSCOMMAND As Integer = 274
Private Const SC_MAXIMIZE As Integer = 61488
<DllImport("user32.dll")> _
Private Shared Function GetWindowRect(ByVal hWnd As IntPtr, ByRef lpRect As RECT) As Boolean
End Function
<DllImport("user32.dll")> _
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
End Function
Structure RECT
Dim Left As Integer ' // x position of upper-left corner
Dim Top As Integer ' // y position of upper-left corner
Dim Right As Integer ' // x position of lower-right corner
Dim Bottom As Integer ' // y position of lower-right corner
End Structure
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim proc As Process
Dim AppPath As String
Dim strPath As String
AppPath = "C:\Users\User\Documents\Visual Studio 2010\Projects\Z26.exe"
strPath = "C:\Users\User\Documents\Visual Studio 2010\Projects\3d_tictac.bin"
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.
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.
Re: Open file with external exe file inside VB form
Originally Posted by dunfiddlin
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.
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.