该文件已经存在+ Logman启动时无法创建文件

时间:2019-05-19 05:59:08

标签: delphi logman

我使用logman命令从delphi代码创建了数据收集器集。 但是无法使用logman start命令启动设置的数据收集器。

启动命令导致以下错误

该文件已存在时无法创建文件

同一启动命令执行5次以上后,数据收集器集将启动。

请分享如何解决此问题的想法。

function ExecAndCapture(const ACmdLine: string; var AOutput: 
string):Integer;
const
 cBufferSize = 2048;
var
  Buffer: Pointer;
  StartupInfo: TStartUpInfo;
  SecurityAttributes: TSecurityAttributes;
  ReadBytes: DWord;
  ProcessInfo: TProcessInformation;
  StdInPipe : TAnoPipe;
  StdOutPipe: TAnoPipe;
  TempStr: string;
  Command: array [0..1023] of char;
begin
   Result := 0;

 StrPCopy(Command, ACmdLine );
 with SecurityAttributes do
 begin
   nlength := SizeOf(TSecurityAttributes);
   binherithandle := True;
   lpsecuritydescriptor := nil;
 end;

 // Create anonymous pipe for standard input
 if not CreatePipe(StdInPipe.Output, StdInPipe.Input, @SecurityAttributes, 
 0) then
   raise Exception.Create('Failed to create pipe for standard input. 
  System error message: ' + SysErrorMessage(GetLastError));

 try
   // Create anonymous pipe for standard output (and also for standard 
error)
    if not CreatePipe(StdOutPipe.Output, StdOutPipe.Input, 
@SecurityAttributes, 0) then
     raise Exception.Create('Failed to create pipe for standard output. 
System error message: ' + SysErrorMessage(GetLastError));

try
  GetMem(Buffer, cBufferSize);
  try
    // initialize the startup info to match our purpose
    FillChar(StartupInfo, Sizeof(TStartUpInfo), #0);
    StartupInfo.cb         := SizeOf(TStartUpInfo);
    StartupInfo.wShowWindow:= SW_HIDE;  // we don't want to show the process
    // assign our pipe for the process' standard input
    StartupInfo.hStdInput  := StdInPipe.Output;
    // assign our pipe for the process' standard output
    StartupInfo.hStdOutput := StdOutPipe.Input;
    StartupInfo.dwFlags    := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;

    if not CreateProcess(nil
                         , Command
                         , @SecurityAttributes
                         , @SecurityAttributes
                         , True
                         , NORMAL_PRIORITY_CLASS
                         , nil
                         , nil
                         , StartupInfo
                         , ProcessInfo) then
      raise Exception.Create('Failed creating the console process. System error msg: ' + SysErrorMessage(GetLastError));

    try
      // wait until the console program terminated
      while WaitForSingleObject(ProcessInfo.hProcess, 50)=WAIT_TIMEOUT do
        Sleep(0);

      // clear the output storage
      AOutput := '';
      // Read text returned by the console program in its StdOut channel
      repeat
        ReadFile(StdOutPipe.Output, Buffer^, cBufferSize, ReadBytes, nil);
        if ReadBytes > 0 then
        begin
          TempStr := TEncoding.ASCII.GetString(Buffer, 0, ReadBytes);
          AOutput := AOutput + TempStr;
          Inc(Result, ReadBytes);
        end;
      until (ReadBytes < cBufferSize);
    finally
      CloseHandle(ProcessInfo.hProcess);
      CloseHandle(ProcessInfo.hThread);
    end;
  finally
    FreeMem(Buffer);
  end;
finally
  CloseHandle(StdOutPipe.Input);
  CloseHandle(StdOutPipe.Output);
end;
 finally
   CloseHandle(StdInPipe.Input);
   CloseHandle(StdInPipe.Output);
 end;
 end;


   procedure TDataCollectorSet.CreateDataCollectorSet;
   const
     C_ALREADY_EXISTS = 'Data Collector already exists';
   var
     CmdStr          : string;
     OutPutStr       : string;
   begin
     CmdStr := 'LOGMAN.EXE CREATE COUNTER -n '+FDataCollectorSet+
                                     ' -si 1'  +
                                     ' -s '+ FHostIP +
                                     ' -u '+ FUserName.QuotedString('"')+' 
                              '+FPassword.QuotedString('"');


     if (ExecAndCapture(CmdStr, OutPutStr)) > 0 then
     begin
       if (Pos(COMMAND_SUCESS, OutPutStr) > 0) then
       begin
         CreatedSuccessfully := true;
         FOutPutMsg          := 'Data collector created sucessfully';
       end
     end ;
   end;


function TDataCollectorSet.StartDataCollectorSet: boolean;
var
  CmdStr :string;
begin
  Result    := false;
  CmdStr    := 'LOGMAN.EXE START '+FDataCollectorSet+' -s '+ FHostIP; 
  if ExecAndCapture(CmdStr, FOutPutMsg) > 0 then
  begin
    if (Pos(COMMAND_SUCESS, FOutPutMsg) > 0) or
       (Pos(C_ALREADY_RUNNING, FOutPutMsg)>0) then
    begin
      result := true;
      FIsRunning := true;
    end;
  end;
end;

0 个答案:

没有答案