API - abrir dialogo para executar programa do windows

Top  Previous  Next

uses ShellAPI;

 

procedure APIExecutarPrograma(Titulo: string = ''; Descricao: string = ''; Icone: TIcon = nil);

type

  SHRunDialogProc = function(wnd: HWND; Unknown1: integer; Unknown2: Pointer; szTitle: PChar; szPrompt: PChar; uiFlages: integer): DWORD; stdcall;

var

  SHRunDialog      : SHRunDialogProc;

  CaptionBuffer    : Pointer;

  DescriptionBuffer: Pointer;

  ShellHandle      : THandle;

 

begin

  ShellHandle := Windows.LoadLibrary(PChar(shell32));

  if ShellHandle <> 0 then

  begin

    SHRunDialog := GetProcAddress(ShellHandle, PChar(61));

 

    CaptionBuffer     := nil;

    DescriptionBuffer := nil;

 

    if (Titulo    <> '') then GetMem(CaptionBuffer, (Length(Titulo) + 1) * SizeOf(WideChar));

    if (Descricao <> '') then GetMem(DescriptionBuffer, (Length(Descricao) + 1) * SizeOf(WideChar));

 

    if (Win32Platform = VER_PLATFORM_WIN32_NT) then

    begin

      if (CaptionBuffer     <> nil) then StringToWideChar(Titulo, PWideChar(CaptionBuffer), (Length(Titulo) + 1));

      if (DescriptionBuffer <> nil) then StringToWideChar(Descricao, PWideChar(DescriptionBuffer), (Length(Descricao) + 1));

    end

    else

    begin

      if (CaptionBuffer     <> nil) then StrPCopy(PChar(CaptionBuffer)    , Titulo);

      if (DescriptionBuffer <> nil) then StrPCopy(PChar(DescriptionBuffer), Descricao);

    end;

 

    if Assigned(Icone) then

      SHRunDialog(Application.Handle, Icone.Handle, nil, CaptionBuffer, DescriptionBuffer, 0)

    else

      SHRunDialog(Application.Handle, Application.Icon.Handle, nil, CaptionBuffer, DescriptionBuffer, 0)

  end

  else

    ShowMessage('Função não suportada por esta versão do Windows');

end;

 

procedure TForm1.Button1Click(Sender: TObject);

begin

  APIExecutarPrograma('Besteira''Besteirol', Image1.Picture.Icon);

end;