createprocess - access violation 'kernel32.dll'

Delphi

20/01/2010

Boa Noite a todos!
Estou tentando usar o createprocess mas estou obtendo um access violation no 'kernel32.dll'.
Alguém já passou por isso e conseguiu resolver ?

Grato pelo ajuda
_osw

_osw

Curtidas 0

Respostas

Jáder Medeiros

Jáder Medeiros

20/01/2010

  Deve ser porque a string de sua IDE é tratada como unicode. A diretiva UNICODE revela essa informação.

  Para corrigir o problema, trate sua string com o código abaixo:


    {$IFDEF UNICODE}

    if StringRefCount(vCommandLine) = -1 then begin

      vCommandLine := Copy(vCommandLine, 1, MaxInt);

    end;

    {$ENDIF UNICODE}



  Abaixo, uma função de exemplo para criar um processo com o CreateProcess:


function CreateProcessAndWait(const aCommandLine: string): string;

const

  ReadBuffer = 1048576;

var

  Security: TSecurityAttributes;

  ReadPipe, WritePipe: THandle;

  start: TStartUpInfo;

  ProcessInfo: TProcessInformation;

  Buffer: PAnsiChar;

  TotalBytesRead, BytesRead: DWORD;

  Apprunning, n, BytesLeftThisMessage, TotalBytesAvail: Integer;

  vCommandLine: string;

begin

  Security.nlength := SizeOf(TSecurityAttributes);

  Security.binherithandle := True;

  Security.lpsecuritydescriptor := nil;

  if CreatePipe (ReadPipe, WritePipe, @Security, 0) then begin

    Buffer := AllocMem(ReadBuffer + 1);

    FillChar(Start, Sizeof(Start), #0);

    start.cb := SizeOf(start);

    start.hStdOutput := WritePipe;

    start.hStdInput := ReadPipe;

    start.dwFlags := STARTF_USESTDHANDLES + STARTF_USESHOWWINDOW;

    start.wShowWindow := SW_HIDE;

    vCommandLine := aCommandLine;

    {$IFDEF UNICODE}

    if StringRefCount(vCommandLine) = -1 then begin

      vCommandLine := Copy(vCommandLine, 1, MaxInt);

    end;

    {$ENDIF UNICODE}

    if CreateProcess(nil, PWideChar(vCommandLine), @Security, @Security, True, CREATE_NO_WINDOW or NORMAL_PRIORITY_CLASS, nil, nil, start, ProcessInfo) then begin

      n := 0;

      TotalBytesRead := 0;

      repeat

        Inc(n, 1);

        Apprunning := WaitForSingleObject(ProcessInfo.hProcess,100);

        Application.ProcessMessages;

        if not PeekNamedPipe(ReadPipe, @Buffer[TotalBytesRead], ReadBuffer, @BytesRead, @TotalBytesAvail, @BytesLeftThisMessage) then begin

          Break;

        end

        else if BytesRead > 0 then begin

          ReadFile(ReadPipe, Buffer[TotalBytesRead], BytesRead, BytesRead, nil);

        end;

        TotalBytesRead := TotalBytesRead + BytesRead;

      until (Apprunning  WAIT_TIMEOUT) or (n > 150);

      Buffer[TotalBytesRead] := #0;

      OemToChar(PAnsiChar(Buffer), PWideChar(string(Buffer)));

      Result := Result + PChar(string(Buffer));

    end;

    FreeMem(Buffer);

    CloseHandle(ProcessInfo.hProcess);

    CloseHandle(ProcessInfo.hThread);

    CloseHandle(ReadPipe);

    CloseHandle(WritePipe);

  end;

end;

GOSTEI 0
POSTAR