C++ 取得一些Windows系統資訊 (Getting Some Windows System Information)

本文示範如何以C++取得系統基本資訊

This article showing how to use C++ getting some Windows system information.

//------- in cpp file ---------------
#define NO_WIN32_LEAN_AND_MEAN  //for 函數win_sys_path
#include <windows.h>            //for 函數getwinver()
//-----------------------------------
//------ 得到本機目前之使用者帳號名稱 (Getting Windows user name)
String getosusername()
{
  char UName[50];
  unsigned long Size = sizeof(UName);
  GetUserName(UName, &Size);
  return (String)UName;
}
//------ 得到本機之電腦名稱 (Getting computer name)
String getcomputername()
{
  char Name[MAX_COMPUTERNAME_LENGTH + 1];
  unsigned long Size = sizeof(Name);
  GetComputerName(Name, &Size);
  return (String)Name;
}
//------ 取得系統路徑 (Getting Windows system paths)
String getwinsyspath(int CSIDL_STR)
{
  //SHGetSpecialFolder三個參數。
  //第一個參數是HWND,它指定了"所有者視窗":在調用這個函數時可能出現的對話方塊或訊息方塊。
  //第二個參數是一個整數id,決定哪個目錄是待查找目錄,它的取值可能是:
  //最後一個參數是pidl位址,SHGetSpecialFolderLocation把地址寫到pidl。
  //注意: 有些目錄是空的。有些特定的目錄在這個文件系統上並沒有一個相應的目錄。
  /*
  CSIDL_STR = CSIDL_BITBUCKET                 //回收桶
  CSIDL_STR = CSIDL_CONTROLS                  //控制面板
  CSIDL_STR = CSIDL_DESKTOP                   //使用者(虛擬)桌面
  CSIDL_STR = CSIDL_DESKTOPDIRECTORY          //使用者(實際)桌面
  CSIDL_STR = CSIDL_COMMON_DESKTOPDIRECTORY   //公用(實際)桌面
  CSIDL_STR = CSIDL_DRIVES                    //我的電腦
  CSIDL_STR = CSIDL_FONTS     //字體目錄
  CSIDL_STR = CSIDL_NETHOOD   //網路芳鄰(實際)
  CSIDL_STR = CSIDL_NETWORK   //網路芳鄰(虛擬)
  CSIDL_STR = CSIDL_PERSONAL  //我的文件
  CSIDL_STR = CSIDL_PRINTERS  //印表機
  CSIDL_STR = CSIDL_PROGRAMS  //程式集
  CSIDL_STR = CSIDL_RECENT    //最近的文件
  CSIDL_STR = CSIDL_SENDTO    //'發送到'的文件
  CSIDL_STR = CSIDL_STARTMENU //開始功能表清單
  CSIDL_STR = CSIDL_STARTUP   //啟動的資料夾
  CSIDL_STR = CSIDL_TEMPLATES //暫時文件
  */
  LPITEMIDLIST pidl;
  LPMALLOC pShellMalloc;
  char szDir[MAX_PATH];
  String result;
  if(SUCCEEDED(SHGetMalloc(&pShellMalloc))){
    if(SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_STR, &pidl))){
      if(SHGetPathFromIDList(pidl, szDir)){
        result=AnsiString(szDir);
      }
      pShellMalloc->Free(pidl);
    }
    pShellMalloc->Release();
  }
  return result;
}
//------ 取得Windows版本 (Getting Windows version)
String getwinver()
{
  int a,b;
  OSVERSIONINFO osvi;
  ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
  osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  GetVersionEx(&osvi);
  a=osvi.dwMajorVersion;    //主版本
  b=osvi.dwMinorVersion;    //次版本
  String vv=(String)a+"."+(String)b;
   String uu;
   if(a==10 && b==0)
     uu="Win 10 or Win 2016 Technical Preview";
   else
     if(a==6 && b==3)
  uu="Win 8.1 or Win Server 2012 R2";
  else
  if(a==6 && b==2)
    uu="Win 8 or Win Server 2012";
  else
  if(a==6 && b==1)
    uu="Win 7 or Win Server 2008 R2";
  else
  if(a==6 && b==0)
    uu="Win Vista or Win Server 2008";
  else
  if(a==5 && b==2)
    uu="Win Server 2003 R2 or Win Home Server or Win Server 2003 or Win XP Pro x64 Edition";
  else
  if(a==5 & &b==0)
    uu="Win 2000";
  else
  if(a==5 && b==1)
    uu="Win XP";
  else
  if(a==4 && b==0)
    uu="Win NT";
  else
  if(a==4 && b==10)
    uu="Win 98";
  else
  if(a==4 && b==90)
    uu="Win ME";
  else
    uu="(未知)";
  return uu+" -- Ver:"+vv;
}

Label9->Caption=getwinver();
Label10->Caption=getosusername();
Label12->Caption=getcomputername();
Label14->Caption=getwinsyspath(CSIDL_DESKTOPDIRECTORY);                //目前使用者桌面路徑
Label13->Caption=getwinsyspath(CSIDL_COMMON_DESKTOPDIRECTORY);//共用桌面路徑

SomeSystemInfo

Tagged: ,

1 thoughts on “C++ 取得一些Windows系統資訊 (Getting Some Windows System Information)

  1. […] 本文示范如何使用C++取得Windows系统基本资讯,程式开发IDE为BCB。 C++取得Windows系统基本资讯 本文 […]

發表留言