Page 1 of 1

3 new Windows 7 API's

Posted: Sat Nov 14, 2009 11:28 pm
by SFSxOI
Nothing really exciting, just thought i'd post them in case anyone else is messing around with threads and processors in Windows 7, using three of the new Windows 7 API's - SetThreadIdealProcessorEx, GetCurrentProcessorNumberEx, GetActiveProcessorGroupCount:

Code: Select all

Prototype.i PFNCreateToolhelp32Snapshot(dwFlags.i, th32ProcessID.i) ;
Prototype.b PFNProcess32First(hSnapshot.i, *lppe.PROCESSENTRY32) ;
Prototype.b PFNProcess32Next(hSnapshot.i, *lppe.PROCESSENTRY32) ;

; gets PID of an .exe
; usage: GetPidByName("notepad.exe")
Procedure GetPidByName(p_name$) 
    Protected hDLL.i, process_name$ 
    Protected PEntry.PROCESSENTRY32, hTool32.i 
    Protected pCreateToolhelp32Snapshot.PFNCreateToolhelp32Snapshot 
    Protected pProcess32First.PFNProcess32First 
    Protected pProcess32Next.PFNProcess32Next 
    Protected pid.i 
    
    hDLL = OpenLibrary(#PB_Any,"kernel32.dll") 
    
    If hDLL 
        pCreateToolhelp32Snapshot = GetFunction(hDLL,"CreateToolhelp32Snapshot") 
        pProcess32First = GetFunction(hDLL,"Process32First") 
        pProcess32Next = GetFunction(hDLL,"Process32Next") 
    Else 
        ProcedureReturn 0 
    EndIf 
    
    PEntry\dwSize = SizeOf(PROCESSENTRY32) 
    hTool32 = pCreateToolhelp32Snapshot(#TH32CS_SNAPPROCESS, 0) 
    pProcess32First(hTool32, @PEntry) 
    process_name$ = Space(#MAX_PATH) 
    CopyMemory(@PEntry\szExeFile,@process_name$,#MAX_PATH) 
    
    If  UCase(process_name$) = UCase(p_name$) 
        ProcedureReturn PEntry\th32ProcessID 
    EndIf 
    
    While pProcess32Next(hTool32, @PEntry) > 0 
        process_name$ = Space(#MAX_PATH) 
        CopyMemory(@PEntry\szExeFile,@process_name$,#MAX_PATH) 
        
        If  UCase(process_name$) = UCase(p_name$) 
            ProcedureReturn PEntry\th32ProcessID 
        EndIf 
    
    Wend 
    
    CloseLibrary(hDLL) 
    
    ProcedureReturn 0 
EndProcedure


; gets thread or process handle
; #Get_Thread_Handle = 1
; #Get_Process_Handle = 2
; uasge:
; hThread.i = Get_Thread_Process_Handle("notepad.exe", #Get_Thread_Handle)
; hThread.i = Get_Thread_Process_Handle("notepad.exe", #Get_Process_Handle)

Procedure Get_Thread_Process_Handle(Proc_file_mame$, Thread_Proc_Handle.i)

Define te.THREADENTRY32
Protected out_pDisableTPriorityBoost.i, Proc_PriorityBoost.i

PID = GetPidByName(Proc_file_mame$)

h=CreateToolhelp32Snapshot_(#TH32CS_SNAPTHREAD,0);

Lib_OpenThread = LoadLibrary_("kernel32.dll")

If Lib_OpenThread
  *h_Thread = GetProcAddress_(Lib_OpenThread, "OpenThread")
  FreeLibrary_(Lib_OpenThread)
EndIf

If (h<>#INVALID_HANDLE_VALUE) 
  te\dwSize=SizeOf(te) 
    If (Thread32First_(h,@te)) 
      Repeat 
      If (te\dwSize>=OffsetOf(THREADENTRY32\th32OwnerProcessID)+SizeOf(te\th32OwnerProcessID)) 
          If PID = te\th32OwnerProcessID
            hProcess.i = OpenProcess_(#PROCESS_ALL_ACCESS_VISTA_WIN7,#False,te\th32OwnerProcessID)
            hThread.i = CallFunctionFast(*h_Thread, #PROCESS_ALL_ACCESS_VISTA_WIN7,#False,te\th32ThreadID)
          EndIf
        EndIf 
        te\dwSize=SizeOf(te)
      Until Not (Thread32Next_(h,@te)); 
    EndIf
  CloseHandle_(h)
EndIf

If Thread_Proc_Handle = #Get_Thread_Handle ; = 1
ProcedureReturn hThread
EndIf

If Thread_Proc_Handle = #Get_Process_Handle ; =2
ProcedureReturn hProcess
EndIf 

EndProcedure

;________________________________________________________________________________
; Gets the active processor group count
; no parameters
; Winodws 7 and above only
; uasge GetActiveProcessorGroupCount()

Procedure GetActiveProcessorGroupCount()

Lib_ProcGroupCount = LoadLibrary_("kernel32.dll")
*Func_ProcGroupCount = GetProcAddress_(Lib_ProcGroupCount, "GetActiveProcessorGroupCount")

ProcGroupCount.i = CallFunctionFast(*Func_ProcGroupCount)

FreeLibrary_(Lib_ProcGroupCount)

ProcedureReturn ProcGroupCount

EndProcedure

;________________________________________________________________________________
; Retrieves the processor group and number of the logical processor in which the calling thread is running.
; Winodws 7 and above only

Procedure.s GetCurrentProcessorNumberEx()

PPROCESSOR_NUMBER.PROCESSOR_NUMBER

Lib_ProcessorNumber = LoadLibrary_("kernel32.dll")
*Func_ProcessorNumber = GetProcAddress_(Lib_ProcessorNumber, "GetCurrentProcessorNumberEx")

PPROCESSORNUMBER.i = CallFunctionFast(*Func_ProcessorNumber, @PPROCESSOR_NUMBER)

FreeLibrary_(Lib_ProcessorNumber)

ProcedureReturn Str(PPROCESSOR_NUMBER\Group) + "    " + Str(PPROCESSOR_NUMBER\Number)

EndProcedure


;________________________________________________________________________________
; Sets the ideal processor for the specified thread and optionally retrieves the previous ideal processor.

; usage:
; hThread.i = Get_Thread_Process_Handle("notepad.exe", #Get_Thread_Handle)
; Debug SetThreadIdealProcessorEx(Hthread, 1)
; Winodws 7 and above only

Procedure SetThreadIdealProcessorEx(Thread_Handle.i, PPROC_Number.b)

PPROCESSOR_NUMBER.PROCESSOR_NUMBER
PPROCESSOR_NUMBER\Number = PPROC_Number
;PPROCESSOR_NUMBER\Group = PPROCESSOR_Group

Lib_IdealProcessor = LoadLibrary_("kernel32.dll")
*Func_IdealProcessor = GetProcAddress_(Lib_IdealProcessor, "SetThreadIdealProcessorEx")

IdealProc.i = CallFunctionFast(*Func_IdealProcessor, Thread_Handle, @PPROCESSOR_NUMBER, #Null)

FreeLibrary_(Lib_IdealProcessor)

ProcedureReturn IdealProc

EndProcedure


I also posted the associated code i'm using to experiment with (Get_Thread_Process_Handle(Proc_file_mame$, Thread_Proc_Handle.i) and GetPidByName(p_name$) )to help you get started in case you don't already have something like this.

A list of all the new Windows 7 API's is here > http://msdn.microsoft.com/en-us/library ... S.85).aspx

Re: 3 new Windows 7 API's

Posted: Mon Nov 16, 2009 12:43 pm
by Tranquil
Thanks for posting. Anyway, your link leads to a "sorry page not found" site here. :-(

Re: 3 new Windows 7 API's

Posted: Mon Nov 16, 2009 11:58 pm
by SFSxOI
copy and paste the link, for some reason the forum won't pick up the aspx part...or.....here it is again this time posted with the url tags > http://msdn.microsoft.com/en-us/library ... S.85).aspx