Click here to Skip to main content
16,015,481 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i cannot think of a way to do that i have a .cpp file from which an exe is created no i need to write a code in cpp to find out the very first date and possible time of that exe file running and i dont want to get the date and time everytime that exe is ran
only the date and time when it was first run
is it even possible?

What I have tried:

i have tried this
#include"stdafx.h"
#include <stdio.h>
#include <windows.h>
#include<conio.h>

int main(int argc, char** argv)
{
	WIN32_FILE_ATTRIBUTE_DATA attr;
	SYSTEMTIME creation;

	if (argc < 2)
		return 1;
	GetFileAttributesEx(_T(argv[1]), GetFileExInfoStandard, &attr);
	FileTimeToSystemTime(&attr.ftLastWriteTime, &creation);
	printf("Created: %04d-%02d-%02d %02d:%02d:%02d\n"
		"Size: %d bytes\n",
		creation.wYear, creation.wMonth, creation.wDay,
		creation.wHour, creation.wMinute, creation.wSecond,
		attr.nFileSizeLow);
	_getch();
	return 0;
}

but i think this will get the build date and not the firs trun time of the exe file
Posted
Updated 29-Jan-20 19:55pm

1 solution

Either add a field to your settings file (if you have one) defaulted to empty, or add a file to your app.
When you run the app, you check the setting or the file and if it's empty or doesn't exist then this is the first time, so get the current date and time, and store it in the setting or file.
If it isn't, that's the first run time.
 
Share this answer
 
Comments
Member 12899279 30-Jan-20 2:16am    
i don't have any settings file how to make that any help?
and do you mean i should create a file when first time an exe is run and then next time if ran i should use simple if to check whether the file that was supposed to be created first time has any data inside of it or not?and if there's any data then it should return date stored in that file(yes i will store date in that file) and if not it means exe is ran for first time and it will store the date in the file and show current date time to the user?
but i don't think thats a nice way..what if user accidentally deletes that text file?
isn't there any way by which it doesnt create an extra file?
OriginalGriff 30-Jan-20 2:28am    
If you don't have a settings file as part of your app, then adding one just for this is a little overkill (and how to do it would depend on what framework you are working under: MFC is different from CLI for example).

When you app runs, look for a specific file in a specific location: If it doesn't exist, this is the first time the app has run - so create it, and write the current date and time into it.

There is nothing you can do that will prevent the user from accidentally or deliberately getting rid of this info, unless you can access an online server: at the very least installing your app in a VM will mean that each time he runs it it will be the first time.

If you are trying to "protect" your software by time-limiting a trial, then you will need to get a lot more sophisticated: google for "protect app trial version c++" and you will get loads of ideas.

But ... don't spend too much time on it: if your app is popular, it will be stolen. Adobe spend man-years each time they release a new version adding protection against theft, and generally a cracked version appears the same day the new version is released. It's very, very hard to stop theft, but very easy to muck up a legitimate user and piss off a whole load of paying customers - and they will go elsewhere if that happens.
Member 12899279 30-Jan-20 8:30am    
can we do this with cli?what i am able to do this to simply create a text file and write date into it and when exe runs again it will check for this file if it exist it will show that date otherwise it will create new txt file and store date in it and display it
but what if the user deletes txt file or he runs exe from another folder
so i am looking for some method thts embedded in exe file in it self
Member 12899279 30-Jan-20 8:30am    
can we do this with cli?what i am able to do this to simply create a text file and write date into it and when exe runs again it will check for this file if it exist it will show that date otherwise it will create new txt file and store date in it and display it
but what if the user deletes txt file or he runs exe from another folder
so i am looking for some method thts embedded in exe file in it self
OriginalGriff 30-Jan-20 8:39am    
No you can't.

Or more specifically, yes you can - with some difficulty - but that is behaviour that virus scanners look for, so your app would on first run send up big red warnings from the user's antivirus and they sure as heck aren't going to be happy! (Or run it ever again).

And ... installing it in a VM would get round that, as well.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900