Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read RTC in Windows

I'm not a C/ASM developer and I would like to get current date and time from RTC with a Windows program.

Here, I found a C code to do this.

I changed that code in the following way, and I compiled it with Visual Studio 2017 cl.exe compiler without errors and warnings:

#include <stdio.h>

int main()
{
   unsigned char tvalue, index;

   printf("STARTING...\n");

   for(index = 0; index < 128; index++)
   {
      __asm
      {
         cli             /* Disable interrupts */
         mov al, index   /* Move index address */
                         /* since the 0x80 bit of al is not set, */
                         /* NMI is active */
         out 0x70, al    /* Copy address to CMOS register */
                         /* some kind of real delay here is probably best */
         in al, 0x71     /* Fetch 1 byte to al */
         sti             /* Enable interrupts */
         mov tvalue, al
      }

      printf("%u\n", (unsigned int)tvalue);
   }

   printf("FINISHED!\n");
   return 0;
}

When I try to execute the exe from the command prompt, I don't see anything, only the row "STARTING...".

What am I doing wrong?

Thanks a lot.

like image 317
Fabrizio Avatar asked Dec 11 '22 02:12

Fabrizio


2 Answers

The example code you found is operating system code, not Windows code. It would be sheer chaos if Windows allowed random processes to interact randomly with hardware devices like the real time clock. The operating system has a driver that talks to the real time clock and it won't allow processes to randomly poke into it.

As just the most obvious problem, you can't just disable interrupts from a process while a modern operating system is running!

like image 96
David Schwartz Avatar answered Dec 24 '22 04:12

David Schwartz


I would like to get current date and time from RTC with a Windows program.

On Windows, you use Windows APIs (or wrappers)

The main APIs to read the system time are :

GetSystemTime

GetSystemTimePreciseAsFileTime

NtQuerySystemTime

like image 20
Castorix Avatar answered Dec 24 '22 05:12

Castorix