Thread: registry question

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    99

    registry question

    Hi everyone, is there a way to get the created, modified and last accessed dates (or any of these) from registry keys?

    Ive had a look at RegQueryInfoKey() and although i don't really understand it, it seems to only give the last write time. Does this mean that created and modifed dates of registry keys cannot be obtained?


    thank you

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think the only info on registry keys is the "LastWriteTime", which
    Quote Originally Posted by MSDN
    The function sets the members of the FILETIME structure to indicate the last time that the key or any of its value entries is modified.
    Registry entries are not files, and they use a more compact set of information than a file. Most files are kilobytes or megabytes, so having a few extra bytes of information for each file is perfectly acceptable, whilst most registry keys/values are bytes long, so having a bunch of extra bytes to hold various dates is noticably more overhead.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    I have placed part of my program below, if searches and finds all the keys stored under the key USBSTOR and stores them into 'buf' temporarily then prints them out. I have tried to use the FILETIME function to find each key's last written time but i seem to be getting a lot of errors with too few parameters in GetFileTime. Am i heading down the right lines with using FILETIME in the registry?


    thank you


    Code:
        
    //below find all the keys stored under the USBSTOR key in variable buf.
    
        HKEY hKey = 0;
        char buf[255] = {0};
        DWORD dwBufSize = sizeof(buf);
    
        const char* subkey = "SYSTEM\\CurrentControlSet\\Enum\\USBSTOR";
    
    
        if( RegOpenKey(HKEY_LOCAL_MACHINE,subkey,&hKey) == ERROR_SUCCESS)
        {
            int i = 0;
            while(RegEnumKeyEx(hKey, i++, buf, &dwBufSize, 0, 0, 0, 0) == ERROR_SUCCESS)
            {
              cout << "Found Key Value Name: " << buf << endl;
              dwBufSize = sizeof(buf);
    
    
    
    //getting the lastwrite time for registry key
    
    FILETIME writetime;
    GetFileTime(hKey, &writetime);
    
    SYSTEMTIME st;
    FileTimeToSystemTime( &writetime, &st )
                                             cout <<  st.wYear << "/"
    					 << st.wDay << "/"					
    					 << st.wMonth << " "
    					 << st.wHour << ":"
    					 << st.wMinute << ":"
    					 << st.wSecond << ":"
    					 << st.wMilliseconds ;
    
    }
    
    }

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    GetFileTime(hKey, &writetime);
    I'm this is absolutely wrong. You should not pass in a handle to a registry key in GetFileTime - you should use RegQueryInfoKey() as you posted in the first post. And if you DO want to use GetFileTime(), then you should pass in all the three parameters it expects:
    http://msdn.microsoft.com/en-us/libr...20(VS.85).aspx

    Code:
    FileTimeToSystemTime( &writetime, &st )
    Missing semicolon on this line.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    ah right, I see, i would like to use the RegQueryInfoKey()

    I am a bit stuck as to what to call in the regqueryinfokey though. 'hKey' is the link to HKEY_LOCAL_MACHINE keywhich I have passed. 'buf' contains the subkey name I want to query, but I am unsure as to whether to pass 'buf' or 'buf1' where 'buf1' contains the complete path and name to the registry key I want to query. I have also passed the FILETIME value, but it still asks for more parameters


    Code:
    FILETIME writeTime;
    
    RegQueryInfoKey(hKey,buf, &writeTime );
    
    
    
    SYSTEMTIME st;
    FileTimeToSystemTime( &writeTime, &st );
                                             cout <<  st.wYear << "/"
    					 << st.wDay << "/"					
    					 << st.wMonth << " "
    					 << st.wHour << ":"
    					 << st.wMinute << ":"
    					 << st.wSecond << ":"
    					 << st.wMilliseconds ;

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, you need to pass all 12 parameters to RegQueryInfoKey() - the first being hkey, all others being NULL, and the last one being a pointer to a filetime variable.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    cheers, this is my last post on the topic and thank you for your patients and help, I'm appreciating it!

    Your advice has got the time and date displaying correctly but I just need to check which key I'm getting the time for as I feel it may be the wrong one as all the times and dates are displaying the same.

    When I pass 'hKey' to my program, is that not passing the key HKEY_LOCAL_MACHINE and therefore I am reading the lastwritten time of this key everytime in my loop thats why its the same every time?

    As my value 'buf' contains the full path to the key I want to query the lastwritten time, do I need to pass this?

    thank you


    Code:
      HKEY hKey = 0;
        char buf[255] = {0};
        DWORD dwBufSize = sizeof(buf);
        const char* subkey = "SYSTEM\\CurrentControlSet\\Enum\\USBSTOR";
        string path = "SYSTEM\\CurrentControlSet\\Enum\\USBSTOR\\";
    	
    
        if( RegOpenKey(HKEY_LOCAL_MACHINE,subkey,&hKey) == ERROR_SUCCESS)
        {
            int i = 0;
            while(RegEnumKeyEx(hKey, i++, buf, &dwBufSize, 0, 0, 0, 0) == ERROR_SUCCESS)
            {
              cout << "Found Key Value Name: " << buf << endl;
              dwBufSize = sizeof(buf);
    		string bufish = buf;
    		string nextkey = path +bufish;          
    cout << nextkey << endl;
         
    
    FILETIME writeTime;
    RegQueryInfoKey(hKey,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,&writeTime);
    
    
    
    SYSTEMTIME st;
    FileTimeToSystemTime( &writeTime, &st );
                                             cout <<  st.wYear << "/"
    					 << st.wDay << "/"					
    					 << st.wMonth << " "
    					 << st.wHour << ":"
    					 << st.wMinute << ":"
    					 << st.wSecond << ":"
    					 << st.wMilliseconds ;

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    sorted it, thanks again for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Problem with the registry
    By neandrake in forum Windows Programming
    Replies: 1
    Last Post: 03-03-2004, 06:41 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. Registry Access
    By ExDigit in forum Windows Programming
    Replies: 3
    Last Post: 01-04-2002, 04:02 AM