We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Uptime past 49.7 days (without using GetTickCount)

nem2k4
nem2k4 asked
on
Medium Priority
1,866 Views
Last Modified: 2007-12-19
I want to get the system uptime.. I can't use GetTickCount because it returns a 32-bit integer, resulting in a wraparound after 49.7 days.

I've read that using the registry root HKEY_PERFORMANCE_DATA you can read the system uptime (using RegQueryValueEx) but I'm not sure how to go about it.. the stuff over at MSDN isn't very helpful and I haven't managed to find any sample Delphi code anywhere.

Any help appreciated :)

nem2k4
Comment
Watch Question

Maybe this can help you.....

 Delphi 3.0 Performance Data Viewer for NT

 displays a tree view of the data held in the dynamic HKEY_PERFORMANCE_DATA registry hive.  
The download  includes components which encapsulate the performance data - including source, and a demo program.

http://www.wilsonc.demon.co.uk/delphi3.htm#NT%20Specific%20Components

Shane

Maybe this can help you.....

 Delphi 3.0 Performance Data Viewer for NT

 displays a tree view of the data held in the dynamic HKEY_PERFORMANCE_DATA registry hive.  
The download  includes components which encapsulate the performance data - including source, and a demo program.

http://www.wilsonc.demon.co.uk/delphi3.htm#NT%20Specific%20Components

Shane

Author

Commented:
Thanks.. https://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20047646.html?query=HKEY_PERFORMANCE_DATA&searchType=topic has the same problem I'm having - how do you actually query it (concrete code)?


The Performance Data Viewer displays perfmon info which is handy but there's no uptime statistic in there.. it uses HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\PerfLib\009
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks, I'll try that... what do I use in place of 'Software\Microsoft\Windows\CurrentVersion' though? :)

Author

Commented:
(and 'CommonFilesDir' ?)
You may be able to use an empty string "", try that

Shane
CommonFilesDir is what you are looking for.

This is the key to you query, cause if your gonna query for something, ya have to know what your querying for, in this case, the name which holds the value your looking for.

Again, since i've never dealt in to that part of the Registry, i dont have a clue where in that Key (HKEY_PERFORMANCE_DATA) it exists.

There are a lot of experts on in the morning, so you may want to wait until then to see if any will jump in here and help you with this issue.

Shane


Im sorry, i should explain more....

"CommonFilesDir" should be relaced with what you are looking for


Shane

Author

Commented:
Using this code:



procedure TForm1.SpeedButton1Click(Sender: TObject);
var
  hReg: HKEY;
  dwType, BufSize: DWORD;
  pBuf: array[0..MAX_PATH] of Char;
begin
  if RegOpenKey(HKEY_PERFORMANCE_DATA, '', hReg) = ERROR_SUCCESS then begin
    BufSize := MAX_PATH;
    RegQueryValueEx(hReg, 'CommonFilesDir', nil, @dwType, @pBuf, @BufSize);
    if (dwType = REG_SZ) then ShowMessage(string(PChar(@pBuf)));
    RegCloseKey(hReg);
  end else
    showmessage('unsuccessful');
end;


I just get the message "unsuccessful".

Author

Commented:
Yeah, that's the problem.. I don't know the exact names of what the names of the things I am looking for either.
Yeah, thats why i was saying, wait till morning, maybe some of the other experts will no some of the names of the things your looking for. Im still looking myself though...

Shane

Author

Commented:
craig_capel's stuff down the bottom looks promising, but according to him it doesn't quite work?

Perhaps using the code you posted, we can use "\\compname\System\System Up Time" as the key value?

The other stuff isn't suitable cuz you have to have a seperate program run on startup etc, my app needs to be self-contained.

Author

Commented:
some thing like:

procedure TForm1.SpeedButton1Click(Sender: TObject);
var
  hReg: HKEY;
  dwType, BufSize: DWORD;
  pBuf: array[0..MAX_PATH] of Char;
begin
  if RegOpenKey(HKEY_PERFORMANCE_DATA, 'System', hReg) = ERROR_SUCCESS then begin
    BufSize := MAX_PATH;
    RegQueryValueEx(hReg, 'System Up Time', nil, @dwType, @pBuf, @BufSize);
    if (dwType = REG_SZ) then ShowMessage(string(PChar(@pBuf)));
    RegCloseKey(hReg);
  end else
    showmessage('unsuccessful');
end;


? (doesn't work, but maybe with some tweaking..)

Some thing like this below, but if that worked, that would only give us the key to open, we would still need to know the name  of the Value, in order to extract its data

procedure TForm1.SpeedButton1Click(Sender: TObject);
var
  hReg: HKEY;
  dwType, BufSize: DWORD;
  pBuf: array[0..MAX_PATH] of Char;
begin
  if RegOpenKey(HKEY_PERFORMANCE_DATA, '\compname\System\System Up Time', hReg) = ERROR_SUCCESS then begin
    BufSize := MAX_PATH;
    RegQueryValueEx(hReg, 'NAME OF VALUE HERE', nil, @dwType, @pBuf, @BufSize);
    if (dwType = REG_SZ) then ShowMessage(string(PChar(@pBuf)));
    RegCloseKey(hReg);
  end else
    showmessage('unsuccessful');
end;


unless its this...


procedure TForm1.SpeedButton1Click(Sender: TObject);
var
  hReg: HKEY;
  dwType, BufSize: DWORD;
  pBuf: array[0..MAX_PATH] of Char;
begin
  if RegOpenKey(HKEY_PERFORMANCE_DATA, '\compname\System', hReg) = ERROR_SUCCESS then begin
    BufSize := MAX_PATH;
    RegQueryValueEx(hReg, 'System Up Time', nil, @dwType, @pBuf, @BufSize);
    if (dwType = REG_SZ) then ShowMessage(string(PChar(@pBuf)));
    RegCloseKey(hReg);
  end else
    showmessage('unsuccessful');

Shane
end;

Author

Commented:
2nd one doesn't work..
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
That works well Lee_Nover, only one thing that bothers me about it - it seems to return the uptime 10 seconds shorter than what it should be (I'm comparing this uptime method you gave to the Windows Uptime application ( http://www.rundegren.com/software/windowsuptime/ ) which seems to be the standard.

Author

Commented:
(Sorry to be picky but my users are.. well.. even more picky!)
just tested .. both display the same value .. with an offset of ~0,5 sec because of the apps timers being out of sync
and are there any records of that app displaying uptimes beyond 50 days ?

Author

Commented:
Hmm.. is there any reason why you use

dt:=IncMilliSecond(dt, GetTickCount);

instead of

dt:=GetTickCount;

?
ofcourse .. left of the decimal are days ! check what IncMillisecond does

Author

Commented:
Sorry, I was just trying to work out why the uptimes differ for this app and Windows Uptime for my computer.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.