函数内要调用其它的的函数只能是本进程内的代码(即使 API 调用也要先通过 VB 提供的一些通用动作来定位这个函数)。 而你前面的帖子中提到,是通过 WriteProcessMemory 将一个函数的整块机器指令写入到目标进程中,但是被它调用所需的其它函数在目标进程中是没有的。 所以仅限于可以独立执行的函数可以跨进程复制并成功调用。
'先看下你的工程属性是不是支持多线程方式 'AsyncThread 'Using the CreateThread function in Visual Basic 'is very risky! VB5 is 'kinda' stable, but VB6 'applications will probably crash when you 'use the CreateThread function.Private Sub Command1_Click() hThread = CreateThread(ByVal 0&, ByVal 0&, AddressOf AsyncThread, ByVal 0&, ByVal 0&, hThreadID) CloseHandle hThread End Sub Private Sub Form_Unload(Cancel As Integer) If hThread <> 0 Then TerminateThread hThread, 0 End Sub 'In a module Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Declare Function CreateThread Lib "kernel32" (lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadID As Long) As Long Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Public hThread As Long, hThreadID As Long Public Sub AsyncThread() Sleep 10000 hThread = 0 End Sub
但千万不要在商业软件上应用,不然你会被用户骂到狗血淋头的^_^!
而你前面的帖子中提到,是通过 WriteProcessMemory 将一个函数的整块机器指令写入到目标进程中,但是被它调用所需的其它函数在目标进程中是没有的。
所以仅限于可以独立执行的函数可以跨进程复制并成功调用。
'AsyncThread
'Using the CreateThread function in Visual Basic
'is very risky! VB5 is 'kinda' stable, but VB6
'applications will probably crash when you
'use the CreateThread function.Private Sub Command1_Click()
hThread = CreateThread(ByVal 0&, ByVal 0&, AddressOf AsyncThread, ByVal 0&, ByVal 0&, hThreadID)
CloseHandle hThread
End Sub
Private Sub Form_Unload(Cancel As Integer)
If hThread <> 0 Then TerminateThread hThread, 0
End Sub
'In a module
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Declare Function CreateThread Lib "kernel32" (lpThreadAttributes As Any, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadID As Long) As Long
Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public hThread As Long, hThreadID As Long
Public Sub AsyncThread()
Sleep 10000
hThread = 0
End Sub