DllCall GetProcessAffinityMas

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
SvenBent
Posts: 266
Joined: 09 Aug 2015, 01:34

DllCall GetProcessAffinityMas

24 May 2016, 13:04

I'm apparently still not totally into the DllCalls

im trying to make a function that returns the Priorty mask of a gien process

Code: Select all

blah:= AffinityGet(156)
msgbox %blah%

Return

AffinityGet(PID)
	{ 
	hPr := DllCall( "OpenProcess",Int,1536,Int,0,Int,PID )  
	DllCall( "GetProcessAffinityMask", Int,hPr, Ptr, PAf, Ptr, SAf )
	DllCall( "CloseHandle", Int,hPr )
	Return PAf
	}

I believe I'm messing up the return value pointers. can someone please explain me what om doing wrong and why ?
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

Re: DllCall GetProcessAffinityMas

24 May 2016, 14:03

Code: Select all

AffinityGet(pid)
{
    hProc := DllCall("OpenProcess", "UInt", 1536, "Int", 0, "UInt", pid)
    DllCall("GetProcessAffinityMask", "Ptr", hProc, "UPtrP", paf, "UPtrP", saf)
    DllCall("CloseHandle", "Ptr", hProc)
    return paf
}
SvenBent
Posts: 266
Joined: 09 Aug 2015, 01:34

Re: DllCall GetProcessAffinityMas

24 May 2016, 14:07

So my Pointers in the dll call was wrong . correct ?

UPtrP
U for beeing unsigned. Ptr for beeing and pointer size integer but not an actual pointer and P because it a pointer aka the adresse and not the value I'm receiving. On the right track ?

Why closehanlde with Ptr and not Int ?


--- edit ---
Reports 0 no matter what affinoty process with PID 156 has
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

Re: DllCall GetProcessAffinityMas

24 May 2016, 14:13

SvenBent wrote:Why closehanlde with Ptr and not Int ?
AHK Docs wrote: PTR
A pointer-sized integer, equivalent to Int or Int64 depending on whether the exe running the script is 32-bit or 64-bit. Ptr should be used for pointers to arrays or structures (such as RECT* or LPPOINT) and almost all handles (such as HWND, HBRUSH or HBITMAP). If the parameter is a pointer to a single numeric value such as LPDWORD or int*, generally the * or P suffix should be used instead of "Ptr".

Source: DllCall
Here's a handy tool from jNizM for DllCall's & Windows Data Types: Windows Data Types for AHK
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: DllCall GetProcessAffinityMas

24 May 2016, 14:15

Edit: I didn't see the previous replies. :thumbup: you can ignore this.

This seems to work.

Code: Select all

blah:= AffinityGet(5672)
msgbox %blah%
Return
 
AffinityGet(PID)
{ 
    ; PROCESS_QUERY_INFORMATION (0x0400)
    ; PROCESS_SET_INFORMATION (0x0200)
    ; 0x0200 | 0x0400 = 1536
    hPr := DllCall( "OpenProcess","Int",1536, "Int",0, "Int",PID )
    ;~ MsgBox, % hPr "`n" A_LastError
    VarSetCapacity(PAf, 8, 0), VarSetCapacity(SAf, 8, 0)
    DllCall( "GetProcessAffinityMask", "Ptr",hPr, "Ptr",&PAf, "Ptr",&SAf )
    ;~ MsgBox, % A_LastError "`n" NumGet(PAf, 0, "Int") 
    DllCall( "CloseHandle", "Ptr",hPr )
    Return NumGet(PAf, 0, "Int64")
}
Return value

If the function succeeds, the return value is nonzero and the function sets the variables pointed to by lpProcessAffinityMask and lpSystemAffinityMask to the appropriate affinity masks.

On a system with more than 64 processors, if the threads of the calling process are in a single processor group, the function sets the variables pointed to by lpProcessAffinityMask and lpSystemAffinityMask to the process affinity mask and the processor mask of active logical processors for that group.
...

Remarks

A process affinity mask is a bit vector in which each bit represents the processors that a process is allowed to run on.
- https://msdn.microsoft.com/en-us/librar ... 85%29.aspx
^^Based on that I guessed that a ProcessAffinityMask is 64 bits. I used VarSetCapacity to allocate space in the variables prior to calling GetProcessAffinityMask.

I also changed the size of hPr to "Ptr". Handles are pointer sized.
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

Re: DllCall GetProcessAffinityMas

24 May 2016, 14:26

SvenBent wrote:UPtrP
U for beeing unsigned. Ptr for beeing and pointer size integer but not an actual pointer and P because it a pointer aka the adresse and not the value I'm receiving. On the right track ?

Code: Select all

BOOL WINAPI GetProcessAffinityMask(
  _In_  HANDLE     hProcess,
  _Out_ PDWORD_PTR lpProcessAffinityMask,
  _Out_ PDWORD_PTR lpSystemAffinityMask
);
PDWORD_PTR is defined as a pointer to a DWORD_PTR (typedef DWORD_PTR *PDWORD_PTR;) and DWORD_PTR is defined as an unsigned long type for pointer precision (typedef ULONG_PTR DWORD_PTR;). ULONG_PTR is:

Code: Select all

#if defined(_WIN64)
 typedef unsigned __int64 ULONG_PTR;
#else
 typedef unsigned long ULONG_PTR;
#endif
UPtrP - U - Unsigned ; Ptr - long type for pointer precision ; P - a pointer to a ....
SvenBent
Posts: 266
Joined: 09 Aug 2015, 01:34

Re: DllCall GetProcessAffinityMas

24 May 2016, 14:30

Ty for all the help and explenations

@ cocoo
Oh thank you so i should in generelt use Closehanlde with ptr and not int. ?
I've just seen alot of them with just int. or maybe it not a bigg difference ?


@ Kon
Thank you it still only gave me 0 until i realized I was asking to retrieve affinity info from system process. As soon as i tried from a user process it works flawlessly.
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
Contact:

Re: DllCall GetProcessAffinityMas

24 May 2016, 14:44

SvenBent wrote:i should in generelt use Closehanlde with ptr and not int. ?
Use Ptr for handles such as hWnd, hProcess, hFile, hPipe etc.
SvenBent wrote:I've just seen alot of them with just int. or maybe it not a bigg difference ?
Those are mostly legacy code but should work fine for 32-bit AHK. However, I suggest you use Ptr (when appropriate) moving forward.
SvenBent
Posts: 266
Joined: 09 Aug 2015, 01:34

Re: DllCall GetProcessAffinityMas

24 May 2016, 14:47

More questions

@Kon you changed
DllCall( "GetProcessAffinityMask", Int,hPr, Ptr, PAf, Ptr, SAf )
to
DllCall( "GetProcessAffinityMask", "Ptr",hPr, "Ptr",&PAf, "Ptr",&SAf )

which is 2 changes as far as i see

1: Changed type of variable hPR to ptr because as Coco also told me: Handles are pointers.
So in general i should use ptr when dealing with handles correct ?

2: You changed PAf and SAf to &PAf and &SAf. from what I've gather just reading around & indicates a pointer. So since the outputs is a pointer we are using the pointer indication on the variable.

You also inserted 2 extra lines

1: VarSetCapacity(PAf, 8, 0), VarSetCapacity(SAf, 8, 0)
So we are sure the Variables are Exactly 8bytes ( 64bits) which is the size of the affinity mask. When do i need to set that? is it a generally good thing or just for pointers ?


2: NumGet(PAf, 0, "Int64")
Because we are working with pointers so the variable contains an adress and not values. We use NumGet to get the actual value from where the pointer is pointing ?
Adress, ExtraToAddOnAdress, "TypeOfValueToGet"


Thank you for all the help im getting a bit closer to understand it i think :D


@Coco

Thank you for the explenation about the legacy part. I will adjust my code to be more correct
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: DllCall GetProcessAffinityMas

25 May 2016, 01:33

Like coco said before, see here Windows Data Types for AHK if you need help for the Windows Data Types
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: aitaixy, Anput, dangoscrub, Google [Bot], joedf, Nerafius and 149 guests