I've got this code i've created, and I think it works OK so far but i'm not really sure. It will inject a .dll at the application start up (which i've found out is when you want to create DirectX stuff in other applications). I'd like to ask if someone else can check it out also and make sure its working. You need the detours.lib file from the MS detours package (place it in your PureBasic folder under \PureLibraries\Windows\Libraries). The .dll file mentioned in the code is a simple test file i'm using, you can substitue just about any .dll (i think). Just put in the application and directory info and compile/run.
If someone could give it a quick check i'd appreciate it.
Code: Select all
;dll injection loader
; PureBasic 4, Beta 11
Import "detours.lib"
DtrCPWDA.b(lpApplicationName$, lpCommandLine$, lpProcessAttributes, lpThreadAttributes, bInheritHandles.b, dwCreationFlags.l, lpEnvironment, lpCurrentDirectory$ , lpStartupInfo, lpProcessInformation, lpDllName$, pfCreateProcessA) As "_DetourCreateProcessWithDllA@48"
EndImport
TargetCommand$ = "App.exe -windowed" ;application name with any startup commands
TargetApp$ = "C:\App\myApp\App.exe" ;full path to application
TargetFolder$ = "C:\App\myApp"; full path to working dir less last '\'
UseLibrary$ = "Direct3D-Hook.dll"; dll full path or dll in same folder with injector, try any dll
SetCurrentDirectory_(TargetFolder$);
;Create Process and Inject our dll file:
startinfo.STARTUPINFO
procinfo.PROCESS_INFORMATION
ZeroMemory_(@startinfo, SizeOf(startinfo));
ZeroMemory_(@procinfo, SizeOf(procinfo));
startinfo\cb = SizeOf(startinfo)
If Not DtrCPWDA(TargetApp$, TargetCommand$, 0, 0, #True, #CREATE_DEFAULT_ERROR_MODE|#CREATE_NEW_CONSOLE, #Null, TargetFolder$ , @startinfo, @procinfo, UseLibrary$, 0)
Debug "DetourCreateProcessWithDllA failed"
Else
Debug "Injection Successful. Will exit when target exits.";
;Loader will standby until the injected target ends.
WaitForSingleObject_(procinfo\hProcess, #INFINITE);
Debug "Target Ended.";
EndIf

