打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
用 Delphi 编写一个控制台程序来执行 Windows 命令行程序
userphoto

2023.07.21 福建

关注

好的,下面是使用 Delphi 编写一个控制台程序来执行 Windows 命令行程序的例子:

```delphi

program ExecuteCommand;

{$APPTYPE CONSOLE}

uses

  SysUtils, Windows;

procedure ExecuteWindowsCommand(const CommandLine: string);

var

  StartupInfo: TStartupInfo;

  ProcessInfo: TProcessInformation;

  CommandLineAsPointer: PChar;

begin

  ZeroMemory(@StartupInfo, SizeOf(TStartupInfo));

  StartupInfo.cb := SizeOf(TStartupInfo);

  CommandLineAsPointer := PChar(CommandLine);

  if not CreateProcess(nil, CommandLineAsPointer, nil, nil, False,

    CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo) then

  begin

    Writeln('Failed to execute Windows command: ', CommandLine);

    Exit;

  end;

  WaitForSingleObject(ProcessInfo.hProcess, INFINITE);

  CloseHandle(ProcessInfo.hProcess);

  CloseHandle(ProcessInfo.hThread);

end;

begin

  try

    // 调用示例,执行系统命令

    ExecuteWindowsCommand('ping www.google.com');

    // 调用示例,执行自定义命令行程序

    ExecuteWindowsCommand('path_to_your_command_line_program.exe parameters');

  except

    on E: Exception do

      Writeln('Error: ', E.ClassName, ' : ', E.Message);

  end;

end.

```

请将 "path_to_your_command_line_program.exe" 替换为你要执行的命令行程序的路径,

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
程序自杀
Windows下如何让非NT程序像NT程序一样毫无踪迹在后台运行?
delphi 如何知道bat运行完毕
一个等待Winexec执行完成后再继续执行后面代码的函数
Delphi 利用 CreateProcess 读取 CMD 程序的显示信息和返回结果
windows 下的自动更新
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服