在一个结构体对szExeFile定义如下:Private Type PROCESSENTRY32
    dwSize As Long
    cntUsage As Long
    th32ProcessID As Long
    th32DefaultHeapID As Long
    th32ModuleID As Long
    cntThreads As Long
    th32ParentProcessID As Long
    pcPriClassBase As Long
    dwFlags As Long
    szExeFile As String * 1024
End Type
下面是部分关键代码
Function GetProcID(ByVal ExeFile As String) As LongDim Pro As PROCESSENTRY32
Dim HSnapshot As LongGetProcID = 0HSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If HSnapshot Then    Pro.dwSize = Len(Pro)
    
    If (Process32First(HSnapshot, Pro)) Then
    
        Do
                 
            If InStr(Pro.szExeFile, ExeFile) <> 0 Then
            MsgBox Pro.szExeFile
             GetProcID = Pro.th32ProcessID
             Exit Function
            End If
            
            
        Loop Until (Process32Next(HSnapshot, Pro) < 1)
    End If
    CloseHandle (HSnapshot)
End IfEnd Function
'-----------------------------------
为什么我这一句总是不正确,我用MsgBox 可以看到 Pro.szExeFile中的字符串中含有指定的名称,如:QQ.exe,但是用InStr(Pro.szExeFile, ExeFile)这个函数测试,始终为0,请问是什么原因,我应该如何进行比较,谢谢!

解决方案 »

  1.   

    你这涉及两个问题:
    1、Pro.szExeFile为1024个空格格式化的定长串,CreateToolhelp32Snapshot返回这个值时会在文件名尾部加上一个Chr(0),你可用此截出文件名:
    s=left(Pro.szExeFile,instr(1,Pro.szExeFile,chr(0))-1)
    s才为正确的文件名。
    2、InStr(Pro.szExeFile, ExeFile)这句可能是因为文件名大小写不同所至,你应该加文本比较参数,正确写法:
    If InStr(1,Pro.szExeFile, ExeFile, vbTextCompare) <> 0 Then