2022年5月
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
無料ブログはココログ

« ショートカットに相対パスを指定する。(その3) | トップページ | Run Applications at a Low Integrity Level (その2) »

2010年3月18日 (木)

引数を「ファイル名を指定して実行」に「送る」。

FileRunX.exe [引数...]

用途
(a) SendToフォルダに置いて、ファイルを「送る」。
(b) ショートカットなどのコマンドラインに前置して、実行前に確認ダイアログを出す。
(c) ショートカットなどのコマンドラインに前置して、実行時に引数の追加変更ダイアログを出す。

vbc /t:winexe FileRunX.vb

Imports System.Text
Imports System.Threading

Delegate Function CallBack(hwnd As Integer, lParam As Integer) As Boolean

Module Module1

Declare Auto Function SHRunDialog Lib "shell32" Alias "#61" (hWnd As Integer, hIcon As Integer, lpszPath As String, szTitle As String, szPrompt As String, uFlags As Integer) As Integer
Declare Function EnumWindows Lib "user32" (lpEnumFunc As CallBack, IParam As Integer) As Integer
Declare Function GetWindowThreadProcessId Lib "user32" (hwnd As Integer, ByRef ProcessId As Integer) As Integer
Declare Auto Function GetClassName Lib "user32" (hwnd As Integer, lpClassName As StringBuilder, nMaxCount As Integer) As Integer
Declare Auto Function FindWindowEx Lib "user32" (hWndParent As Integer, hWndChildAfter As Integer, lpszClassName As String, lpszWindowCaption As Integer) As Integer
Declare Auto Function SendMessage Lib "user32" (hwnd As Integer, wMsg As Integer, wParm As Integer, lParm As String) As Integer
Const WM_SETTEXT As Integer=&H000C
Dim hwnd As Integer

Sub Main()
Dim FileRunThread As New Thread(AddressOf FileRun)
FileRunThread.IsBackground=True
FileRunThread.Start()
Dim sTitle As String = "ファイル名を指定して実行" & Chr(0)
Dim sPrompt As String = "実行するプログラム名、または開くフォルダやドキュメント名、インターネット リソース名を入力してください。" & Chr(0)
SHRunDialog(0, 0, Environment.CurrentDirectory, sTitle, sPrompt, 0)
End Sub

Sub FileRun()
Dim CommandLine As String = System.Environment.CommandLine
Dim Quoted As Boolean = False
Dim k As Integer
For k=0 To CommandLine.Length-1
  If CommandLine.Chars(k) = """" Then
    Quoted = Not Quoted
  ElseIf Not Quoted AndAlso CommandLine.Chars(k) = " " Then
    Exit For
  End If
Next
CommandLine = CommandLine.Remove(0,k).Trim()
'  Console.WriteLine("Arguments:{0}",CommandLine)

If CommandLine.Length=0 Then Exit Sub
System.Threading.Thread.Sleep(0)
Do
  hwnd=0
  EnumWindows(AddressOf EnumFunc, System.Diagnostics.Process.GetCurrentProcess().Id)
  If hwnd Then
    hwnd = FindWindowEx(hwnd, 0, "ComboBox", 0)
    If hwnd Then
      SendMessage(hwnd, WM_SETTEXT, 0, CommandLine)
      Exit Sub
    End If
  End If
  System.Threading.Thread.Sleep(100)
Loop
End Sub

Function EnumFunc(hwnd1 As Integer, lParam As Integer) As Boolean
Dim pid As Integer
GetWindowThreadProcessId(hwnd1, pid)
Dim sb1 As New StringBuilder(256)
GetClassName(hwnd1, sb1, sb1.Capacity + 1)
If lParam=pid And sb1.ToString()="#32770" Then
  hwnd=hwnd1
  Return False
End If
Return True
End Function

End Module

« ショートカットに相対パスを指定する。(その3) | トップページ | Run Applications at a Low Integrity Level (その2) »