線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:3551
推到 Plurk!
推到 Facebook!

請問如何偵測開機時間有多久了!!

答題得分者是:Stallion
mitlion0807
一般會員


發表:1
回覆:0
積分:0
註冊:2005-08-24

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-09-05 01:13:16 IP:220.142.xxx.xxx 未訂閱
請問我要如何讓DELPHI偵測這台電腦已經開機多久了??
Stallion
版主


發表:52
回覆:1600
積分:1995
註冊:2004-09-15

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-09-05 01:37:37 IP:211.22.xxx.xxx 未訂閱
用下面這個WINAPI吧~ The GetTickCount function retrieves the number of milliseconds that have elapsed since Windows was started. DWORD GetTickCount(VOID) Parameters This function has no parameters. Return Values If the function succeeds, the return value is the number of milliseconds that have elapsed since Windows was started. Remarks The elapsed time is stored as a DWORD value. Therefore, the time will wrap around to zero if Windows is run continuously for 49.7 days. Windows NT: To obtain the time elapsed since the computer was started, look up the System Up Time counter in the performance data in the registry key HKEY_PERFORMANCE_DATA. The value returned is an 8 byte value. ---------------------------------------------- We will either find a way, or make one. -Hannibal -。
chris_shieh
高階會員


發表:46
回覆:308
積分:240
註冊:2004-04-26

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-09-05 14:43:37 IP:203.70.xxx.xxx 未訂閱
http://delphi.ktop.com.tw/topic.php?topic_id=66286    真正要得到這個訊息 根據Delphi 所附的Windows SDK Help 可以去讀取registry, Windows會一直去update HKEY_PERFORMANCE_DATA 內的 資料    可以參考這裡有完整的函式與範例 http://www.delphipraxis.net/topic16973.html    
//////////////////////////////////////////////////////////////////////////////// 
// 
//  GetSystemUpTimeNt() 
// 
//    Uses the registry interface to get the value of the performance counter 
//    '\\localhost\System\System Up Time' in milliseconds (returns 0 on error). 
//     function GetSystemUpTimeNt(): Int64; 
{$IFDEF WIN32} 
type 
  PPerfDataBlock = ^TPerfDataBlock; 
  TPerfDataBlock = packed record 
    Signature       : array [0..3] of WCHAR; 
    LittleEndian    : DWORD; 
    Version         : DWORD; 
    Revision        : DWORD; 
    TotalByteLength : DWORD; 
    HeaderLength    : DWORD; 
    NumObjectTypes  : DWORD; 
    DefaultObject   : DWORD; 
    SystemTime      : SYSTEMTIME; 
    PerfTime        : LARGE_INTEGER; 
    PerfFreq        : LARGE_INTEGER; 
    PerfTime100nSec : LARGE_INTEGER; 
    SystemNameLength: DWORD; 
    SystemNameOffset: DWORD; 
  end; 
  PPerfObjectType = ^TPerfObjectType; 
  TPerfObjectType = packed record 
    TotalByteLength     : DWORD; 
    DefinitionLength    : DWORD; 
    HeaderLength        : DWORD; 
    ObjectNameTitleIndex: DWORD; 
    ObjectNameTitle     : LPWSTR; 
    ObjectHelpTitleIndex: DWORD; 
    ObjectHelpTitle     : LPWSTR; 
    DetailLevel         : DWORD; 
    NumCounters         : DWORD; 
    DefaultCounter      : DWORD; 
    NumInstances        : DWORD; 
    CodePage            : DWORD; 
    PerfTime            : LARGE_INTEGER; 
    PerfFreq            : LARGE_INTEGER; 
  end; 
  PPerfCounterDefinition = ^TPerfCounterDefinition; 
  TPerfCounterDefinition = packed record 
    ByteLength           : DWORD; 
    CounterNameTitleIndex: DWORD; 
    CounterNameTitle     : LPWSTR; 
    CounterHelpTitleIndex: DWORD; 
    CounterHelpTitle     : LPWSTR; 
    DefaultScale         : DWORD; 
    DetailLevel          : DWORD; 
    CounterType          : DWORD; 
    CounterSize          : DWORD; 
    CounterOffset        : DWORD; 
  end; 
  PPerfInstanceDefinition = ^TPerfInstanceDefinition; 
  TPerfInstanceDefinition = packed record 
    ByteLength            : DWORD; 
    ParentObjectTitleIndex: DWORD; 
    ParentObjectInstance  : DWORD; 
    UniqueID              : DWORD; 
    NameOffset            : DWORD; 
    NameLength            : DWORD; 
  end; 
  PLARGE_INTEGER = ^LARGE_INTEGER; 
const 
  PERF_SIZE_LARGE      = $00000100; 
  PERF_TYPE_COUNTER    = $00000400; 
  PERF_COUNTER_ELAPSED = $00040000; 
  PERF_OBJECT_TIMER    = $00200000; 
  PERF_DISPLAY_SECONDS = $30000000; 
  PERF_ELAPSED_TIME    = PERF_SIZE_LARGE or PERF_TYPE_COUNTER or 
                         PERF_COUNTER_ELAPSED or PERF_OBJECT_TIMER or 
                         PERF_DISPLAY_SECONDS; 
  PERF_NO_INSTANCES = DWORD(-1); 
var 
  ValSize: DWORD; 
  Counter: PChar; 
  CurrIdx: PChar; 
  CurrStr: PChar; 
  CntrStr: PChar; 
  CntrSys: DWORD; 
  CntrSUT: DWORD; 
  QrySize: DWORD; 
  QryData: PPerfDataBlock; 
  CurrObj: PPerfObjectType; 
  ObjLoop: DWORD; 
  CurrDef: PPerfCounterDefinition; 
  DefLoop: DWORD; 
  ObjInst: PPerfInstanceDefinition; 
  CntrVal: PLARGE_INTEGER; 
{$ENDIF} 
begin 
  Result := 0;  // indicates failure 
{$IFDEF WIN32} 
  ValSize := 0; 
  if (RegQueryValueEx(HKEY_PERFORMANCE_DATA, 'Counter 009', nil, nil, nil, 
    @ValSize) = ERROR_SUCCESS) then 
  try 
    Inc(ValSize, 1024); 
    Counter := GetMemory(ValSize); 
    if (Counter <> nil) then 
    try 
      if (RegQueryValueEx(HKEY_PERFORMANCE_DATA, 'Counter 009', nil, nil, 
        PByte(Counter), @ValSize) = ERROR_SUCCESS) then 
      begin 
        CntrStr := nil; 
        CntrSys := 0; 
        CntrSUT := 0; 
        CurrIdx := Counter; 
        while (CurrIdx[0] <> #0) do 
        begin 
          CurrStr := PChar(@CurrIdx[StrLen(CurrIdx)   1]); 
          if ((CntrSys = 0) and (StrComp(CurrStr, 'System') = 0)) then 
          begin 
            CntrStr := CurrIdx; 
            CntrSys := StrToInt(string(CurrIdx)); 
            if (CntrSUT <> 0) then 
              Break; 
          end; 
          if ((CntrSUT = 0) and (StrComp(CurrStr, 'System Up Time') = 0)) then 
          begin 
            CntrSUT := StrToInt(string(CurrIdx)); 
            if (CntrSys <> 0) then 
              Break; 
          end; 
          CurrIdx := PChar(@CurrStr[StrLen(CurrStr)   1]); 
        end; 
        if ((CntrStr <> nil) and (CntrSys <> 0) and (CntrSUT <> 0)) then 
        begin 
          QrySize := 0; 
          QryData := nil; 
          try 
            repeat 
              Inc(QrySize, 4096); 
              QryData := ReallocMemory(QryData, QrySize); 
              if (QryData = nil) then 
                Break; 
              ValSize := QrySize; 
            until (RegQueryValueEx(HKEY_PERFORMANCE_DATA, CntrStr, nil, nil, 
              PByte(QryData), @ValSize) <> ERROR_MORE_DATA); 
            if ((ValSize > 0) and (QryData <> nil)) then 
              if (QryData.Signature = 'PERF') then 
              begin 
                CurrObj := PPerfObjectType(Cardinal(QryData)   
                  QryData.HeaderLength); 
                for ObjLoop := 1 to QryData.NumObjectTypes do 
                begin 
                  if ((CurrObj.ObjectNameTitleIndex = CntrSys) and 
                    (CurrObj.NumInstances > 0) and 
                    (CurrObj.PerfFreq.QuadPart >= 1000)) then 
                  begin 
                    CurrDef := PPerfCounterDefinition(Cardinal(CurrObj)   
                      CurrObj.HeaderLength); 
                    for DefLoop := 1 to CurrObj.NumCounters do 
                    begin 
                      if (CurrDef.CounterNameTitleIndex = CntrSUT) and 
                        (CurrDef.CounterType = PERF_ELAPSED_TIME) then 
                      begin 
                        if (CurrObj.NumInstances = PERF_NO_INSTANCES) then 
                          CntrVal := PLARGE_INTEGER(Cardinal(CurrObj)   
                             CurrObj.DefinitionLength   CurrDef.CounterOffset) 
                        else 
                        begin 
                          // first instance 
                          ObjInst := PPerfInstanceDefinition(Cardinal(CurrObj)   
                            CurrObj.DefinitionLength); 
                          CntrVal := PLARGE_INTEGER(Cardinal(ObjInst)   
                             ObjInst.ByteLength   CurrDef.CounterOffset); 
                        end; 
                        Result := 
                          (CurrObj.PerfTime.QuadPart - CntrVal.QuadPart) div 
                          (CurrObj.PerfFreq.QuadPart div 1000);  // milliseconds 
                        Break; 
                      end; 
                      CurrDef := PPerfCounterDefinition(Cardinal(CurrDef)   
                        CurrDef.ByteLength); 
                    end; 
                    Break; 
                  end; 
                  CurrObj := PPerfObjectType(Cardinal(CurrObj)   
                    CurrObj.TotalByteLength); 
                end; 
              end; 
          finally 
            if (QryData <> nil) then 
              FreeMemory(QryData); 
          end; 
        end; 
      end; 
    finally 
      FreeMemory(Counter); 
    end; 
  finally 
    RegCloseKey(HKEY_PERFORMANCE_DATA); 
  end; 
{$ENDIF} 
end;     //////////////////////////////////////////////////////////////////////////////// 
// 
//  GetSystemUpTime9x() 
// 
//    Uses GetTickCount() to get the 'System Up Time' in milliseconds. 
//    Will wrap around to zero if the system is run continuously for 49.7 days! 
//     function GetSystemUpTime9x(): Int64; 
begin 
{$IFDEF WIN32} 
  Result := GetTickCount(); 
{$ELSE} 
  Result := 0; 
{$ENDIF} 
end;     //////////////////////////////////////////////////////////////////////////////// 
// 
//  GetSystemUpTime() 
// 
//    Wrapper for GetSystemUpTimeNt() and GetSystemUpTime9x() 
//     function GetSystemUpTime(): Int64; 
begin 
  Result := GetSystemUpTimeNt(); 
  if (Result = 0) then 
    Result := GetSystemUpTime9x(); 
end;     //////////////////////////////////////////////////////////////////////////////// 
// Sample     procedure TForm1.Button1Click(Sender: TObject); 
var 
  UpTimeNt: Int64; 
  UpTime9x: Int64; 
begin 
  UpTime9x := GetSystemUpTime9x(); 
  UpTimeNt := GetSystemUpTimeNt(); 
  ShowMessage(Format('GetTickCount: %d day(s) %2.2d:%2.2d:%2.2d.%3.3d'#10   
    'Perf-Counter: %d day(s) %2.2d:%2.2d:%2.2d.%3.3d', [UpTime9x div 86400000, 
    UpTime9x mod 86400000 div 3600000, UpTime9x mod 3600000 div 60000, 
    UpTime9x mod 60000 div 1000, UpTime9x mod 1000, UpTimeNt div 86400000, 
    UpTimeNt mod 86400000 div 3600000, UpTimeNt mod 3600000 div 60000, 
    UpTimeNt mod 60000 div 1000, UpTimeNt mod 1000])); 
end;
@瞭解越多.懂得越少@
christie
資深會員


發表:30
回覆:299
積分:475
註冊:2005-03-25

發送簡訊給我
#4 引用回覆 回覆 發表時間:2007-11-09 13:47:49 IP:203.73.xxx.xxx 未訂閱
插花一下
function UpTime: string;
const
ticksperday: Integer = 1000 * 60 * 60 * 24;
ticksperhour: Integer = 1000 * 60 * 60;
ticksperminute: Integer = 1000 * 60;
tickspersecond: Integer = 1000;
var
t: Longword;
d, h, m, s: Integer;
begin
t := GetTickCount;
d := t div ticksperday;
Dec(t, d * ticksperday);
h := t div ticksperhour;
Dec(t, h * ticksperhour);
m := t div ticksperminute;
Dec(t, m * ticksperminute);
s := t div tickspersecond;
Result := 'Up Time: ' IntToStr(d) ' Days ' IntToStr(h) ' Hours ' IntToStr(m)
' Minutes ' IntToStr(s) ' Seconds';
end;
------
What do we live for if not to make life less difficult for each other?
系統時間:2024-04-25 22:24:30
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!