
Code: Select all
#kernel32 = 1
;
buffer.s{512}
max.i = Len(buffer)-2
window_h.i
window_name.s
process_id.i
process_h.i
application_name.s
;
; GetModuleFIleNameEx is not by default included in PureBasic, so open the appropriate DLL or LIB
; (Psapi.lib on Windows XP?)
;
OpenLibrary(#kernel32,"kernel32.dll")
;
; GetModuleFileNameW is the unicode version of GetModuleFileName
; Windows 7 complicates matters a bit, but K32GetModuleFileNameExW seems to work fine on Win7
;
; classic way by locating and executing the function
;
; K32GetModuleFileNameExW_ = GetFunction(#kernel32,"K32GetModuleFileNameExW")
; l = CallFunctionFast(K32GetModuleFileNameExW_ ,process_h, 0, @buffer, max)
; application_name = PeekS(@buffer,l)
;
; the alternative way using a prototype
;
; Prototype K32GetModuleFileName_(process_h.i, module_h.i, buffer_p.i, buffer_l.i)
; K32GetModuleFileName_.K32GetModuleFileName_ = GetFunction(#kernel32,"K32GetModuleFileNameExW")
; l = K32GetModuleFileName_(process_h,0,@buffer,max)
; application_name = PeekS(@buffer,l)
;
; prototypes make more sense when you repeatedly call the function in question
;
Prototype K32GetModuleFileName_(process_h.i, module_h.i, buffer_p.i, buffer_l.i)
K32GetModuleFileName_.K32GetModuleFileName_ = GetFunction(#kernel32,"K32GetModuleFileNameExW")
;
window_h = GetForegroundWindow_()
If window_h <> 0
l = GetWindowText_(window_h,@buffer,max)
window_name = PeekS(@buffer,l)
GetWindowThreadProcessId_(window_h,@process_id)
If process_id <> 0
process_h = OpenProcess_(#PROCESS_QUERY_INFORMATION|#PROCESS_VM_READ , #False, process_id)
If process_h <> 0
l = K32GetModuleFileName_(process_h,0,@buffer,max)
application_name = PeekS(@buffer,l)
EndIf
EndIf
;
Debug window_name
Debug application_name
EndIf
;
CloseLibrary(#kernel32)