Option Explicit
Public Declare Function VirtualAllocEx Lib "kernel32" (ByVal hProcess As Long, ByVal lpAddress As Long, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
Public Declare Function CreateThread Lib "kernel32" (ByVal lpSecurityAttributes As Long, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, ByVal lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadId As Long) As Long
Public Type 参数
address As Long 'addressof sub or fun
ThreadInf As Long '线程信息
CriticalAI As Long '临界区标识
'reserved As Long
Arguments As Long '参数数量
End Type
Public Const CREATE_DEFAULT = &H0& '默认值,创建一个立即运行的线程
Public Function VBCreatThread(ByVal address As Long, ByVal CriticalAI As Long, ByRef ThreadID As Long) As Boolean
Dim a As Long, HandleOfThread As Long, PassParameters As 参数, Arguments As Long
Arguments = 1
a = VirtualAllocEx(-1, 0, 20 + (Arguments * 4), MEM_COMMIT, PAGE_EXECUTE_READWRITE)
If a = 0 Then: Exit Function
With PassParameters
.address = address '0
.ThreadInf = 0 '4 线程返回信息
.CriticalAI = CriticalAI '8
.Arguments = Arguments * 4 '10
End With
RtlMoveMemory a, ByVal VarPtr(PassParameters), 20
'WriteProcessMemory -1, a, ByVal VarPtr(PassParameters), 20, 0
'-----------------------常规信息
PutMem4 a + 20, 0&
'WriteProcessMemory -1, a + 20, 0, Arguments * 4, 0
'-----------------------参数信息
HandleOfThread = CreateThread(ByVal 0&, ByVal 0&, address, a, ByVal CREATE_DEFAULT, ThreadID) '创建立即执行
' WaitForSingleObject 线程句柄, &HFFFF
' CloseHandle 线程句柄
End Function
'这里是线程循环结构
Public Sub MainThread()
Do While (True)
PutMem4 &H400500, 1&
Loop
End Sub
新建的模块添加上列代码,创建属于我们自己的线程执行我们自己的功能