프로그래밍/델파이

부팅된 시각 알아내기

채윤아빠 2008. 2. 25. 11:29
728x90
반응형
다음과 같은 선언부를 별도의 유닛으로 만들거나, 관련 부분에 추가해 줍니다.
type
  NET_API_STATUS = DWORD;

  _STAT_SERVER_0=record
    sts0_start: DWORD; // the time when statistics collection started
    sts0_fopens: DWORD; // number of times a file is opened on a server
    sts0_devopens: DWORD; // number of times a server device is opened
    sts0_jobsqueued: DWORD; // number of server print jobs spooled
    sts0_sopens: DWORD; // number of times the server session started
    sts0_stimedout: DWORD; // number of times the server session automatically disconnected
    sts0_serrorout: DWORD; // number of times the server sessions failed with an error
    sts0_pwerrors: DWORD; // number of server password violations
    sts0_permerrors: DWORD; // number of server access permission errors
    sts0_syserrors: DWORD; // number of server system errors
    sts0_bytessent_low: DWORD; // number of server bytes sent to the network
    sts0_bytessent_high: DWORD; // number of server bytes sent to the network
    sts0_bytesrcvd_low: DWORD; // number of server bytes received from the network
    sts0_bytesrcvd_high: DWORD; // number of server bytes received from the network
    sts0_avresponse: DWORD; // average server response time (in milliseconds)
    sts0_reqbufneed: DWORD; // number of times the server required a request buffer but failed to allocate one
    sts0_bigbufneed: DWORD; // number of times the server required a big buffer but failed to allocate one
  end;

  STAT_SERVER_0 = _STAT_SERVER_0;
  PSTAT_SERVER_0 = ^_STAT_SERVER_0;
  LPSTAT_SERVER_0 = ^_STAT_SERVER_0;


  _STAT_WORKSTATION_0 = record
    StatisticsStartTime: int64; // Specifies the time statistics collection started
    BytesReceived: int64; // total number of bytes received by the workstation
    SmbsReceived: int64; // total number of server message blocks (SMBs) received by the workstation
    PagingReadBytesRequested: int64; // total number of bytes that have been read by paging I/O requests
    NonPagingReadBytesRequested: int64; // total number of bytes that have been read by non-paging I/O requests
    CacheReadBytesRequested: int64; // number of bytes that have been read by cache I/O requests
    NetworkReadBytesRequested: int64; // total amount of bytes that have been read by disk I/O requests
    BytesTransmitted: int64; // total number of bytes transmitted by the workstation
    SmbsTransmitted: int64; // total number of SMBs transmitted by the workstation
    PagingWriteBytesRequested: int64; // total number of bytes that have been written by paging I/O requests
    NonPagingWriteBytesRequested: int64; // total number of bytes that have been written by non-paging I/O requests
    CacheWriteBytesRequested: int64; // total number of bytes that have been written by cache I/O requests
    NetworkWriteBytesRequested: int64; // total number of bytes that have been written by disk I/O requests
    InitiallyFailedOperations: DWORD; // total number of network operations that failed to begin
    FailedCompletionOperations: DWORD; // total number of network operations that failed to complete
    ReadOperations: DWORD; // total number of read operations initiated by the workstation
    RandomReadOperations: DWORD; // total number of random access reads initiated by the workstation
    ReadSmbs: DWORD; // total number of read requests the workstation has sent to servers
    LargeReadSmbs: DWORD; // total number of read requests the workstation has sent to servers that are greater than twice the size of the server's negotiated buffer size
    SmallReadSmbs: DWORD; // total number of read requests the workstation has sent to servers that are less than 1/4 of the size of the server's negotiated buffer size
    WriteOperations: DWORD; // total number of write operations initiated by the workstation
    RandomWriteOperations: DWORD; // total number of random access writes initiated by the workstation
    WriteSmbs: DWORD; // total number of write requests the workstation has sent to servers
    LargeWriteSmbs: DWORD; // total number of write requests the workstation has sent to servers that are greater than twice the size of the server's negotiated buffer size
    SmallWriteSmbs: DWORD; // total number of write requests the workstation has sent to servers that are less than 1/4 of the size of the server's negotiated buffer size
    RawReadsDenied: DWORD; // total number of raw read requests made by the workstation that have been denied (Windows XP/2000:  This member is not supported)
    RawWritesDenied: DWORD; // total number of raw write requests made by the workstation that have been denied (Windows XP/2000:  This member is not supported)
    NetworkErrors: DWORD; // total number of network errors received by the workstation
    Sessions: DWORD; // total number of workstation sessions that were established
    FailedSessions: DWORD; // number of times the workstation attempted to create a session but failed
    Reconnects: DWORD; // total number of connections that have failed
    CoreConnects: DWORD; // total number of connections to servers supporting the PCNET dialect that have succeeded
    Lanman20Connects: DWORD; // total number of connections to servers supporting the LanManager 2.0 dialect that have succeeded
    Lanman21Connects: DWORD; // total number of connections to servers supporting the LanManager 2.1 dialect that have succeeded
    LanmanNtConnects: DWORD; // total number of connections to servers supporting the NTLM dialect that have succeeded
    ServerDisconnects: DWORD; // number of times the workstation was disconnected by a network server
    HungSessions: DWORD; // total number of sessions that have expired on the workstation
    UseCount: DWORD; // total number of network connections established by the workstation
    FailedUseCount: DWORD; // total number of failed network connections for the workstation
    CurrentCommands: DWORD; // number of current requests that have not been completed
  end;

  STAT_WORKSTATION_0 = _STAT_WORKSTATION_0;
  PSTAT_WORKSTATION_0 = ^_STAT_WORKSTATION_0;
  LPSTAT_WORKSTATION_0 = ^_STAT_WORKSTATION_0;


  LPBYTE = ^BYTE;

  function NetApiBufferFree(
    lpBuffer: Pointer
  ): NET_API_STATUS; stdcall;

  function  NetStatisticsGet(
    erver: PWideChar;
    ervice: PWideChar;
    level : DWORD;
    options : DWORD;
    out pBuf: Pointer
  ) : NET_API_STATUS; stdcall;

  function NetApiBufferSize(pBuffer: Pointer; out dwByteCount: DWORD): NET_API_STATUS; stdcall;

implementation

const
  NetAPI32 = 'NetAPI32.dll';

  function NetApiBufferFree; external NetAPI32 name 'NetApiBufferFree';
  function NetApiBufferSize; external NetAPI32 name 'NetApiBufferSize';
  function NetStatisticsGet; external NetAPI32 name 'NetStatisticsGet';

아래와 같이 코드를 작성하면 PC가 부팅된 시각을 얻을 수 있습니다.
procedure TForm1.Button1Click(Sender: TObject);
var
  ret : NET_API_STATUS;
  bufPtr: Pointer;
  SW: STAT_WORKSTATION_0;
  dwSize: DWORD;
begin
  ret := NetStatisticsGet(nil, 'LanmanWorkstation', 0, 0, bufPtr);  // LanmanWorkstation
  if (ret = 0) then
  begin
    CopyMemory(@SW, bufPtr, SizeOf(STAT_WORKSTATION_0));
    NetApiBufferSize(bufPtr, dwSize);
    ShowMessage('부팅된 시각 : ' + FormatDateTime('yyyy-mm-dd hh:nn:ss', FileTimeToDateTime(TFileTime(SW.StatisticsStartTime))));
    ret := NetApiBufferFree(bufPtr);
  end;
end;