Attribute VB_Name = "DllInjector"

Private Declare Function GetProcAddress Lib "Kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
Private Declare Function GetModuleHandle Lib "Kernel32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long
Private Declare Function LoadLibrary Lib "Kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Private Declare Function VirtualAllocEx Lib "Kernel32" (ByVal hProcess As Long, lpAddress As Any, ByVal dwSize As Long, ByVal fAllocType As Long, FlProtect As Long) As Long
Public Declare Function WriteProcessMemory Lib "Kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Declare Function CreateRemoteThread Lib "Kernel32" (ByVal ProcessHandle As Long, lpThreadAttributes As Long, ByVal dwStackSize As Long, ByVal lpStartAddress As Any, ByVal lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadID As Long) As Long
Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long)


'The Injection Function
Public Function InjectDll(dllpath As String, ProsH As Long)
Dim DLLVirtLoc As Long, DllLength, Inject As Long, LibAddress As Long
Dim CreateThread As Long, ThreadID As Long

'STEP 1 -  The easy part...Putting the bitch in the process' memory
'Find a nice spot for your DLL to chill using VirtualAllocEx
g_virutalallocex:
DllLength = Len(dllpath)
DLLVirtLoc = VirtualAllocEx(ProsH, ByVal 0, DllLength, &H1000, ByVal &H4)
If DLLVirtLoc = 0 Then End 'bla = MsgBox("VirtualAllocEx API failed! - try again?", vbYesNo, "ERROR")
'If bla = vbYes Then GoTo g_virutalallocex
'If bla = vbNo Then End

'Inject the Dll into that spot
g_writepmemory:
Inject = WriteProcessMemory(ProsH, DLLVirtLoc, ByVal dllpath, DllLength, vbNull)
If Inject = 0 Then End 'bla = MsgBox("Failed to Write DLL to Process! - try again?", vbYesNo, "ERROR")
'If bla = vbYes Then GoTo g_writepmemory
'If bla = vbNo Then End

'STEP 2 - Loading it in the process
'Just throwing our Dll into the process isnt going to do sth unless you
'Load it into the precess address using LoadLibrary.  The LoadLibrary function
'maps the specified executable module into the address space of the
'calling process.  You call LoadLibrary by using CreateRemoteThread to
'create a thread that runs in the address space of another process.
'First we find the LoadLibrary API function and store it
g_loadlibary:
LibAddress = GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA")
If LibAddress = 0 Then End 'bla = MsgBox("Can't find LoadLibrary API from kernel32.dll", vbYesNo, "ERROR")
'If bla = vbYes Then GoTo g_loadlibary
'If bla = vbNo Then End
'We set a pointer to LoadLibrary(LibAddress) in our process, LoadLibrary then puts
'our Dll(DLLVirtLoc) into the process address.  Easy enough right?
g_creatthread:
CreateThread = CreateRemoteThread(ProsH, vbNull, 0, LibAddress, DLLVirtLoc, 0, ThreadID)
If CreateThread = 0 Then End 'bla = MsgBox("Failed to Create Thead! - try again?", vbYesNo, "ERROR")
'If bla = vbYes Then GoTo g_creatthread
'If bla = vbNo Then End

End Function

