Imports System.Diagnostics
Imports System.Threading
Module Module1
Sub Main()
Dim args() As String
args = System.Environment.GetCommandLineArgs()
If Not args.Length >= 2 Then
MsgBox("引数の数が異常です")
Return
End If
'第1引数はコマンド
Dim cmd As String = args(1).Trim
cmd = cmd.ToLower
Select Case cmd
Case "-key"
SendKey(args)
Return
Case "-active"
DoActiveByProcessName(args)
Return
Case "-activepid"
DoActiveByProcessId(args)
Return
Case "-pause"
Pause(args)
Return
Case Else
MsgBox("引数が不正です")
End Select
End Sub
'memo. http://msdn.microsoft.com/ja-jp/library/system.windows.forms.sendkeys(v=vs.110).aspx
Private Sub SendKey(ByVal args() As String)
If Not args.Length = 3 Then
MsgBox("引数の数が異常です")
Return
End If
'第2引数は送信するキー
Dim key As String = args(2)
My.Computer.Keyboard.SendKeys(key, True)
End Sub
Private Sub DoActiveByProcessName(ByVal args() As String)
If Not args.Length = 3 Then
MsgBox("引数の数が異常です")
Return
End If
'第2引数はプロセス名
Dim processName As String = args(2).Trim
Dim ps As Process() = Process.GetProcessesByName(processName)
Select Case True
Case ps.Length = 1
Dim pid As Integer = ps(0).Id
AppActivate(pid)
Return
Case ps.Length > 1
MsgBox("該当するプロセスが複数あります")
Return
End Select
MsgBox("指定された名前のプロセスが見つかりませんでした")
End Sub
Private Sub DoActiveByProcessId(ByVal args() As String)
If Not args.Length = 3 Then
MsgBox("引数の数が異常です")
Return
End If
'第2引数はプロセスID
Dim processIdStr As String = args(2).Trim
Dim processId As Integer
If Integer.TryParse(processIdStr, processId) Then
Try
processId = Process.GetProcessById(processId).Id
AppActivate(processId)
Catch ex As Exception
MsgBox("指定されたPIDをアクティブにできませんでした")
End Try
Return
Else
MsgBox("PIDは整数値で指定してください")
End If
Return
End Sub
Private Sub Pause(ByVal args() As String)
If args.Length = 2 Or args.Length = 3 Then
Else
MsgBox("引数の数が異常です")
Return
End If
If args.Length = 3 Then
'第2引数はスリープする時間
Dim sleepStr As String = args(2)
Dim sleepMilliSecond As Integer
'第2引数を解析してみる
If Integer.TryParse(sleepStr, sleepMilliSecond) Then
Thread.Sleep(sleepMilliSecond)
Else
Thread.Sleep(500) '解析出来なかった場合
End If
End If
'第3引数が指定されていない場合
Thread.Sleep(300)
End Sub
End Module
最終更新:2013年11月08日 03:15