So I'm looking into putting this file which is a .jar file into this vb file . I've tried looking for other ways to do this like on different programs but it didn't work out as well as they all do the same thing . I can't seem to figure it out.
I think i MAY have gone a little overboard here, but it piqued my interest so I coded out a method for doing it for any windows process (i hesitate with the "any" windows process ) I dunno if there is an easier way for a JAR applet, I didn't really check.
Please note, some methods are marked as not being written by me. I can't remember who wrote them but I take no credit for it. They are just 2 helper methods for enumerating the children of a given handle.
Here you go: (Note, it may look daunting, but it is really easy to use )
[highlight=vb.net]
Imports System****ntime.InteropServices
[/highlight]
[highlight=vb.net]
Private Declare Function SetParent Lib "user32.dll" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
Private Declare Sub GetClassName Lib "user32.dll" Alias "GetClassNameA" (ByVal hWnd As System.IntPtr, ByVal lpClassName As System.Text.StringBuilder, ByVal nMaxCount As Integer)
Private Declare Function EnumChildWindows Lib "user32.dll" (ByVal WindowHandle As IntPtr, ByVal Callback As EnumWindowCallback, ByVal lParam As IntPtr) As Boolean
Private Delegate Function EnumWindowCallback(ByVal hwnd As IntPtr, ByVal params As IntPtr) As Boolean
'METHOD NOT WRITTEN BY JASON, I DO NOT TAKE CREDIT
Public Shared Function getChildWindows(ByVal ParentHandle As IntPtr) As IntPtr()
Dim ChildrenList As New List(Of IntPtr)
Dim ListHandle As GCHandle = GCHandle.Alloc(ChildrenList)
Try
EnumChildWindows(ParentHandle, AddressOf EnumWindow, GCHandle.ToIntPtr(ListHandle))
Finally
If ListHandle.IsAllocated Then ListHandle.Free()
End Try
Return ChildrenList.ToArray
End Function
'METHOD NOT WRITTEN BY JASON, I DO NOT TAKE CREDIT.
Private Shared Function EnumWindow(ByVal Handle As IntPtr, ByVal Parameter As IntPtr) As Boolean
Dim ChildrenList As List(Of IntPtr) = GCHandle.FromIntPtr(Parameter).Target
ChildrenList.Add(Handle)
Return True
End Function
Private Function openProcessToControl(ByVal processLocation As String, ByVal parent As Control)
Dim sInfo As New ProcessStartInfo(processLocation) 'set the location of the process
Dim newApplet As New Process With {.StartInfo = sInfo} 'create the process
newApplet.Start() 'start the process
Dim windowHwnd As IntPtr = WaitForWindow(newApplet, 3000)
If windowHwnd = IntPtr.Zero Then Return False 'no handle, sorreh.
Dim windowName As String = getHwndName(windowHwnd) 'assigns the read class name to a variable
Dim destHwnd As IntPtr = parent.Handle 'get the handle of the input control.
If destHwnd = IntPtr.Zero Then Return False 'if the control doesn't have a handle (i.e not created..etc) return false.
Try
SetParent(windowHwnd, destHwnd) 'attempt to set the parent of the process to our handler!
Catch ex As Exception
Return False 'fucked up, damn.
End Try
Threading.Thread.Sleep(100) 'give it 100ms to register.
Dim children As IntPtr() = getChildWindows(destHwnd) 'now to make sure it worked! We'll enumerate all the children of the control handle
Dim enumChildren As IEnumerable(Of IntPtr) = From child As IntPtr In children Where getHwndName(child) = windowName Select child
Return enumChildren.Count > 0
End Function
Private Function getHwndName(ByVal hwnd As IntPtr)
Dim sBuilder As New System.Text.StringBuilder(256)
GetClassName(hwnd, sBuilder, sBuilder.Capacity)
Return sBuilder.ToString
End Function
Private Function waitForWindow(ByVal lpProcess As Process, Optional ByVal maxWait As Integer = 1000) As IntPtr
Dim currentWait As Integer = 0
While lpProcess.MainWindowHandle = IntPtr.Zero
lpProcess.Refresh() 'refresh the process, helps with random IntPtr.Zero results being returned even though there is clearly a window
If currentWait = maxWait Then Return IntPtr.Zero 'if we've waited long enough, fuck it and give up
Threading.Thread.Sleep(10) '10ms chunks.
currentWait += 10
End While
Return lpProcess.MainWindowHandle 'if we reached here there is a window handle, so return it.
End Function
[/highlight]
Now, to use it:
[highlight=vb.net]
If openProcessToControl("notepad.exe", Me) Then 'Please note, for processes other than notepad, you will need to specify the whole path to the executable. This will add notepad to the main control.
MessageBox.Show("Fuck yeah Jason, you da baws")
Else
MessageBox.Show("Awww man, it didn't work!")
End If
[/highlight]
Enjoy.
EDIT: Oh yeah you can remove all the shit about enumerating the child handles if you don't really care about being dead certain that it worked. Just return true after the setparent try-catch block and you'll be AOK.
EDIT2: If your process has a long initialize time, change the WaitForWindow time limit.
Originally Posted by Jason
I think i MAY have gone a little overboard here, but it piqued my interest so I coded out a method for doing it for any windows process (i hesitate with the "any" windows process ) I dunno if there is an easier way for a JAR applet, I didn't really check.
Please note, some methods are marked as not being written by me. I can't remember who wrote them but I take no credit for it. They are just 2 helper methods for enumerating the children of a given handle.
Here you go: (Note, it may look daunting, but it is really easy to use )
[highlight=vb.net]
Imports System****ntime.InteropServices
[/highlight]
[highlight=vb.net]
Private Declare Function SetParent Lib "user32.dll" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
Private Declare Sub GetClassName Lib "user32.dll" Alias "GetClassNameA" (ByVal hWnd As System.IntPtr, ByVal lpClassName As System.Text.StringBuilder, ByVal nMaxCount As Integer)
Private Declare Function EnumChildWindows Lib "user32.dll" (ByVal WindowHandle As IntPtr, ByVal Callback As EnumWindowCallback, ByVal lParam As IntPtr) As Boolean
Private Delegate Function EnumWindowCallback(ByVal hwnd As IntPtr, ByVal params As IntPtr) As Boolean
'METHOD NOT WRITTEN BY JASON, I DO NOT TAKE CREDIT
Public Shared Function getChildWindows(ByVal ParentHandle As IntPtr) As IntPtr()
Dim ChildrenList As New List(Of IntPtr)
Dim ListHandle As GCHandle = GCHandle.Alloc(ChildrenList)
Try
EnumChildWindows(ParentHandle, AddressOf EnumWindow, GCHandle.ToIntPtr(ListHandle))
Finally
If ListHandle.IsAllocated Then ListHandle.Free()
End Try
Return ChildrenList.ToArray
End Function
'METHOD NOT WRITTEN BY JASON, I DO NOT TAKE CREDIT.
Private Shared Function EnumWindow(ByVal Handle As IntPtr, ByVal Parameter As IntPtr) As Boolean
Dim ChildrenList As List(Of IntPtr) = GCHandle.FromIntPtr(Parameter).Target
ChildrenList.Add(Handle)
Return True
End Function
Private Function openProcessToControl(ByVal processLocation As String, ByVal parent As Control)
Dim sInfo As New ProcessStartInfo(processLocation) 'set the location of the process
Dim newApplet As New Process With {.StartInfo = sInfo} 'create the process
newApplet.Start() 'start the process
Dim windowHwnd As IntPtr = WaitForWindow(newApplet, 3000)
If windowHwnd = IntPtr.Zero Then Return False 'no handle, sorreh.
Dim windowName As String = getHwndName(windowHwnd) 'assigns the read class name to a variable
Dim destHwnd As IntPtr = parent.Handle 'get the handle of the input control.
If destHwnd = IntPtr.Zero Then Return False 'if the control doesn't have a handle (i.e not created..etc) return false.
Try
SetParent(windowHwnd, destHwnd) 'attempt to set the parent of the process to our handler!
Catch ex As Exception
Return False 'fucked up, damn.
End Try
Threading.Thread.Sleep(100) 'give it 100ms to register.
Dim children As IntPtr() = getChildWindows(destHwnd) 'now to make sure it worked! We'll enumerate all the children of the control handle
Dim enumChildren As IEnumerable(Of IntPtr) = From child As IntPtr In children Where getHwndName(child) = windowName Select child
Return enumChildren.Count > 0
End Function
Private Function getHwndName(ByVal hwnd As IntPtr)
Dim sBuilder As New System.Text.StringBuilder(256)
GetClassName(hwnd, sBuilder, sBuilder.Capacity)
Return sBuilder.ToString
End Function
Private Function waitForWindow(ByVal lpProcess As Process, Optional ByVal maxWait As Integer = 1000) As IntPtr
Dim currentWait As Integer = 0
While lpProcess.MainWindowHandle = IntPtr.Zero
lpProcess.Refresh() 'refresh the process, helps with random IntPtr.Zero results being returned even though there is clearly a window
If currentWait = maxWait Then Return IntPtr.Zero 'if we've waited long enough, fuck it and give up
Threading.Thread.Sleep(10) '10ms chunks.
currentWait += 10
End While
Return lpProcess.MainWindowHandle 'if we reached here there is a window handle, so return it.
End Function
[/highlight]
Now, to use it:
[highlight=vb.net]
If openProcessToControl("notepad.exe", Me) Then 'Please note, for processes other than notepad, you will need to specify the whole path to the executable. This will add notepad to the main control.
MessageBox.Show("Fuck yeah Jason, you da baws")
Else
MessageBox.Show("Awww man, it didn't work!")
End If
[/highlight]
Enjoy.
EDIT: Oh yeah you can remove all the shit about enumerating the child handles if you don't really care about being dead certain that it worked. Just return true after the setparent try-catch block and you'll be AOK.
EDIT2: If your process has a long initialize time, change the WaitForWindow time limit.
Not to mention i have no experience with vb atm and don't know what to do with these smexy codes .
Originally Posted by moelex64
Not to mention i have no experience with vb atm and don't know what to do with these smexy codes .
I GAVE you an example of using the function you just need to change "notepad.exe" to the full path to your applet.
Originally Posted by moelex64
Not to mention i have no experience with vb atm and don't know what to do with these smexy codes .
Errm..
This is not the easiest stuff..
Try to start with something smaller if you just started learning ^^..