当前位置:编程学习 > VB >>

初学VB.Net,写了个小程序,请大虾们帮忙看看

初学VB.Net,写了个小程序,请大虾们帮忙看看,为何执行到If (Process32First(l, PE32)) <> 0 Then '遍历第一个进程
这一行报错:Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

完整代码如下:
Public Class FMain
    Public Structure processentry32
        Public dwsize As Long
        Public cntusage As Long
        Public th32processid As Long
        Public th32defaultheapid As Long
        Public th32moduleid As Long
        Public cntthreads As Long
        Public th32parentprocessid As Long
        Public pcpriclassbase As Long
        Public dwflags As Long
        Public szexefile As String
    End Structure

    Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
    Private Declare Function CloseToolhelp32Snapshot Lib "kernel32" (ByVal hObject As Long) As Boolean
    'Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, ByRef lppe As processentry32) As Long

    <DllImport("KERNEL32.DLL", EntryPoint:="Process32First", SetLastError:=True, _
    CharSet:=CharSet.Unicode, ExactSpelling:=True, _
    CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function Process32First(ByVal hSnapshot As Long, ByRef lppe As processentry32) As Long

    End Function
    Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, ByVal lppe As processentry32) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long
    Private Declare Function TerminateProcess Lib "kernel32" (ByVal ApphProcess As Long, ByVal uExitCode As Long) As Long
    Private Declare Sub ExitProcess Lib "Kernel32" (ByVal ProcessID As Long)

    Const TH32CS_SNAPHEAPLIST = &H1
    Const TH32CS_SNAPPROCESS = &H2
    Const TH32CS_SNAPTHREAD = &H4
    Const TH32CS_SNAPMODULE = &H8
    Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
    Const TH32CS_INHERIT = &H80000000

    '获得当前所有的系统进程 
    Public Function GetAllProcess() As ArrayList
        Dim PE32 As processentry32
        Dim l As Long
        Dim l1 As Long
        Dim flag As Boolean
        Dim mName As String
        Dim i As Integer
        Dim PID As Long

        GetAllProcess = Nothing
        PE32 = Nothing

        l = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)

        If l Then
            PE32.dwsize = Len(PE32)
            If (Process32First(l, PE32)) <> 0 Then '遍历第一个进程
                Do
                    i = InStr(1, PE32.szexefile, Chr(0))
                    mName = LCase(Microsoft.VisualBasic.Left(PE32.szexefile, i - 1))
                    'If mName = "Word.exe" Then
                    PID = PE32.th32processid
                    mName = PE32.szexefile
                    GetAllProcess.Add(mName + ":" + PID)
                    'Dim mProcID As Long
                    'mProcID = OpenProcess(1&, -1&, PID)
                    'TerminateProcess(mProcID, 0&)
                    flag = True
                    'Else
                    'flag = False
                    'End If
                Loop Until (Process32Next(l, PE32) < 1) '遍历所有进程知道返回值为False
            End If
            l1 = CloseHandle(l)
        End If
        Return GetAllProcess()
    End Function

    Public Sub ShowAllProcess()
        Dim AllPE As ArrayList
        AllPE = GetAllProcess()
        If AllPE.Count > 0 Then
            LV_Process.Clear()
            Dim PECount As Integer
            PECount = 0
            While PECount < AllPE.Count
                LV_Process.Items.Add("Word:" + Str(AllPE.Item(PECount)))
                PECount = PECount + 1
            End While
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnQuit.Click
        'Me.Close()
        ExitProcess(0)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        ShowAllProcess()
    End Sub
End Class
--------------------编程问答-------------------- 没人回复啊,自己顶下 --------------------编程问答-------------------- 杯具了。。。。。。 --------------------编程问答-------------------- 不急不急:)我来问一下,你是在vs2008下测试的吗 --------------------编程问答-------------------- to:bcrun 谢谢回复
我是在VS 2005下测试的,我发现l = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
这一句,返回值特别的大,达到8位以上长度,不知道是什么问题,盼高手回复啊,小虾谢过了先。 --------------------编程问答-------------------- http://technet.microsoft.com/en-us/sysinternals/bb545027.aspx
用shell函数调用pslist工具吧
shell("pslist.exe >pslist.txt")
然后读文件pslist.txt的内容
补充:VB ,  API
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,