MS Detours Injection - can someone help test please

Everything else that doesn't fall into one of the other PB categories.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

MS Detours Injection - can someone help test please

Post by SFSxOI »

I've been toying around with the MS Detours functions, and also wanted to play with Prototypes a little, so why not combine the two.

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
acidburn
User
User
Posts: 23
Joined: Mon Aug 07, 2006 1:37 pm

Post by acidburn »

hmmm can i use this code for AI PROGRAM FOR GAMES? heh I think I need also a packet sniffer. just wondering...
Post Reply