Results 1 to 7 of 7

Thread: [2005] how to terminate a background process

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2008
    Posts
    15

    [2005] how to terminate a background process

    hi there,
    could somebody post some useful code for me or reference page?

    the closest that i could find was http://forums.microsoft.com/MSDN/Sho...08309&SiteID=1

    but the full code is not there....and it is still unsolved as yet...


    Process.GetProcessesByName is not available in the compact framework. what are the alternatives?

  2. #2
    Frenzied Member
    Join Date
    Oct 2005
    Posts
    1,286

    Re: [2005] how to terminate a background process

    Hi,
    try this - Look under 'components' for toolhelpce

    Pete
    Pete Vickers
    MVP - Device Application Development
    http://www.gui-innovations.com http://mobileworld.appamundi.com/blogs/

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2008
    Posts
    15

    Re: [2005] how to terminate a background process

    Quote Originally Posted by petevick
    Hi,
    try this - Look under 'components' for toolhelpce

    Pete
    thanks for the tip. is the library usable for the window mobile 6 pro?

    and i stil dun hv any idea how to use it? can show me some tip off code?

    sory for my newbieness.

  4. #4
    Frenzied Member
    Join Date
    Oct 2005
    Posts
    1,286

    Re: [2005] how to terminate a background process

    Hi,
    don't see any reason why it shouldn't work.
    Similar to
    Code:
    Dim refProcColl As ProcessCollection = Process.CEProcessList
    If (refProcColl Is Nothing) Then
    Else
        For Each refProcess As Process In refProcColl
            ' work with the properties and methods to get the process details
        Next
    End If
    There is an XML help file in the package
    Pete Vickers
    MVP - Device Application Development
    http://www.gui-innovations.com http://mobileworld.appamundi.com/blogs/

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2008
    Posts
    15

    Re: [2005] how to terminate a background process

    Quote Originally Posted by petevick
    Hi,
    don't see any reason why it shouldn't work.
    Similar to
    Code:
    Dim refProcColl As ProcessCollection = Process.CEProcessList
    If (refProcColl Is Nothing) Then
    Else
        For Each refProcess As Process In refProcColl
            ' work with the properties and methods to get the process details
        Next
    End If
    There is an XML help file in the package
    just as i expected. it does not work on the window mobile 6. damn!
    refProcColl is empty/nothing...

    i was already damn happy when it work on the emulator....damn damn....

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2008
    Posts
    15

    Re: [2005] how to terminate a background process

    anyway, that dont stop me from doing some googling

    i found this
    http://msdn2.microsoft.com/en-us/library/aa446560.aspx

    but the code are c#. hey i dont know c# too. so basically i ve done some conversion to vb.net. so dont be mad if i ve done it wrong because i m stil a amateur coder. hey stil learning....

    basically there is some syntax error(MARK IN RED) that i dont understand here. i hope somebody is here to help me understand..thanks in advance.

    Code:
    Imports system
    'Imports System.Diagnostics
    Imports System.Runtime.InteropServices
    Imports System.Text.Encoding
    
    
    Public Class Process
    
        <CLSCompliant(False)> _
        Public TH32CS_SNAPPROCESS As UInteger = 2
    
        Private Const PROCESS_TERMINATE As Integer = 1
        Private Const INVALID_HANDLE_VALUE As Integer = -1
    
        <CLSCompliant(False)> _
        Public Declare Function CreateToolhelp32Snapshot Lib "toolhelp.dll" _
            (ByVal flags As UInteger, ByVal processid As UInteger) As IntPtr
    
        Public Declare Function CloseToolhelp32Snapshot Lib "toolhelp.dll" _
        (ByVal handle As IntPtr) As Integer
    
        Public Declare Function Process32First Lib "toolhelp.dll" _
        (ByVal handle As IntPtr, ByVal pe() As Byte) As Integer
    
        Public Declare Function Process32Next Lib "toolhelp.dll" _
        (ByVal handle As IntPtr, ByVal pe() As Byte) As Integer
    
        Private Declare Function OpenProcess Lib "coredll.dll" _
        (ByVal flags As Integer, ByVal fInherit As Boolean, ByVal PID As Integer) As IntPtr
    
        Private Declare Function TerminateProcess Lib "coredll.dll" _
        (ByVal hProcess As IntPtr, ByVal ExitCode As UInteger) As Boolean
    
        Private Declare Function CloseHandle Lib "coredll.dll" _
        (ByVal handle As IntPtr) As Boolean
    
        Private Class PROCESSENTRY32
    
            ' constants for structure definition
            Private Const SizeOffset As Integer = 0
            Private Const UsageOffset As Integer = 4
            Private Const ProcessIDOffset As Integer = 8
            Private Const DefaultHeapIDOffset As Integer = 12
            Private Const ModuleIDOffset As Integer = 16
            Private Const ThreadsOffset As Integer = 20
            Private Const ParentProcessIDOffset As Integer = 24
            Private Const PriClassBaseOffset As Integer = 28
            Private Const dwFlagsOffset As Integer = 32
            Private Const ExeFileOffset As Integer = 36
            Private Const MemoryBaseOffset As Integer = 556
            Private Const AccessKeyOffset As Integer = 560
            Private Const Size As Integer = 564
    
            ' the whole size of the structure
            Private Const MAX_PATH As Integer = 260
    
            Dim dwSize As UInteger
            Dim cntUsage As UInteger
            Dim th32ProcessID As UInteger
            Dim th32DefaultHeapID As UInteger
            Dim th32ModuleID As UInteger
            Dim cntThreads As UInteger
            Dim th32ParentProcessID As UInteger
            Dim pcPriClassBase As Integer
            Dim dwFlags As UInteger
            Dim szExeFile As String
            Dim th32MemoryBase As UInteger
            Dim th32AccessKey As UInteger
    
            ' utility:  get a uint from the byte array
            Private Shared Function GetUInt(ByVal aData() As Byte, ByVal Offset As Integer) As UInteger
                Return BitConverter.ToUInt32(aData, Offset)
            End Function
    
            ' utility:  set a uint into the byte array
            Private Shared Sub SetUInt(ByVal aData() As Byte, ByVal Offset As Integer, ByVal Value As Integer)
                Dim buint() As Byte = BitConverter.GetBytes(Value)
                Buffer.BlockCopy(buint, 0, aData, Offset, buint.Length)
            End Sub
    
            ' utility:  get a ushort from the byte array
            Private Shared Function GetUShort(ByVal aData() As Byte, ByVal Offset As Integer) As System.UInt16
                Return BitConverter.ToUInt16(aData, Offset)
            End Function
    
            ' utility:  set a ushort int the byte array
            Private Shared Sub SetUShort(ByVal aData() As Byte, ByVal Offset As Integer, ByVal Value As Integer)
                Dim bushort() As Byte = BitConverter.GetBytes(CType(Value, Short))
                Buffer.BlockCopy(bushort, 0, aData, Offset, bushort.Length)
            End Sub
    
            ' utility:  get a unicode string from the byte array
            Private Shared Function GetString(ByVal aData() As Byte, ByVal Offset As Integer, ByVal Length As Integer) As String
                Dim sReturn As String = System.Text.Encoding.Unicode.GetString(aData, Offset, Length)
                Return sReturn
            End Function
    
            ' utility:  set a unicode string in the byte array
            Private Shared Sub SetString(ByVal aData() As Byte, ByVal Offset As Integer, ByVal Value As String)
                Dim arr() As Byte = System.Text.Encoding.ASCII.GetBytes(Value)
                Buffer.BlockCopy(arr, 0, aData, Offset, arr.Length)
            End Sub
    
            Public Function ToByteArray() As Byte()
                Dim aData() As Byte
                aData = New Byte((Size) - 1) {}
                'set the Size member
                SetUInt(aData, SizeOffset, Size)
                Return aData
            End Function
    
            ' create a PROCESSENTRY instance based on a byte array      
            Public Sub New(ByVal aData() As Byte)
                MyBase.New()
                dwSize = GetUInt(aData, SizeOffset)
                cntUsage = GetUInt(aData, UsageOffset)
                th32ProcessID = GetUInt(aData, ProcessIDOffset)
                th32DefaultHeapID = GetUInt(aData, DefaultHeapIDOffset)
                th32ModuleID = GetUInt(aData, ModuleIDOffset)
                cntThreads = GetUInt(aData, ThreadsOffset)
                th32ParentProcessID = GetUInt(aData, ParentProcessIDOffset)
                pcPriClassBase = CType(GetUInt(aData, PriClassBaseOffset), Long)
                dwFlags = GetUInt(aData, dwFlagsOffset)
                szExeFile = GetString(aData, ExeFileOffset, MAX_PATH)
                th32MemoryBase = GetUInt(aData, MemoryBaseOffset)
                th32AccessKey = GetUInt(aData, AccessKeyOffset)
            End Sub
    
            Public ReadOnly Property Name() As String
                Get
                    Return szExeFile.Substring(0, szExeFile.IndexOf(Microsoft.VisualBasic.ChrW(92)))
                End Get
            End Property
    
            Public ReadOnly Property PID() As System.UInt64
                Get
                    Return th32ProcessID
                End Get
            End Property
    
            Public ReadOnly Property BaseAddress() As System.UInt64
                Get
                    Return th32MemoryBase
                End Get
            End Property
    
            Public ReadOnly Property ThreadCount() As System.UInt64
                Get
                    Return cntThreads
                End Get
            End Property
    
        End Class
    
        Private processName As String
        Private handle As IntPtr
        Private threadCount As Integer
        Private baseAddress As Integer
    
        'default constructor
        Public Sub New()
            MyBase.New()
    
        End Sub
    
        'private helper constructor
        Private Sub New(ByVal id As IntPtr, ByVal procname As String, ByVal threadcount As Integer, ByVal baseaddress As Integer)
            MyBase.New()
            handle = id
            processName = procname
            threadcount = threadcount
            baseaddress = baseaddress
        End Sub
    
        Public Shared Function GetProcesses() As Process()
            'Dim TH32CS_SNAPPROCESS As UInteger = 2
    
            'temp ArrayList
            Dim procList As ArrayList = New ArrayList
            Dim handle As IntPtr = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) ''''''ok, i get an error :cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class
            If (CType(handle, Integer) > 0) Then
                Try
                    Dim peCurrent As PROCESSENTRY32
                    Dim pe32 As PROCESSENTRY32 '= New PROCESSENTRY32
                    'Get byte array to pass to the API calls
                    Dim peBytes() As Byte = pe32.ToByteArray()
                    'Get the first process
                    Dim retval As Integer = Process32First(handle, peBytes)
    
                    While (retval = 1)
                        'Convert bytes to the class
                        peCurrent = New PROCESSENTRY32(peBytes)
                        'New instance of the Process class
                        Dim proc As Process = New Process(New IntPtr(CType(peCurrent.PID, Integer)), peCurrent.Name, CType(peCurrent.ThreadCount, Integer), CType(peCurrent.BaseAddress, Integer))
                        procList.Add(proc)
                        retval = Process32Next(handle, peBytes)
    
                    End While
                Catch ex As Exception
                    Throw New Exception(("Exception: " + ex.Message))
                End Try
                'Close handle
                CloseToolhelp32Snapshot(handle)
                Return CType(procList.ToArray(GetType(Process)), Process())
            Else
                Throw New Exception("Unable to create snapshot")
            End If
        End Function
    
    cont.....

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2008
    Posts
    15

    Re: [2005] how to terminate a background process

    Code:
    Public ReadOnly Property BaseAddress() As Integer '''another error: 'BaseAddress' is already declared as 'Private Dim baseAddress as Integer' in this class
            Get
                Return BaseAddress
            End Get
        End Property
    
        Public ReadOnly Property ThreadCount() As Integer
            Get
                Return ThreadCount
            End Get
        End Property
    
        Public ReadOnly Property Handle() As IntPtr
            Get
                Return Handle
            End Get
        End Property
    
        Public ReadOnly Property ProcessName() As String
            Get
                Return ProcessName
            End Get
        End Property
    
        Public ReadOnly Property BaseAddess() As Integer
            Get
                Return baseAddress
            End Get
        End Property
    
        'ToString implementation for ListBox binding
        Public Overrides Function ToString() As String
            Return processName
        End Function
    End Class
    and there is a few error on the property

    you can copy and paste on ur vs, i m sure u ll c the error instantly.....thanks for any advice....
    if u hv better alternative please let me know too...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width