0

I have a program that gets installed on my laptop to a json file.

I retrieve the date of the installed program from registry and I would like to convert that date to a timestamp.

This is my code :

HKEY hUninstKey = NULL;
HKEY hAppKey = NULL;

WCHAR InstallDate[1024];
WCHAR *sRoot = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
long lResult = ERROR_SUCCESS;
DWORD dwType = KEY_ALL_ACCESS;
DWORD dwBufferSize = 0;

//Open the "Uninstall" key.
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, sRoot, 0, KEY_READ, &hUninstKey) != ERROR_SUCCESS)
{

}

for (DWORD dwIndex = 0; lResult == ERROR_SUCCESS; dwIndex++)
{
    //Enumerate all sub keys...
    dwBufferSize = sizeof(sAppKeyName);
    if ((lResult = RegEnumKeyEx(hUninstKey, dwIndex, sAppKeyName,
        &dwBufferSize, NULL, NULL, NULL, NULL)) == ERROR_SUCCESS)
    {
        //Open the sub key.

        if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, sSubKey, 0, KEY_READ, &hAppKey) != ERROR_SUCCESS)
        {
            RegCloseKey(hAppKey);
            RegCloseKey(hUninstKey);

        }

        //Get the display name value from the application's sub key.
        dwBufferSize = sizeof((RegQueryValueEx(hAppKey, L"InstallDate", NULL, &dwType, (unsigned char*)InstallDate, &dwBufferSize) == ERROR_SUCCESS))
        {

            // Display Date Of Installed Program
            const TCHAR* xInstallDate = (TCHAR*)InstallDate;
            char DateInstall[MAX_LENGTH + 200];
            wcstombs_s(&nNumCharConverted, DateInstall, MAX_LENGTH + 200,
                xInstallDate, MAX_LENGTH + 200);


            file << "\"',date='" << DateInstall  << endl;               
        }
        else {
            //Display name value doe not exist, this application was probably uninstalled.
        }


        RegCloseKey(hAppKey);
    }
}

RegCloseKey(hUninstKey);

Could anyone please help with my code?

solid.py
  • 2,782
  • 5
  • 23
  • 30
  • what happens when you run it? What errors do you get, or what unexpected behaviour? – KBriggs Mar 03 '20 at 14:53
  • when i run it i get date='20191216' and i want to get it with timeStamp Like date='1576454400' – GoForex GoForex Mar 03 '20 at 15:01
  • So you want to convert a date time string to epoch time? If so, this will be helpful: https://stackoverflow.com/questions/11979655/how-to-convert-a-time-into-epoch-time – KBriggs Mar 03 '20 at 15:04
  • 2
    Make a small program that only does the conversion. The bulk of the code here is just noise that's helping the bug hide. Use [mcve] as inspiration. – user4581301 Mar 03 '20 at 15:05
  • Does this answer your question? [How to convert a time into epoch time?](https://stackoverflow.com/questions/11979655/how-to-convert-a-time-into-epoch-time) – KBriggs Mar 03 '20 at 15:05
  • A time point is always relative to a particular clock. You never mentioned, which clock you want to reference. – IInspectable Mar 03 '20 at 15:25
  • @KBriggs yes Thank You. it's working – GoForex GoForex Mar 03 '20 at 15:25
  • UNIX time is not a clock that's [commonly used in Windows](https://learn.microsoft.com/en-us/windows/win32/sysinfo/about-time). Yes, it *"works"*, in that it produces a value. But that value isn't of much use in Windows. – IInspectable Mar 03 '20 at 15:38
  • Why you have provided such complex example. Code which manipulates windows registries obscures your actual problem. – Marek R Mar 03 '20 at 16:04
  • `dwBufferSize = sizeof((RegQueryValueEx(hAppKey, L"InstallDate", NULL, &dwType, (unsigned char*)InstallDate, &dwBufferSize) == ERROR_SUCCESS))` WAT? – Marek R Mar 03 '20 at 16:10

1 Answers1

0

You should look into the usage of std::chrono::parse. It converts string streams into chrono objects which represent time.

This is the documentation of std::chrono::parse: https://en.cppreference.com/w/cpp/chrono/parse

This is a relevant question on SO (you are possibly a duplicate): How to parse a date string into a c++11 std::chrono time_point or similar?

Bart
  • 1,405
  • 6
  • 32