Data e hora de outro PC na rede

Projeto [x]Harbour - Compilador de código aberto compatível com o Clipper.

Moderador: Moderadores

Eduardo Pinho
Colaborador
Colaborador
Mensagens: 72
Registrado em: 13 Jun 2014 19:30
Localização: Niteroi

Data e hora de outro PC na rede

Mensagem por Eduardo Pinho »

Boa tarde,
Alguém sabe como faz pra pegar a data e hora de outro PC na rede, pelo Harbour?
Consigo fazer uma gambiarra usando hb_run e net time, mas sempre quando uso "run" pipoca na tela uma janelinha preta meio tosca que nao consigo tirar.
Além do mais, imagino que talvez exista uma forma mais simples.
Pegar data e hora precisa da internet pelo harbour, tambem ajudaria.
Agradeço a ajuda.
Abcs
Avatar do usuário
Vlademiro
Usuário Nível 4
Usuário Nível 4
Mensagens: 752
Registrado em: 11 Jul 2005 02:46

Data e hora de outro PC na rede

Mensagem por Vlademiro »

Aqui no fórum tem uma rotina do Alexandre Simões que esconde essa janela. Eu uso aqui nos meus fontes. Vou procurar aqui.
Avatar do usuário
Vlademiro
Usuário Nível 4
Usuário Nível 4
Mensagens: 752
Registrado em: 11 Jul 2005 02:46

Data e hora de outro PC na rede

Mensagem por Vlademiro »

Código: Selecionar todos

#ifdef __PLATFORM__WINDOWS
/*
https://pctoledo.org/forum/viewtopic.php?f=57&t=23997&p=139078&hilit=run#p139078
*/
FUNCTION VLJ_ExecuteWin( cProgram, cParameter, lEspera ) 
LOCAL oShell, lOk := .T., nStyle, oErro

   lEspera := Hb_DefaultValue(lEspera, .f.)
   hb_Default( @cParameter , "" )
   
   BEGIN SEQUENCE WITH __BreakBlock()
      oShell := Win_OleCreateObject( "WScript.Shell" )
   RECOVER
      lOk := .F.
   END
   
   IF lOk
      BEGIN SEQUENCE WITH __BreakBlock()
         /*
         intWindowStyle
         Description
         0  Hides the window and activates another window.
         1  Activates and displays a window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when displaying the window for the first time.
         2  Activates the window and displays it as a minimized window.
         3  Activates the window and displays it as a maximized window.
         4  Displays a window in its most recent size and position. The active window remains active.
         5  Activates the window and displays it in its current size and position.
         6  Minimizes the specified window and activates the next top-level window in the Z order.
         7  Displays the window as a minimized window. The active window remains active.
         8  Displays the window in its current state. The active window remains active.
         9  Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window.
         10 Sets the show-state based on the state of the program that started the application. 
         */
         nStyle := 0
         oShell:Run("%comspec% /c " + cProgram + " " + cParameter, nStyle, lEspera) 
      RECOVER
         Alert("Erro executando " + cProgram, "Erro")
         lOk := .F.
      END
      
      oShell := Nil
      
   ENDIF
   
RETURN lOk
#endif
Avatar do usuário
asimoes
Colaborador
Colaborador
Mensagens: 4919
Registrado em: 26 Abr 2007 16:48
Localização: RIO DE JANEIRO-RJ

Data e hora de outro PC na rede

Mensagem por asimoes »

Pegar data e hora da internet, horário de Brasília

Código: Selecionar todos

Alert( GetDataTimeNtp( "200.160.7.186" ) )


FUNCTION GetDataTimeNtp( cSeverNtp )
LOCAL cRet, cDateTime:= Alltrim( GETNTPDATE( cSeverNtp ) )
         
RETURN ( cDateTime )

#pragma BEGINDUMP

#include <hbapi.h>
#include <winsock.h>
#include <time.h>
	 

#define MAXLEN 1024

HB_FUNC( GETNTPDATE )
{
   char * hostname = ( char * ) hb_parc( 1 );
   unsigned char msg[ 48 ] = { 010, 0, 0, 0, 0, 0, 0, 0, 0 };   // the packet we send
   unsigned long buf[ MAXLEN ]; // the buffer we get back
   struct sockaddr_in server_addr;
   int  s;  // socket
   WSADATA wsa;
   struct timeval timeout;
   fd_set fds;
   time_t tmit;
             
   WSAStartup( 0x101, &wsa );    

   s = socket( PF_INET, SOCK_DGRAM, getprotobyname( "udp" )->p_proto );

   memset( &server_addr, 0, sizeof( server_addr ) );
   server_addr.sin_family = AF_INET;
   server_addr.sin_addr.s_addr = inet_addr( hostname );
   server_addr.sin_port = htons( 123 ); // ntp port
       
   sendto( s, msg, sizeof( msg ), 0, ( struct sockaddr * ) &server_addr, sizeof( server_addr ) );
       
   FD_ZERO( &fds );
   FD_SET( s, &fds );
   timeout.tv_sec = 10;
   timeout.tv_usec = 0;
         
   if( select( 0, &fds, NULL, NULL, &timeout ) )
   {      
      recv( s, ( void * ) buf, sizeof( buf ), 0 );

      tmit = ntohl( buf[ 10 ] );
      tmit-= 2208988800U;
   }  
   else
      MessageBox( 0, "can't read from NTP server", "ok", 0 );      
       
   WSACleanup();

   hb_retc( ctime( &tmit ) );  
}

#pragma ENDDUMP
►Harbour 3.x | Minigui xx-x | HwGui◄
Pense nas possibilidades abstraia as dificuldades.
Não corrigir nossas falhas é o mesmo que cometer novos erros.
A imaginação é mais importante que o conhecimento. (Albert Einstein)
Avatar do usuário
asimoes
Colaborador
Colaborador
Mensagens: 4919
Registrado em: 26 Abr 2007 16:48
Localização: RIO DE JANEIRO-RJ

Data e hora de outro PC na rede

Mensagem por asimoes »

Se quiser pegar somente a hora:

cTime := SubStr( cDateTime, 12, 8 )
►Harbour 3.x | Minigui xx-x | HwGui◄
Pense nas possibilidades abstraia as dificuldades.
Não corrigir nossas falhas é o mesmo que cometer novos erros.
A imaginação é mais importante que o conhecimento. (Albert Einstein)
Avatar do usuário
asimoes
Colaborador
Colaborador
Mensagens: 4919
Registrado em: 26 Abr 2007 16:48
Localização: RIO DE JANEIRO-RJ

Data e hora de outro PC na rede

Mensagem por asimoes »

►Harbour 3.x | Minigui xx-x | HwGui◄
Pense nas possibilidades abstraia as dificuldades.
Não corrigir nossas falhas é o mesmo que cometer novos erros.
A imaginação é mais importante que o conhecimento. (Albert Einstein)
Eduardo Pinho
Colaborador
Colaborador
Mensagens: 72
Registrado em: 13 Jun 2014 19:30
Localização: Niteroi

Data e hora de outro PC na rede

Mensagem por Eduardo Pinho »

Obrigado a todos.. Resolvi...
Responder