Delphi XE2およびそれ以前のバージョンではWindows.pasにBELOW_NORMAL_PRIORITY_CLASSとABOVE_NORMAL_PRIORITY_CLASSが定義されていないので、まずこれらを定義します。
{$IF RTLVersion < 24} const BELOW_NORMAL_PRIORITY_CLASS = $00004000; {$EXTERNALSYM ABOVE_NORMAL_PRIORITY_CLASS} ABOVE_NORMAL_PRIORITY_CLASS = $00008000; {$EXTERNALSYM ABOVE_NORMAL_PRIORITY_CLASS} {$IFEND}フォームにEditとComboBox、Buttonをひとつずつ配置し、フォームのOnCreateイベントでEditとComboBoxに値を格納します。
procedure TForm1.FormCreate(Sender: TObject); begin Edit1.Text := '%windir%\notepad.exe'; with ComboBox1.Items do begin BeginUpdate; try Clear; AddObject(Format('%s (0x%8.8X)',['IDLE',IDLE_PRIORITY_CLASS]), TObject(IDLE_PRIORITY_CLASS)); AddObject(Format('%s (0x%8.8X)',['BELOW_NORMAL',BELOW_NORMAL_PRIORITY_CLASS]), TObject(BELOW_NORMAL_PRIORITY_CLASS)); AddObject(Format('%s (0x%8.8X)',['NORMAL',NORMAL_PRIORITY_CLASS]), TObject(NORMAL_PRIORITY_CLASS)); AddObject(Format('%s (0x%8.8X)',['ABOVE_NORMAL',ABOVE_NORMAL_PRIORITY_CLASS]), TObject(ABOVE_NORMAL_PRIORITY_CLASS)); AddObject(Format('%s (0x%8.8X)',['HIGH',HIGH_PRIORITY_CLASS]), TObject(HIGH_PRIORITY_CLASS)); AddObject(Format('%s (0x%8.8X)',['REALTIME',REALTIME_PRIORITY_CLASS]), TObject(REALTIME_PRIORITY_CLASS)); finally EndUpdate; end; end; with ComboBox1 do begin ItemIndex := Items.IndexOfObject(TObject(NORMAL_PRIORITY_CLASS)); end; end;優先順位を指定してプロセスを起動します。
{$WARN SYMBOL_PLATFORM OFF} procedure TForm1.Button1Click(Sender: TObject); var ApplicationName: String; CreationFlags: DWORD; StartupInfo: TStartupInfo; ProcessInformation: TProcessInformation; Length: Integer; begin Length := ExpandEnvironmentStrings(PChar(Edit1.Text),nil,0); SetLength(ApplicationName,Length); ExpandEnvironmentStrings(PChar(Edit1.Text),PChar(ApplicationName),Length); UniqueString(ApplicationName); with ComboBox1 do begin if ItemIndex < 0 then begin Exit; end; CreationFlags := DWORD(Items.Objects[ItemIndex]); end; FillChar(StartupInfo,SizeOf(StartupInfo),0); StartupInfo.cb := SizeOf(StartupInfo); FillChar(ProcessInformation,SizeOf(ProcessInformation),0); Win32Check(CreateProcess(PChar(ApplicationName),nil,nil,nil,False, CreationFlags,nil,nil, StartupInfo,ProcessInformation)); CloseHandle(ProcessInformation.hProcess); CloseHandle(ProcessInformation.hThread); end;ここではEditに入力された起動対象プログラムに%windir%などの環境変数を使用することを前提としているため、Win32APIのExpandEnvironmentStrings関数 (en)で展開しています。
→優先順位クラスを指定してプロセスを起動する (Gist)
0 件のコメント:
コメントを投稿