Pesquisar no Programero

segunda-feira, 24 de novembro de 2008

Texto na Diagonal usando o Canvas (Delphi)

var
  lf : TLogFont;
  tf : TFont;
begin
  with Form1.Canvas do
    begin
      Font.Name := 'Arial';
      Font.Size := 24;
      tf := TFont.Create;
      tf.Assign(Font);
      GetObject(tf.Handle, sizeof(lf), @lf); 
      lf.lfEscapement := 450;
      lf.lfOrientation := 450;
      tf.Handle := CreateFontIndirect(lf);
      Font.Assign(tf);
      tf.Free;
      TextOut(20, Height div 2, 'Texto Diagonal!');
   end;
end;

sexta-feira, 14 de novembro de 2008

Iniciar e Parar Serviços do Windows (Delphi)

Em certas ocasiões nos deparamos com a necessidade de manipular determinadas atividades do SO, como iniciar ou parar um banco de dados, ou qualquer outro serviço que esteja funcionando no momento. Segue abaixo um código que encontrei na Internet para tal finalidade (não me recordo à fonte, assim que eu a encontrar colocarei).

Iniciar Serviço:

uses WinSvc;
//
// start service
//
// return TRUE if successful
//
// sMachine:
//   machine name, ie: \SERVER
//   empty = local machine
//
// sService
//   service name, ie: Alerter
//
function ServiceStart(
  sMachine,
  sService : string ) : boolean;
var
  //
  // service control
  // manager handle
  schm,
  //
  // service handle
  schs   : SC_Handle;
  //
  // service status
  ss     : TServiceStatus;
  //
  // temp char pointer

  psTemp : PChar;
  //
  // check point

  dwChkP : DWord;
begin
  ss.dwCurrentState := -1;
  // connect to the service
  // control manager
  schm := OpenSCManager(
    PChar(sMachine),
    Nil,
    SC_MANAGER_CONNECT);
  // if successful...
if(schm > 0)then
begin
    // open a handle to
    // the specified service
    schs := OpenService(
      schm,
      PChar(sService),
      // we want to
      // start the service and
      SERVICE_START or
     // query service status
      SERVICE_QUERY_STATUS);
   // if successful...
if(schs > 0)then
begin
      psTemp := Nil;
if(StartService(
           schs,
           0,
           psTemp))then
begin
  // check status
if(QueryServiceStatus(
             schs,
             ss))then
begin
while(SERVICE_RUNNING
            <> ss.dwCurrentState)do
begin
           //
            // dwCheckPoint contains a
            // value that the service
            // increments periodically
            // to report its progress
            // during a lengthy
            // operation.
            //
            // save current value
            //
            dwChkP := ss.dwCheckPoint;
          //
            // wait a bit before
            // checking status again
            //
            // dwWaitHint is the
            // estimated amount of time
            // the calling program
            // should wait before calling
            // QueryServiceStatus() again
            //
            // idle events should be
            // handled here...
            //
            Sleep(ss.dwWaitHint);
if(not QueryServiceStatus(
                 schs,
                 ss))then
begin
          // couldn't check status
              // break from the loop
              break; 
end;
if
(ss.dwCheckPoint <
              dwChkP)then
begin
         // QueryServiceStatus
              // didn't increment
              // dwCheckPoint as it
              // should have.
              // avoid an infinite
              // loop by breaking
              break;
       end;
          end;
        end;
      end;
     // close service handle
      CloseServiceHandle(schs);
   end;
  // close service control
    // manager handle
    CloseServiceHandle(schm);
  end;
// return TRUE if
  // the service status is running
  Result :=
    SERVICE_RUNNING =
      ss.dwCurrentState;
end;

Parar Serviço:

//
// stop service
//
// return TRUE if successful
//
// sMachine:
//   machine name, ie: \SERVER
//   empty = local machine
//
// sService
//   service name, ie: Alerter
//
function ServiceStop(
  sMachine,
  sService : string ) : boolean;
var
//
  // service control
  // manager handle
  schm,
//
  // service handle

  schs   : SC_Handle;
//
  // service status
ss     : TServiceStatus;
  //
  // check point

  dwChkP : DWord;
begin
// connect to the service
  // control manager

  schm := OpenSCManager(
    PChar(sMachine),
    Nil,
    SC_MANAGER_CONNECT);
// if successful...
if(schm > 0)then
begin
// open a handle to
    // the specified service

    schs := OpenService(
      schm,
      PChar(sService),
   // we want to
      // stop the service and
      SERVICE_STOP or
   // query service status
      SERVICE_QUERY_STATUS);
// if successful...
if(schs > 0)then
begin
if(ControlService(
           schs,
           SERVICE_CONTROL_STOP,
           ss))then
begin
  // check status
if(QueryServiceStatus(
             schs,
             ss))then
begin
while(SERVICE_STOPPED
            <> ss.dwCurrentState)do
begin
            //
            // dwCheckPoint contains a
            // value that the service
            // increments periodically
            // to report its progress
            // during a lengthy
            // operation.
            //
            // save current value
            //
            dwChkP := ss.dwCheckPoint;
      //
            // wait a bit before
            // checking status again
            //
            // dwWaitHint is the
            // estimated amount of time
            // the calling program
            // should wait before calling
            // QueryServiceStatus() again
            //
            // idle events should be
            // handled here...
            //
            Sleep(ss.dwWaitHint);
if(not QueryServiceStatus(
                 schs,
                 ss))then
begin
        // couldn't check status
              // break from the loop
              break; 
end;
if(ss.dwCheckPoint <
              dwChkP)then
begin
            // QueryServiceStatus
              // didn't increment
              // dwCheckPoint as it
              // should have.
              // avoid an infinite
              // loop by breaking
              break;
          end;
          end;
        end;
      end;
   // close service handle
      CloseServiceHandle(schs);
    end;
  // close service control
    // manager handle
    CloseServiceHandle(schm);
  end;
// return TRUE if
  // the service status is stopped
  Result :=
    SERVICE_STOPPED =
      ss.dwCurrentState;
end;

Exemplo de uso:

Para inciar:
   ServiceStart('nome_do_computador', 'nome_do_servico');
Para parar:
   ServiceStop('nome_do_computador', 'nome_do_servico');

 

Exemplo completo

Para aqueles que encontraram alguma dificuldade em implementar, ou que estejam buscando uma maneira de faze-lo segue um exemplo completo e rodando, qualquer dúvida é só postar nos comentários.