I have a piece of code which gives me the list of currently running processes in vb. Know I want to sort those processes according to their type for example : system processes seperatly etc.
Any ideas on this?
Printable View
I have a piece of code which gives me the list of currently running processes in vb. Know I want to sort those processes according to their type for example : system processes seperatly etc.
Any ideas on this?
By list of running processes I mean all processes which are running currently on windows.
I'm not expert on this, but running Task Manager in XP allows you to list the "User Name" for each process. I notice that several of these are named "SYSTEM", others are named "LOCAL SERVICE", etc.
Does your code return the User Name? Can you use it for what you want to do?
I thought that but what if I can create a windows service and run it under system acoount so the username is managed and it always shows system.
Well, I'm really making it up as I go along now, but isn't that the definition of a system process? I don't know. Maybe you'd better hope that someone else jumps in on this thread.Quote:
I thought that but what if I can create a windows service and run it under system acoount so the username is managed and it always shows system.
Some one tell me how to use GetProcessAffinityMask function.
After spending some time Googling, I saw no examples of using:Quote:
Originally Posted by pbuddy_8
GetProcessAffinityMask
SetProcessAffinityMask
SetThreadAffinityMask
with VB. I did, however, find some examples using C++. Are you familiar with that language?
ack No
No I want to use these in vb
I found this. I ran, and really don't know what it is telling me, but I'm sure you will.Quote:
Originally Posted by pbuddy_8
VB Code:
Private Declare Function GetCurrentProcess Lib "Kernel32.dll" () As Long Private Declare Function GetProcessAffinityMask Lib "Kernel32.dll" ( _ ByVal hProcess As Long, ByRef lpProcessAffinityMask As Long, _ ByRef SystemAffinityMask As Long) As Long Private Sub Form_Load() Dim ProcMask As Long, SysMask As Long Call GetProcessAffinityMask(GetCurrentProcess(), ProcMask, SysMask) Debug.Print "Process affinity mask: 0x" & Hex(ProcMask) Debug.Print "SystemAffinityMask: 0x" & Hex(SysMask) End Sub
Here is what I came up with.
The data returned from the GetProcessAffinityMask contains what processors are available and which are currently in the mask, just because more than one processor is in the mask does not mean it will be used.
The data it bit-packed into the return variables, each bit is a processor.
7 6 5 4 3 2 1 0 Processor #
0 0 0 0 0 0 1 1 Processors Available
If you have 2 processor your SystemMask value would probably be 3 (00000011) in bianary
The code presented will find the last processor and set the process to only use last processor. If you only have 1 processor nothing will be done.
Knowing this information the code should be self explanitory
Your little bit of code above helped me so I thought I would share this.
Thanks,
William Solberg
Code:Private Declare Function GetCurrentProcess Lib "Kernel32.dll" () As Long
Private Declare Function GetProcessAffinityMask Lib "Kernel32.dll" ( _
ByVal hProcess As Long, ByRef lpProcessAffinityMask As Long, _
ByRef SystemAffinityMask As Long) As Boolean
Private Declare Function SetProcessAffinityMask Lib "Kernel32.dll" ( _
ByVal hProcess As Long, ByVal dwProcessAffinityMask As Long) As Long
Private Sub Form_Load()
Dim ProcMask As Long, SysMask As Long
If GetProcessAffinityMask(GetCurrentProcess(), ProcMask, SysMask) Then
If SysMask > 1 Then
Dim processor As Integer
processor = 0
For i = 1 To 7 'Looks for other processor besides 0
If checkbit(SysMask, i) Then processor = i
Next i
If processor Then
Dim ProcessorMask As Long
ProcessorMask = 2 ^ processor 'Creates the mask for only the last processor found
If SetProcessAffinityMask(GetCurrentProcess(), ProcessorMask) Then
' Any special code you want to perform if successful
End If
End If
End If
End If
End Sub
Private Function checkbit(ByVal n As Integer, bitnumber as Integer) As Boolean
Dim i As Integer
For i = 0 To 7
If 2 ^ i And n Then
If bitnumber = i Then
checkbit = True
Exit Function
End If
End If
Next i
End Function