1

我试图在 Win 7 上从 Delphi XE2 停止 mysql。我已经根据我在网上找到的 Delphi 7 编写了一些代码,应该这样做,但它不起作用:

function StopMySQL: boolean;
const
  UserName = 'Mark';
  Domain = 'PC-Mark';
  Command = 'net stop mysql';
var
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
begin
  FillChar(StartupInfo, SizeOf(StartupInfo), #0);
  StartupInfo.cb := SizeOf(StartupInfo);
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
  StartupInfo.wShowWindow := SW_HIDE;
  result := CreateProcessWithLogonW(Username, Domain, Password, 0, nil, Command, 0,     nil, nil, StartupInfo, ProcessInfo);
  if result then
    WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
end;

有谁知道如何做到这一点?

TIA 马克帕特森

4

2 回答 2

6

从 Vista 开始,您将需要提升的访问权限来启动/停止服务。
一种简单的方法是将 RunAs 与 Shellexecute(Ex) 一起使用。

ShellExecute(handle,'RunAs','net','stop mysql',nil,sw_Show);

更好的方法是使用清单,它可能如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <dependency>
    <dependentAssembly>
      <assemblyIdentity
        type="win32"
        name="Microsoft.Windows.Common-Controls"
        version="6.0.0.0"
        publicKeyToken="6595b64144ccf1df"
        language="*"
        processorArchitecture="*"/>
    </dependentAssembly>
  </dependency>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
       <requestedExecutionLevel level="requireAdministrator" />
      </requestedPrivileges> 
    </security>
  </trustInfo>
</assembly>

一个不需要“网络”的最小解决方案可能看起来像这样,应该做更多的事情,例如。测试服务是否正在运行,但那是你的,可以用 WinSvc 来完成:

implementation
uses WinSvc;
{$R *.dfm}
{$R administrator.res}

function ServiceStop(Machine, ServiceName: string): Boolean;

var
  ServiceControlManager, ServiceHandle: SC_Handle;
  ServiceStatus: TServiceStatus;
  dwCheckPoint: DWORD;
begin
  ServiceControlManager := OpenSCManager(PChar(Machine), nil, SC_MANAGER_CONNECT);
  if ServiceControlManager > 0 then
  begin
    ServiceHandle := OpenService(ServiceControlManager, PChar(ServiceName),
      SERVICE_STOP or SERVICE_QUERY_STATUS);
    if ServiceHandle > 0 then
    begin
      if (ControlService(ServiceHandle, SERVICE_CONTROL_STOP, ServiceStatus)) then
      begin
        if (QueryServiceStatus(ServiceHandle, ServiceStatus)) then
        begin
          while (SERVICE_STOPPED <> ServiceStatus.dwCurrentState) do
          begin
            dwCheckPoint := ServiceStatus.dwCheckPoint;
            Sleep(ServiceStatus.dwWaitHint);
            if (not QueryServiceStatus(ServiceHandle, ServiceStatus)) then
              break;
            if (ServiceStatus.dwCheckPoint < dwCheckPoint) then
              break;
          end;
        end;
      end;
      CloseServiceHandle(ServiceHandle);
    end;
    CloseServiceHandle(ServiceControlManager);
  end;

  Result := (SERVICE_STOPPED = ServiceStatus.dwCurrentState);
end;

procedure TForm2.Button1Click(Sender: TObject);

begin
   If ServiceStop('','mysql') then Showmessage('Service Stopped')
   else Showmessage('Nope');
end;

管理员资源可以由

  • 将上述显示 XML-Code 保存为 administrator.manifest
  • 使用内容创建为文件 administrator.rc

1 24“管理员.manifest”

  • 跑步

brcc32 管理员.rc

使用创建的 administrator.res 将需要:

  1. 在较新的 delphi 版本上禁用运行时主题,它们包含在清单中
  2. 从现有项目中删除 XPMan(组件和代码,包含在清单中)
  3. 如果您需要调试应用程序,delphi 也必须以管理员身份启动。

清单所需的源代码和任何内容都可以在此处下载

于 2013-03-19T06:46:29.843 回答
1

我最终做的是:

uses WinApi, ShellApi;

function StartStopDatabase(start: boolean): integer;
var
  Info: TShellExecuteInfo;
  verb: string;
  ExitCode: DWORD;
begin
  Result := -1;
  if start
    then verb := 'start'
    else verb := 'stop';
  FillChar(Info, SizeOf(Info), 0);
  Info.cbSize := SizeOf(TShellExecuteInfo);
  Info.fMask := SEE_MASK_NOCLOSEPROCESS;
  Info.Wnd := Application.Handle;
  Info.lpVerb := 'RunAs';
  Info.lpFile := 'net';
  Info.lpParameters := PWideChar(verb + ' mysql');
  Info.nShow := SW_SHOW;
  if ShellExecuteEx(@Info) then begin
    repeat
      Sleep(100);
      Application.ProcessMessages;
      GetExitCodeProcess(Info.hProcess, ExitCode);
    until (ExitCode <> STILL_ACTIVE) or Application.Terminated;
    Result := ExitCode;
  end;
end;
于 2013-03-25T05:57:34.890 回答