简体   繁体   English

如何在 delphi 中运行 cmd 命令?

[英]How can I run a cmd command in delphi?

I'm trying to run a cmd command in delphi and have tried我正在尝试在 delphi 中运行 cmd 命令并尝试过

ShellExecute(Handle, 'runas', 'cmd.exe', ' net start ExportFileMainService', nil, SW_SHOWNORMAL);

but it doesn't seem to be working and I'm not sure why.但它似乎没有工作,我不知道为什么。

Here is some code I wrote in Delphi 2010 to open a command prompt:这是我在 Delphi 2010 中编写的一些代码,用于打开命令提示符:

function TForm7.RunCmdAndWait(hWnd: HWND; aParameters: string):
    Cardinal;
var
  sei: TShellExecuteInfo;
  aFile, dir: string;
begin
  Result := 0;
  dir := ExtractFilePath(Application.ExeName);
  SetCurrentDir(dir);
  FillChar(sei, SizeOf(sei), 0);
  sei.cbSize := SizeOf(sei);
  sei.Wnd := hWnd;
  sei.fMask := {SEE_MASK_FLAG_NO_UI or} SEE_MASK_NOCLOSEPROCESS;
  sei.lpVerb := 'open';
  aFile := 'cmd';
  sei.lpFile := PChar(aFile);
  sei.lpParameters := PChar(aParameters);
  sei.lpDirectory := PChar(dir);
  sei.nShow := SW_SHOWNORMAL;

  if not ShellExecuteEx(@sei) then
  begin
    RaiseLastOSError;
  end;
  if sei.hProcess <> 0 then
  begin
    while WaitForSingleObject(sei.hProcess, 250) = WAIT_TIMEOUT do
      Application.ProcessMessages;
    GetExitCodeProcess(sei.hProcess, Result);
    CloseHandle(sei.hProcess);
  end else
    TaskMessageDlg('Error running program!', 'No handle to process.',
      mtError, [mbOK], 0);
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM