邪恶八进制信息安全团队技术讨论组's Archiver

小珂 2006-7-11 11:03

[转载]VB编程实现两个可执行文件的绑定

文章作者:tankl
信息来源:邪恶八进制信息安全团队([url]www.eviloctal.com[/url])


Option Explicit

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function Putfocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
Const GW_HWNDNEXT = 2
Dim mWnd As Long
Function InstanceToWnd(ByVal target_pid As Long) As Long
   Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
   'Find the first window
   test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
   Do While test_hwnd <> 0
      &#39;Check if the window isn&#39;t a child
      If GetParent(test_hwnd) = 0 Then
        &#39;Get the window&#39;s thread
        test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
        If test_pid = target_pid Then
           InstanceToWnd = test_hwnd
           Exit Do
        End If
      End If
      &#39;retrieve the next window
      test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
   Loop
End Function
Private Sub Form_Load()
   &#39;KPD-Team 1999
   &#39;URL: [url]http://www.allapi.net/[/url]
   &#39;E-Mail: [email]KPDTeam@Allapi.net[/email]
   Dim Pid As Long
   &#39;Lock the window update
   LockWindowUpdate GetDesktopWindow
   &#39;Execute notepad.Exe
   Pid = Shell("c:\windows\notepad.exe", vbNormalFocus)
   If Pid = 0 Then MsgBox "Error starting the app"
   &#39;retrieve the handle of the window
   mWnd = InstanceToWnd(Pid)
   &#39;Set the notepad&#39;s parent
   SetParent mWnd, Me.hwnd
   &#39;Put the focus on notepad
   Putfocus mWnd
   &#39;Unlock windowupdate
   LockWindowUpdate False
End Sub
Private Sub Form_Unload(Cancel As Integer)
   &#39;Unload notepad
   DestroyWindow mWnd
   &#39;End this program
   TerminateProcess GetCurrentProcess, 0
End Sub

页: [1]
© 1999-2008 EvilOctal Security Team