createprocess - access violation 'kernel32.dll'

20/01/2010

0

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

Responder

Posts

17/04/2011

Jáder Medeiros

  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;

Responder

Que tal ter acesso a um e-book gratuito que vai te ajudar muito nesse momento decisivo?

Ver ebook

Recomendado pra quem ainda não iniciou o estudos.

Eu quero
Ver ebook

Recomendado para quem está passando por dificuldades nessa etapa inicial

Eu quero

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar