---------------------------------------------------------
File:				crt_test_readme.txt 
Description: 		Readme	file for crt_test.exe
See also:			crt_eg.tcl
---------------------------------------------------------
* ANTIVIRUS FALSE POSITIVES *

This is a very simple example program which links to MSVCRT40.DLL and which reads the time
It uses 2 methods (dynamic and static DLL linking) to read the time

Some antivirus programs (Avast, Norton) may report this as a false positive
This has been reported as a false positive countless times but the vendors seem unable to fix the issue

See: crt_eg.tcl for a Tcl equivalent which includes more examples

The full C++  source code for the C equivalent (crt_test.exe) is below...
-------------------------------------------------------------------------------------------
#include <windows.h>		// For dynamic linking HINSTANCE
#include <stdio.h>
#include <time.h>
#include <memory.h>

/*
The struct which is used:
struct tm {
        int tm_sec;     // seconds after the minute - [0,59]
        int tm_min;     // minutes after the hour - [0,59]
        int tm_hour;    // hours since midnight - [0,23]
        int tm_mday;    // day of the month - [1,31]
        int tm_mon;     // months since January - [0,11]
        int tm_year;    // years since 1900
        int tm_wday;    // days since Sunday - [0,6]
        int tm_yday;    // days since January 1 - [0,365]
        int tm_isdst;   // daylight savings time flag
        }; // */

/*
// Specimen prototypes
_CRTIMP char * __cdecl asctime(const struct tm *);
_CRTIMP char * __cdecl ctime(const time_t *);
_CRTIMP clock_t __cdecl clock(void);
_CRTIMP double __cdecl difftime(time_t, time_t);
_CRTIMP struct tm * __cdecl gmtime(const time_t *);
_CRTIMP struct tm * __cdecl localtime(const time_t *);
_CRTIMP time_t __cdecl mktime(struct tm *);
_CRTIMP size_t __cdecl strftime(char *, size_t, const char *,
        const struct tm *);
_CRTIMP char * __cdecl _strdate(char *);
_CRTIMP char * __cdecl _strtime(char *);
_CRTIMP time_t __cdecl time(time_t *); // */

void main()
{
	struct tm when, *ptr;
	time_t now, result;
	int    days;

	printf("sizeof(struct tm) == %li\r\n",sizeof(when));

	// Dynamically-linked //

	HINSTANCE hApi=NULL;							// Global handle
	hApi = ::LoadLibraryA("msvcrt40.dll");
	if(!hApi)
	{
		printf("Failed to link library msvcrt40.dll\r\n");
		return;
	}
	// Static ptr declarations
	typedef time_t (__cdecl *time_type)(time_t*);
	typedef char * (__cdecl *asctime_type)(const struct tm *);
	typedef struct tm* (__cdecl *localtime_type)(const time_t *);

	// Declare //
	time_type time_p;
	asctime_type asctime_p;
	localtime_type localtime_p;

	time_p = (time_t (__cdecl *)(time_t*))GetProcAddress(hApi,"time");
	if(!time_p)
	{
		printf("Failed to link library msvcrt40.dll -> time\r\n");
		return;
	}
	asctime_p = (char * (__cdecl *)(const struct tm *))GetProcAddress(hApi,"asctime");
	if(!asctime_p)
	{
		printf("Failed to link library msvcrt40.dll -> asctime\r\n");
		return;
	}
	localtime_p = (struct tm* (__cdecl *)(const time_t *))GetProcAddress(hApi,"localtime");
	if(!localtime_p)
	{
		printf("Failed to link library msvcrt40.dll -> localtime\r\n");
		return;
	}
	printf("Dynamically-linked to msvcrt40.dll OK\r\n");
	time_p( &now );
	ptr=localtime_p( &now );
	when = *ptr;
	printf( "Current time for %li is %s\n", (long)ptr, asctime_p( &when ) );

	/////////////////////////////////////////////////////////////////////////////
	// Statically-linked //
	/////////////////////////////////////////////////////////////////////////////
	printf("tm_sec==%x\r\n",when.tm_sec);
	printf("tm_min==%x\r\n",when.tm_min);
	printf("tm_hour==%x\r\n",when.tm_hour);
	printf("tm_mday==%x\r\n",when.tm_mday);
	printf("tm_mon==%x\r\n",when.tm_mon);
	printf("tm_year==%x\r\n",when.tm_year);
	printf("tm_wday==%x\r\n",when.tm_wday);
	printf("tm_yday==%x\r\n",when.tm_yday);
	printf("tm_isdst==%x\r\n",when.tm_isdst);

	printf("-- Hex ----------------------------------\r\n");
	char* tmp=(char*) ptr;
	for(long j=0; j<sizeof(when); j++)
	{
		printf("%2.2x ",(unsigned char) *(tmp+j));
		if(j> 0 && ((j+1) % 8 == 0))
			printf("\r\n");
	}
	printf("\r\n");
	printf("-----------------------------------------\r\n");

	////////////////////////////////////////////////////////////////////////
	//set t 1470404087	# Fri Aug 05 14:34:47 2016
	////////////////////////////////////////////////////////////////////////
	printf("\r\nPreset time %li\r\n",1470404087);
	now=1470404087;	// 
	ptr=localtime_p( &now );		// This is not GMT
	when = *ptr;

	printf("tm_sec==%x %li\r\n",when.tm_sec,when.tm_sec);
	printf("tm_min==%x %li\r\n",when.tm_min,when.tm_min);
	printf("tm_hour==%x %li\r\n",when.tm_hour,when.tm_hour);
	printf("tm_mday==%x\r\n",when.tm_mday);
	printf("tm_mon==%x\r\n",when.tm_mon);
	printf("tm_year==%x\r\n",when.tm_year);
	printf("tm_wday==%x\r\n",when.tm_wday);
	printf("tm_yday==%x\r\n",when.tm_yday);
	printf("tm_isdst==%x\r\n",when.tm_isdst);

	printf("-- Hex ----------------------------------\r\n");
	tmp=(char*) ptr;
	for(j=0; j<sizeof(when); j++)
	{
		printf("%2.2x ",(unsigned char) *(tmp+j));
		if(j> 0 && ((j+1) % 8 == 0))
			printf("\r\n");
	}
	printf("\r\n");
	printf("-----------------------------------------\r\n");

	printf("Current time for %lu is %s\n", now, asctime_p( &when ) );
	printf("Should be Fri Aug 05 14:34:47 2016\r\n");
}

-------------------------------------------------------------------------------------------