Tuesday, December 13, 2011

Tuesday 12.13.11

Public Interface IAppFunctionality
Sub DoIt()
End Interface

_
Public NotInheritable Class CompanyInfoAttribute
Inherits System.Attribute

Private companyName As String
Private companyUrl As String

Public Sub New()
End Sub
Public Property Name() As String
Get
Return companyName
End Get
Set(ByVal value As String)
companyName = value
End Set
End Property

Public Property Url() As String
Get
Return companyUrl
End Get
Set(ByVal value As String)
companyUrl = value
End Set
End Property

End Class

Imports System.Windows.Forms
Imports CommonSnappableTypes


Module Module1

Sub Main()
Dim s As New VbNetSpanInModule()
s.DoIt()
End Sub

End Module

_
Public Class VbNetSpanInModule
Implements IAppFunctionality

Public Sub New()

End Sub

Public Sub DoIt() Implements CommonSnappableTypes.IAppFunctionality.DoIt
MessageBox.Show("You have just used the VB 2008 snap in!")
End Sub
End Class




Imports System.Reflection
Imports CommonSnappableTypes

Public Class Form1

Private Sub SnapInModuleToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SnapInModuleToolStripMenuItem.Click
'allow user to select an assemnly to load
Dim dlg As New OpenFileDialog()

If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then
If dlg.FileName.Contains("CommonSnappableTypes") Then
MessageBox.Show("CommonSnappableTypes has no snap-ins!")
ElseIf LoadExternalModule(dlg.FileName) = False Then
MessageBox.Show("nothing implements IAppFunctionality!")
End If
End If
End Sub

Private Function LoadExternalModule(ByVal path As String) As Boolean
Dim foundSnapin As Boolean = False
Dim itfAppFx As IAppFunctionality
Dim theSnapInAsm As Assembly = Nothing

'try to dynamically load the selected assembly
Try
theSnapInAsm = Assembly.LoadFrom(path)
Catch ex As Exception
MessageBox.Show(ex.Message)
Return foundSnapin
End Try

'get all IAppFunctionality compatible classes in assembly using a LINQ query and implicity typed data
Dim classTypes = From t In theSnapInAsm.GetTypes() Where t.IsClass And (t.GetInterface("IAppFunctionality") IsNot Nothing) Select t

For Each c As Object In classTypes
foundSnapin = True
'use late binding to create the type
Dim o As Object = theSnapInAsm.CreateInstance(c.FullName)

'call DoIt() off the interface
itfAppFx = CType(o, IAppFunctionality)
itfAppFx.DoIt()
lstLoadedSnapIns.items.add(c.FullName)

Next
Return foundSnapin


End Function


Module Module1

Sub Main()
ListAllRunningProcesses()
Console.ReadLine()

Dim theProc As Process
Try
theProc = Process.GetProcessById(987)
'manipulate the process handle...
Catch ex As Exception
Console.WriteLine("-> Sorry...bad PID!")
End Try
Console.ReadLine()
'Prompt user for a PID and print out the set of active threads
Console.WriteLine("***** Enter PID of process to investigate *****")
Console.Write("PID: ")
Dim pID As String = Console.ReadLine()
Try
Dim theProcID As Integer = Integer.Parse(pID)
EnumThreadsForPid(theProcID)
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Console.ReadLine()
Dim proc As Process() = Process.GetProcessesByName("ListAllProcesses")
For Each p In proc
Console.WriteLine("Proc name is {0}", p.ProcessName)
Console.WriteLine("Proc id is {0}", p.Id)
EnumModsForPid(p.Id)
Next
StartAndKillProcess()

End Sub


Public Sub ListAllRunningProcesses()
'get all the processes on the local machine
Dim runningProcs As Process() = Process.GetProcesses(".")

'print out each PID and name of each process
For Each p As Process In runningProcs
Dim info As String = String.Format("-> PID: {0}" & Chr(9) & "Name: {1}", p.Id, p.ProcessName)
Console.WriteLine(info)
Next
Console.WriteLine("*****************************")
Console.WriteLine()
End Sub

Public Sub EnumThreadsForPid(ByVal pID As Integer)
Dim theProc As Process
Try
theProc = Process.GetProcessById(pID)
Catch ex As Exception
Console.WriteLine("-> Sorry...bad PID!")
Console.WriteLine("***********************")
Console.WriteLine()
Return
End Try
Console.WriteLine("Here are the threads used by: {0}", theProc.ProcessName)

'list out stats for each thread in the specified process
Dim theThreads As ProcessThreadCollection = theProc.Threads
For Each pt As ProcessThread In theThreads
Dim info As String = String.Format("-> Thread ID: {0}" & Chr(9) & "Start Time {1}" & _
Chr(9) & "Priority {2}", _
pt.Id, pt.StartTime.ToShortTimeString(), pt.PriorityLevel)
Console.WriteLine(info)
Next
Console.WriteLine("*************************")
Console.WriteLine()
End Sub

Public Sub EnumModsForPid(ByVal pID As Integer)
Dim theProc As Process
Try
theProc = Process.GetProcessById(pID)

Catch ex As Exception
Console.WriteLine("->Sorry...bad PID!")
Console.WriteLine("********************")
Console.WriteLine()
Return
End Try
Console.WriteLine("Here are the loaded modules for: {0}", theProc.ProcessName)
Try
Dim theMods As ProcessModuleCollection = theProc.Modules
For Each pm As ProcessModule In theMods
Dim info As String = String.Format("-> Mod Name: {0}", pm.ModuleName)
Console.WriteLine(info)
Next
Console.WriteLine("******************")
Console.WriteLine()
Catch ex As Exception
Console.WriteLine("No mods!")
End Try
End Sub

Public Sub StartAndKillProcess()
'launch internet explorer
Console.WriteLine("--> Hit a key to launch IE")
Console.ReadLine()
Dim ieProc As Process = Process.Start("IExplorer.exe", "www.intertech.com")
Console.WriteLine("--> Hit a key to kill {0}...", ieProc.ProcessName)
Console.ReadLine()
'kill the iexplorer.exe process
Try
ieProc.Kill()
Catch 'in case the user already killed it...
End Try
End Sub
End Module

No comments: