티스토리 뷰

반응형

프로그래밍 하다보면 로그기록 등을 위하여 PC명칭 OS버젼 MAC주소 HDD볼륨을 알아야 할 때가 있습니다.

막상 필요할 때 만들려 하면 생각도 안 나고...

 

내 PC에서는 잘 동작이 되는 데,

다른 PC는 어떤지는 잘 모르겠습니다.

 

그래도 비슷하지 않을까 싶어 올려봅니다.

혹 사용하여 보시고 의견이 있으면 댓글 올려 주세요.

 


[ 필수 uses 절 ]

Winapi

  

 

[ 함수 정의 ]

  function GetMyPcName()  : string; // PC명칭 구하기
  function GetOSVersion() : String; // OS버젼 구하기
  function GetMACAdress() : string; // MAC 구하기
  function GetHDDVolume() : string; // HDD 볼륨 구하기

 

 

[ 함수 본문 ]  PC 명칭 구하기

//
// PC명칭 구하기
// 함수값 반환
function GetMyPcName(): string;
var
  UserName: string;
  UserNameLen: Dword;
begin
  UserNameLen := 50;
  SetLength(UserName, UserNameLen);
  If GetUserName(PChar(UserName), UserNameLen) then
    Result := Copy(UserName, 1, UserNameLen - 1)
  else
    Result := 'Unknown';
end;

 

[ 함수 본문 ]  PC 윈도우버젼 구하기

//
// 사용중인 PC 윈도우버젼 구하기
// 함수값 반환
function GetOSVersion(): String;
var
  osInfo: OSVERSIONINFOEX;
  ver: string;
begin

  try
    osInfo.dwOSVersionInfoSize := SizeOf(OSVERSIONINFOEX);
    if GetVersionEx(osInfo) then
    begin
      with osInfo do
      begin
        case dwPlatformId of
          VER_PLATFORM_WIN32_NT: // Windows NT/2000/XP/7/8/10
            begin
              case dwMajorVersion of
                0 .. 4:
                  ver := 'Windows NT';
                5:
                  begin
                    if (dwMinorVersion = 0) then
                      ver := 'Windows 2000'
                    else if (dwMinorVersion = 1) then
                      ver := 'Windows XP'
                    else if (dwMinorVersion = 2) then
                    begin
                      if GetSystemMetrics(SM_SERVERR2) <> 0 then
                        ver := 'Windows Server 2003 R2'
                      else if GetSystemMetrics(SM_SERVERR2) = 0 then
                        ver := 'Windows Server 2003';
                    end;
                  end;

                6:
                  begin
                    if (dwMinorVersion = 0) then
                    begin
                      if (wProductType = VER_NT_WORKSTATION) then
                        ver := 'Windows Vista'
                      else if (wProductType <> VER_NT_WORKSTATION) then
                        ver := 'Windows 2008'
                    end
                    else if (dwMinorVersion = 1) then
                    begin
                      if (wProductType = VER_NT_WORKSTATION) then
                        ver := 'Windows 7'
                      else if (wProductType <> VER_NT_WORKSTATION) then
                        ver := 'Windows 2008 R2'
                    end
                    else if (dwMinorVersion = 2) then
                    begin
                      if (wProductType = VER_NT_WORKSTATION) then
                        ver := 'Windows 8'
                      else if (wProductType <> VER_NT_WORKSTATION) then
                        ver := 'Windows 2012'
                    end
                    else if (dwMinorVersion = 3) then
                    begin
                      if (wProductType = VER_NT_WORKSTATION) then
                        ver := 'Windows 8.1'
                      else if (wProductType <> VER_NT_WORKSTATION) then
                        ver := 'Windows 2012 R2'
                    end;
                  end;

                10:
                  begin
                    if (dwMinorVersion = 0) then
                    begin
                      if (wProductType = VER_NT_WORKSTATION) then
                        ver := 'Windows 10'
                      else if (wProductType <> VER_NT_WORKSTATION) then
                        ver := 'Windows 2016 Technical Preview';
                    end;

                  end;
              end;
            end;

          VER_PLATFORM_WIN32_WINDOWS: // Windows 9x/ME
            begin
              if (dwMajorVersion = 4) and (dwMinorVersion = 0) then
                ver := 'Windows 95'
              else if (dwMajorVersion = 4) and (dwMinorVersion = 10) then
              begin
                if szCSDVersion[1] = 'A' then
                  ver := 'Windows 98SE'
                else
                  ver := 'Windows 98';
              end
              else if (dwMajorVersion = 4) and (dwMinorVersion = 90) then
                ver := 'Windows ME'
              else
                ver := 'Unknown';
            end;

        else
          ver := 'Unknown';
        end; // case

      end; // with

    end
    else
      ver := 'Unknown';

  except
    ver := 'Get Error';

  end;

  Result := ver;

end;

 

[ 함수 본문 ] MAC 주소 구하기

//
// MAC 주소 구하기
// 함수값 반환
function GetMACAdress: string;
var
  NCB: PNCB;
  Adapter: PAdapterStatus;

  URetCode: PChar;
  RetCode: AnsiChar;
  I: Integer;
  Lenum: PlanaEnum;
  _SystemID: string;
  TMPSTR: string;
begin
  Result := '';
  _SystemID := '';
  Getmem(NCB, SizeOf(TNCB));
  Fillchar(NCB^, SizeOf(TNCB), 0);

  Getmem(Lenum, SizeOf(TLanaEnum));
  Fillchar(Lenum^, SizeOf(TLanaEnum), 0);

  Getmem(Adapter, SizeOf(TAdapterStatus));
  Fillchar(Adapter^, SizeOf(TAdapterStatus), 0);

  Lenum.Length := chr(0);
  NCB.ncb_command := chr(NCBENUM);
  NCB.ncb_buffer := Pointer(Lenum);
  NCB.ncb_length := SizeOf(Lenum);
  RetCode := Netbios(NCB);

  I := 0;
  repeat
    Fillchar(NCB^, SizeOf(TNCB), 0);
    NCB.ncb_command := chr(NCBRESET);
    NCB.ncb_lana_num := Lenum.lana[I];
    RetCode := Netbios(NCB);

    Fillchar(NCB^, SizeOf(TNCB), 0);
    NCB.ncb_command := chr(NCBASTAT);
    NCB.ncb_lana_num := Lenum.lana[I];
    // Must be 16
    NCB.ncb_callname := '*               ';

    NCB.ncb_buffer := Pointer(Adapter);

    NCB.ncb_length := SizeOf(TAdapterStatus);
    RetCode := Netbios(NCB);
    // ---- calc _systemId from mac-address[2-5] XOR mac-address[1]...
    if (RetCode = chr(0)) or (RetCode = chr(6)) then
    begin
      _SystemID := IntToHex(Ord(Adapter.adapter_address[0]), 2) + '-' + IntToHex(Ord(Adapter.adapter_address[1]), 2) + '-' +
        IntToHex(Ord(Adapter.adapter_address[2]), 2) + '-' + IntToHex(Ord(Adapter.adapter_address[3]), 2) + '-' + IntToHex(Ord(Adapter.adapter_address[4]), 2) +
        '-' + IntToHex(Ord(Adapter.adapter_address[5]), 2);
    end;
    Inc(I);
  until (I >= Ord(Lenum.Length)) or (_SystemID <> '00-00-00-00-00-00');
  FreeMem(NCB);
  FreeMem(Adapter);
  FreeMem(Lenum);
  GetMACAdress := _SystemID;
end;

 

[ 함수 본문 ]  HDD 볼률 구하기

//
// HDD 볼륨 구하기
function GetHDDVolume() : string;
var
  MC, FL, PDW : DWord;
begin

  GetVolumeInformation(nil, nil, 0, @PDW, MC, FL, nil, 0);

  Result := IntToStr(PDW);

end;
반응형
댓글