登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

miaozk2006

点滴积累与收藏:关于技术,关于生活

 
 
 

日志

 
 

在VB中如何让线程或进程在指定的CPU上运行  

2013-02-10 08:24:16|  分类: 编程-VB |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

代码如下:

Option Explicit

Private Declare Function WTSEnumerateProcesses Lib "wtsapi32.dll" Alias "WTSEnumerateProcessesA" (ByVal hServer As Long, ByVal Reserved As Long, ByVal Version As Long, ByRef ppProcessInfo As Long, ByRef pCount As Long) As Long
Private Declare Function SetProcessAffinityMask Lib "kernel32.dll" (ByVal hProcess As Long, ByVal dwProcessAffinityMask As Long) As Long
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Private Declare Sub WTSFreeMemory Lib "wtsapi32.dll" (ByVal pMemory As Long)
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Const WTS_CURRENT_SERVER_HANDLE = 0&
Private Type WTS_PROCESS_INFO
    SessionID As Long
    ProcessID As Long
    pProcessName As Long
    pUserSid As Long
End Type

Public Sub Main()
    Call SetAffinityByEXE("notepad.exe")
End Sub

Private Sub SetAffinityByEXE(strImageName As String)
    Const PROCESS_QUERY_INFORMATION = 1024
    Const PROCESS_VM_READ = 16
    Const MAX_PATH = 260
    Const STANDARD_RIGHTS_REQUIRED = &HF0000
    Const SYNCHRONIZE = &H100000
    Const PROCESS_ALL_ACCESS = &H1F0FFF
    Const TH32CS_SNAPPROCESS = &H2&
    Const hNull = 0
    Const WIN95_System_Found = 1
    Const WINNT_System_Found = 2
    Const Default_Log_Size = 10000000
    Const Default_Log_Days = 0
    Const SPECIFIC_RIGHTS_ALL = &HFFFF
    Const STANDARD_RIGHTS_ALL = &H1F0000

    Dim BitMasks() As Long, NumMasks As Long, LoopMasks As Long
    Dim MyMask As Long
    Const AffinityMask As Long = &HF ' 00001111b

    Dim lngPID As Long
    Dim lngHwndProcess
    lngPID = GetProcessID(strImageName)

    If lngPID = 0 Then
        MsgBox "Could Not Get process ID of " & strImageName, vbCritical, "Error"
        Exit Sub
    End If
    lngHwndProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, lngPID)
    If lngHwndProcess = 0 Then
        MsgBox "Could Not obtain a handle For the Process ID: " & lngPID, vbCritical, "Error"
        Exit Sub
    End If
    BitMasks() = GetBitMasks(AffinityMask)

    'Use CPU0
    MyMask = BitMasks(0)
    'Use CPU1
    'MyMask = BitMasks(1)
    'Use CPU0 and CPU1
    'MyMask = BitMasks(0) Or BitMasks(1)
    'The CPUs to use are specified by the array index.
    'To use CPUs 0, 2, and 4, you would use:
    'MyMask = BitMasks(0) Or BitMasks(2) Or BitMasks(4)
    'To Set Affinity, pass the application h
    '     andle and your custom affinity mask:
    'SetProcessAffinityMask(lngHwndProcess,
    '     MyMask)
    'Use GetCurrentProcess() API instead of
    '     lngHwndProcess to set affinity on the current app.

    If SetProcessAffinityMask(lngHwndProcess, MyMask) = 1 Then
        MsgBox "Affinity Set", vbInformation, "Success"
    Else
        MsgBox "Failed To Set Affinity", vbCritical, "Failure"
    End If
End Sub

Private Function GetBitMasks(ByVal inValue As Long) As Long()
    Dim RetArr() As Long, NumRet As Long
    Dim LoopBits As Long, BitMask As Long
    Const HighBit As Long = &H80000000
    ReDim RetArr(0 To 31) As Long

    For LoopBits = 0 To 30
        BitMask = 2 ^ LoopBits
        If (inValue And BitMask) Then
            RetArr(NumRet) = BitMask
            NumRet = NumRet + 1
        End If
    Next LoopBits
    If (inValue And HighBit) Then
        RetArr(NumRet) = HighBit
        NumRet = NumRet + 1
    End If
    If (NumRet > 0) Then ' Trim unused array items and return array
        If (NumRet < 32) Then ReDim Preserve RetArr(0 To NumRet - 1) As Long
        GetBitMasks = RetArr
    End If
End Function

Private Function GetProcessID(strProcessName As String) As Long
    Dim RetVal As Long
    Dim Count As Long
    Dim i As Integer
    Dim lpBuffer As Long
    Dim p As Long
    Dim udtProcessInfo As WTS_PROCESS_INFO
    Dim lngProcessID As Long
    Dim strTempProcessName As String
   
    RetVal = WTSEnumerateProcesses(WTS_CURRENT_SERVER_HANDLE, 0&, 1, lpBuffer, Count)
    If RetVal Then ' WTSEnumerateProcesses was successful
        p = lpBuffer
        For i = 1 To Count
            ' Count is the number of Structures in the buffer
            ' WTSEnumerateProcesses returns a pointer, so copy it to a
            ' WTS_PROCESS_INO UDT so you can access its members
            CopyMemory udtProcessInfo, ByVal p, LenB(udtProcessInfo)
            ' Add items to the ListView control
            lngProcessID = CLng(udtProcessInfo.ProcessID)
            ' Since pProcessName contains a pointer,call GetStringFromLP to get the
            ' variable length string it points to
            If udtProcessInfo.ProcessID = 0 Then
                'MsgBox "System Idle Process"
            Else
                strTempProcessName = GetStringFromLP(udtProcessInfo.pProcessName)
                If UCase(strTempProcessName) = UCase(strProcessName) Then
                    GetProcessID = lngProcessID
                End If
            End If
            p = p + LenB(udtProcessInfo)
        Next i
        WTSFreeMemory lpBuffer 'Free your memory buffer
    Else
        MsgBox "Error", vbCritical, "Fatal Error"
    End If
End Function

Private Function GetStringFromLP(ByVal StrPtr As Long) As String
    Dim b As Byte
    Dim tempStr As String
    Dim bufferStr As String
    Dim Done As Boolean
   
    Done = False
    Do
        ' Get the byte/character that StrPtr is pointing to.
        CopyMemory b, ByVal StrPtr, 1
        If b = 0 Then ' If you've found a null character, then you're done.
            Done = True
        Else
            tempStr = Chr$(b) ' Get the character For the byte's value
            bufferStr = bufferStr & tempStr 'Add it To the String
            StrPtr = StrPtr + 1 ' Increment the pointer To Next byte/char
        End If
    Loop Until Done
    GetStringFromLP = bufferStr
End Function

 

摘自:网络整理
  

VB相关


VB 释放资源文件到指定目录函数

VB 读取资源文件里面的字符串

VB中资源文件.res的使用方法详解

VB6.0中创建和使用文本资源文件

VB WindowsMediaPlayer 播放

vbWindowsMediaPlayer的常用属性和方法

VB Environ系统环境变量函数大全

VB 去除文本框粘贴功能

VB LISTBOX

VB 删除数组中的重复元素

VB数组快速排序算法

关于三个概念:ActiveXOLECOM

VB 获得磁盘的文件系统

VB中用API实现文件拖放

加密算法-MD5算法

VB中使用MD5算法

VB 全局热键HOOK (不占系统资源版本)

VB 小技巧自定义TextBox文本框右键菜单

VB 写下载者代码

VB 一行代码的诀窍

VBS教程-wscript对象

vb枚举进程

VB中如何让线程或进程在指定的CPU上运行

VB判断指定的WORD文档是否被打开

VB如何读取快捷方式的目标路径

VBAPI控制输入法状态

为系统加载右键注册控件选项【VB 注册控件】

VB如何根据窗口标题获得进程名称

VB快速查找大型文件中包含的字符串

VB实现可执行文件运行时自删除

VB 打开txt,bat,jpg 任意后缀程序

VB 写文件关联程序

VB 自启动建立右键菜单

VB 判断IP能否ping

VB FTP操作类(可上传、下载、创建文件夹等等)

VB部分文件汇总B

Vb 求素数最经典的方法也是最快的方法

vb用数组方式快速导出MSFlexGrid表格数据到Excel表格中

VBMsFlexGrid控件的使用细则

点击MSFlexGrid数据控件的标题进行数据排序

VB 获取鼠标坐标

VBNEW的用法(申请内存空间)

VB CreateObject函数

VB中的New CreateObject的区别

VB ListBox 添加不重复的值

VB 单击ListView控件某列表头进行排序

VB 简单实现简体与繁体互转

VB 阿拉伯数字转换为中文大写数值函数

VB 获取Textbox文本框中的行数函数


更多精彩>>>
  评论这张
 
阅读(918)| 评论(0)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018