<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Windows API 中的系統函數 > 原文: [http://zetcode.com/gui/winapi/system/](http://zetcode.com/gui/winapi/system/) 在 Windows API 教程的這一部分中,我們介紹了系統函數。 系統函數接收有關系統的信息并以各種方式與系統通信。 ## 屏幕大小 `GetSystemMetrics()`函數檢索各種系統指標和系統配置設置。 `screen_size.c` ```c #include <windows.h> #include <wchar.h> #pragma comment(lib, "user32.lib") int wmain(void) { int x = GetSystemMetrics(SM_CXSCREEN); int y = GetSystemMetrics(SM_CYSCREEN); wprintf(L"The screen size is: %dx%d\n", x, y); return 0; } ``` 該代碼示例將屏幕大小輸出到控制臺。 ```c #pragma comment(lib, "user32.lib") ``` 該程序需要`user32.lib`庫進行編譯。 ```c int x = GetSystemMetrics(SM_CXSCREEN); int y = GetSystemMetrics(SM_CYSCREEN); ``` 我們用`GetSystemMetrics()`確定屏幕分辨率。 ```c C:\Users\Jano\Documents\Pelles C Projects\system\ScreenSize>ScreenSize.exe The screen size is: 1280x800 ``` 屏幕大小為`1280x800`。 ## 鎖定工作站 `LockWorkStation()`鎖定工作站的顯示。 `lock_workstation.c` ```c #include <windows.h> #include <wchar.h> #pragma comment(lib, "user32.lib") int wmain(void) { int r = LockWorkStation(); if( r == 0 ) { wprintf(L"LockWorkStation() failed %d\n", GetLastError()); return 1; } return 0; } ``` 該程序需要`user32.lib`進行編譯。 ## 電腦名稱 `GetComputerNameEx()`函數檢索與本地計算機關聯的 NetBIOS 或 DNS 名稱。 名稱是在系統啟動時建立的。 `computer_name.c` ```c #include <windows.h> #include <wchar.h> int wmain(void) { wchar_t computerName[MAX_COMPUTERNAME_LENGTH + 1]; DWORD size = sizeof(computerName) / sizeof(computerName[0]); int r = GetComputerNameW(computerName, &size); if (r == 0) { wprintf(L"Failed to get computer name %ld", GetLastError()); return 1; } wprintf(L"Computer name: %ls\n", computerName); return 0; } ``` 該示例將計算機名稱打印到控制臺。 ```c wchar_t computerName[MAX_COMPUTERNAME_LENGTH + 1]; ``` `MAX_COMPUTERNAME_LENGTH`常數確定計算機名稱的最大長度。 ```c int r = GetComputerNameW(computerName, &size); ``` 我們使用`GetComputerNameW()`函數獲得計算機的名稱。 名稱存儲在`computerName`數組中。 ```c C:\Users\Jano\Documents\Pelles C Projects\system\ComputerName>ComputerName.exe Computer name: ANDROMEDA ``` 我們運行代碼示例。 ## 用戶名 `GetUserNameW()`函數返回用戶名。 `username.c` ```c #include <windows.h> #include <Lmcons.h> #include <wchar.h> int wmain(void) { wchar_t username[UNLEN+1]; DWORD len = sizeof(username) / sizeof(wchar_t); int r = GetUserNameW(username, &len); if (r == 0) { wprintf(L"Failed to get username %ld", GetLastError()); return 1; } wprintf(L"User name: %ls\n", username); return 0; } ``` 該示例將用戶名打印到控制臺。 ```c #include <Lmcons.h> ``` `Lmcons.h`文件具有`ULEN`常量的定義。 ```c wchar_t username[UNLEN+1]; ``` `ULEN`常量定義用戶名的最大長度。 ```c int r = GetUserNameW(username, &len); ``` `GetUserNameW()`函數檢索用戶名并將其存儲到`username`數組中。 ```c C:\Users\Jano\Documents\Pelles C Projects\system\Username>username.exe User name: Jano ``` 這是`username.exe`程序的示例輸出。 ## 當前目錄 當前目錄是用戶所在或正在使用的目錄。 在 Windows API 中,`SetCurrentDirectoryW()`更改當前目錄,`GetCurrentDirectoryW()`檢索當前目錄。 `current_directory.c` ```c #include <windows.h> #include <wchar.h> #define BUFSIZE MAX_PATH int wmain(int argc, wchar_t **argv) { wchar_t buf[BUFSIZE]; if(argc != 2) { wprintf(L"Usage: %ls <dir>\n", argv[0]); return 1; } DWORD r = SetCurrentDirectoryW(argv[1]); if (r == 0) { wprintf(L"SetCurrentDirectoryW() failed (%ld)\n", GetLastError()); return 1; } r = GetCurrentDirectoryW(BUFSIZE, buf); if (r == 0) { wprintf(L"GetCurrentDirectoryW() failed (%ld)\n", GetLastError()); return 1; } if (r > BUFSIZE) { wprintf(L"Buffer too small; needs %d characters\n", r); return 1; } wprintf(L"Current directory is: %ls\n", buf); return 0; } ``` 在代碼示例中,我們更改并打印當前工作目錄。 該程序接收一個命令行參數-要更改的目錄。 ```c #define BUFSIZE MAX_PATH ``` 我們使用`MAX_PATH`常量,該常量定義系統路徑的最大長度。 ```c if(argc != 2) { wprintf(L"Usage: %ls <dir>\n", argv[0]); return 1; } ``` 如果我們不將參數傳遞給程序,則會顯示一條錯誤消息。 ```c DWORD r = SetCurrentDirectoryW(argv[1]); ``` 我們使用`SetCurrentDirectoryW()`切換到目錄,該目錄作為參數傳遞。 ```c r = GetCurrentDirectoryW(BUFSIZE, buf); ``` 通過`GetCurrentDirectoryW()`函數調用獲取當前的工作目錄。 ```c if (r > BUFSIZE) { wprintf(L"Buffer too small; needs %d characters\n", r); return 1; } ``` 如果返回的值大于`BUFSIZE`,則緩沖區太小。 ## Windows 版本 版本幫助器函數可用于確定當前的操作系統版本。 `windows_version.c` ```c #include <windows.h> #include <wchar.h> #include <VersionHelpers.h> int wmain(void) { //if (IsWindows10OrGreater()) { // wprintf(L"This is Windows 10+"); // } if (IsWindows8Point1OrGreater()) { wprintf(L"This is Windows 8.1+\n"); } else if (IsWindows8OrGreater()) { wprintf(L"This is Windows 8\n"); } else if (IsWindows7OrGreater ()) { wprintf(L"This is Windows 7\n"); } else if (IsWindowsVistaOrGreater ()) { wprintf(L"This is Windows Vista\n"); } else if (IsWindowsXPOrGreater()) { wprintf(L"This is Windows XP\n"); } return 0; } ``` 我們使用版本幫助器函數來確定操作系統版本。 ```c #include <VersionHelpers.h> ``` 輔助函數在`VersionHelpers.h`文件中聲明。 ```c //if (IsWindows10OrGreater()) { // wprintf(L"This is Windows 10+"); // } ``` 在撰寫本文時,Pelles C 在其 SDK 中尚未定義`IsWindows10OrGreater()`。 ```c if (IsWindows8Point1OrGreater()) { wprintf(L"This is Windows 8.1+\n"); } ``` 如果當前版本為 Windows 8.1 或更高版本,則`IsWindows8Point1OrGreater()`返回 true。 ```c C:\Users\Jano\Documents\Pelles C Projects\system\WindowsVersion>WindowsVersion.exe This is Windows 7 ``` 這是程序的示例輸出。 ## 內存 `GlobalMemoryStatusEx()`檢索有關系統當前對物理和虛擬內存使用情況的信息。 `memory.c` ```c #include <windows.h> #include <wchar.h> int wmain(void) { MEMORYSTATUSEX mem = {0}; mem.dwLength = sizeof(mem); int r = GlobalMemoryStatusEx(&mem); if (r == 0) { wprintf(L"Failed to memory status %ld", GetLastError()); return 1; } wprintf(L"Memory in use: %ld percent\n", mem.dwMemoryLoad); wprintf(L"Total physical memory: %lld\n", mem.ullTotalPhys); wprintf(L"Free physical memory: %lld\n", mem.ullAvailPhys); wprintf(L"Total virtual memory: %lld\n", mem.ullTotalVirtual); wprintf(L"Free virtual memory: %lld\n", mem.ullAvailVirtual); return 0; } ``` 該程序將有關內存使用情況的統計信息打印到控制臺。 ```c MEMORYSTATUSEX mem = {0}; ``` `GlobalMemoryStatusEx()`函數在`MEMORYSTATUSEX`結構中存儲有關內存狀態的信息。 ```c int r = GlobalMemoryStatusEx(&mem); ``` `GlobalMemoryStatusEx()`函數被執行; 信息存儲在結構中。 ```c wprintf(L"Memory in use: %ld percent\n", mem.dwMemoryLoad); ``` `dwMemoryLoad`成員指定正在使用的物理內存的大約百分比。 ```c wprintf(L"Total physical memory: %lld\n", mem.ullTotalPhys); ``` `ullTotalPhys`成員以字節為單位指定實際的物理內存。 ```c wprintf(L"Free physical memory: %lld\n", mem.ullAvailPhys); ``` `ullTotalPhys`成員以字節為單位指定當前可用的物理內存量。 ```c wprintf(L"Total virtual memory: %lld\n", mem.ullTotalVirtual); ``` `ullTotalVirtual`成員以字節為單位指定虛擬內存總量。 ```c wprintf(L"Free virtual memory: %lld\n", mem.ullAvailVirtual); ``` `ullAvailVirtual`成員以字節為單位指定可用虛擬內存量。 ```c C:\Users\Jano\Documents\Pelles C Projects\system\Memory>Memory.exe Memory in use: 47 percent Total physical memory: 4226072576 Free physical memory: 2229788672 Total virtual memory: 8796092891136 Free virtual memory: 8796052586496 ``` 這是程序的示例輸出。 ## 已知數據夾 從 Windows Vista 開始,使用新系統來標識 Windows 中的重要目錄。 它被稱為已知文件夾。 已知文件夾使用一組 GUID(全局唯一標識符)值來引用重要文件夾。 `SHGetKnownFolderPath()`函數檢索由文件夾 ID 標識的已知文件夾的完整路徑。 `documents_dir.c` ```c #include <windows.h> #include <initguid.h> #include <KnownFolders.h> #include <ShlObj.h> #include <wchar.h> int wmain(void) { PWSTR path = NULL; HRESULT hr = SHGetKnownFolderPath(FOLDERID_Documents, 0, NULL, &path); if (SUCCEEDED(hr)) { wprintf(L"%ls\n", path); } CoTaskMemFree(path); return 0; } ``` 該示例確定用戶的`Documents`目錄的完整路徑。 我們需要將`shell32.lib`和`ole32.lib`添加到項目庫中。 ```c #include <initguid.h> ``` 由于某些內部 API 問題,我們需要包含`initguid.h`文件; 否則,該示例將無法編譯。 它失敗,并顯示`Unresolved external symbol 'FOLDERID_Documents'`錯誤。 ```c HRESULT hr = SHGetKnownFolderPath(&FOLDERID_Documents, 0, NULL, &path); ``` `SHGetKnownFolderPath()`用于確定文檔目錄的路徑。 ```c if (SUCCEEDED(hr)) { wprintf(L"%ls\n", path); } ``` `SUCCEEDED`宏可用于確定函數調用是否成功。 ```c CoTaskMemFree(path); ``` 最后,必須使用`CoTaskMemFree()`函數釋放分配的內存。 ```c C:\Users\Jano\Documents\Pelles C Projects\system\DocumentsDir>DocumentsDir.exe C:\Users\Jano\Documents ``` 這是`DocumentsDir.exe`程序的示例輸出。 ## 驅動器名稱 `GetLogicalDriveStringsW()`函數使用指定系統中有效驅動器的字符串填充緩沖區。 `get_drives.c` ```c #include <windows.h> #include <wchar.h> int wmain(void) { wchar_t LogicalDrives[MAX_PATH] = {0}; DWORD r = GetLogicalDriveStringsW(MAX_PATH, LogicalDrives); if (r == 0) { wprintf(L"Failed to get drive names %ld", GetLastError()); return 1; } if (r > 0 && r <= MAX_PATH) { wchar_t *SingleDrive = LogicalDrives; while(*SingleDrive) { wprintf(L"%ls\n", SingleDrive); SingleDrive += wcslen(SingleDrive) + 1; } } return 0; } ``` 該示例打印系統中的有效驅動器。 ```c wchar_t LogicalDrives[MAX_PATH] = {0}; ``` 驅動器名稱是路徑類型,因此`MAX_PATH`常量與其最大長度有關。 `LogicalDrives`是一個字符串數組,用作`GetLogicalDriveStringsW()`函數的緩沖區。 ```c DWORD r = GetLogicalDriveStringsW(MAX_PATH, LogicalDrives); ``` `GetLogicalDriveStringsW()`被調用。 緩沖區中填充了以零結尾的字符串,這些字符串代表設備名稱。 該函數的第一個參數是指定緩沖區的最大大小。 緩沖區是第二個參數。 ```c wchar_t *SingleDrive = LogicalDrives; while(*SingleDrive) { wprintf(L"%ls\n", SingleDrive); SingleDrive += wcslen(SingleDrive) + 1; } ``` 我們遍歷設備名稱數組并將其打印到控制臺。 ```c C:\Users\Jano\Documents\Pelles C Projects\system\GetDrives>GetDrives.exe C:\ D:\ ``` 系統上有兩個驅動器:`C:\`和`D:\`。 ## 可用空間 `GetDiskFreeSpaceExW()`檢索有關磁盤卷上可用空間量的信息。 該函數提供三段信息:總空間量,可用空間量以及與調用線程關聯的用戶可用的可用空間。 `free_disk_space.c` ```c #include <windows.h> #include <wchar.h> int wmain(void) { unsigned __int64 freeCall, total, free; int r = GetDiskFreeSpaceExW(L"C:\\", (PULARGE_INTEGER) &freeCall, (PULARGE_INTEGER) &total, (PULARGE_INTEGER) &free); if (r == 0) { wprintf(L"Failed to get free disk space %ld", GetLastError()); return 1; } wprintf(L"Available space to caller: %I64u MB\n", freeCall / (1024*1024)); wprintf(L"Total space: %I64u MB\n", total / (1024*1024)); wprintf(L"Free space on drive: %I64u MB\n", free / (1024*1024)); return 0; } ``` 該示例檢查`C:\`驅動器上的磁盤空間。 ```c unsigned __int64 freeCall, total, free; ``` 數量以字節為單位; 這些數字可能非常大。 使用`unsigned __int64`類型,它是一個能夠存儲非常大的值的正 64 位整數。 ```c int r = GetDiskFreeSpaceExW(L"C:\\", (PULARGE_INTEGER) &freeCall, (PULARGE_INTEGER) &total, (PULARGE_INTEGER) &free); ``` `GetDiskFreeSpaceExW()`被調用。 ```c wprintf(L"Available space to caller: %I64u MB\n", freeCall / (1024*1024)); wprintf(L"Total space: %I64u MB\n", total / (1024*1024)); wprintf(L"Free space on drive: %I64u MB\n", free / (1024*1024)); ``` 使用`wprintf()`函數將這三個金額打印到控制臺。 這些值以 MB 表示。 ```c C:\Users\Jano\Documents\Pelles C Projects\system\FreeDiskSpace>FreeDiskSpace.exe Available space to caller: 20377 MB Total space: 69999 MB Free space on drive: 20377 MB ``` 這是`FreeDiskSpace.exe`程序的示例輸出。 ## CPU 速度 可以通過檢查注冊表值來確定 CPU 速度。 該值將在安裝過程中寫入注冊表。 我們需要查詢`HARDWARE\DESCRIPTION\System\CentralProcessor\0`鍵。 `cpu_speed.c` ```c #include <windows.h> #include <wchar.h> int wmain(void) { DWORD BufSize = MAX_PATH; DWORD mhz = MAX_PATH; HKEY key; long r = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &key); if (r != ERROR_SUCCESS) { wprintf(L"RegOpenKeyExW() failed %ld", GetLastError()); return 1; } r = RegQueryValueExW(key, L"~MHz", NULL, NULL, (LPBYTE) &mhz, &BufSize); if (r != ERROR_SUCCESS) { wprintf(L"RegQueryValueExW() failed %ld", GetLastError()); return 1; } wprintf(L"CPU speed: %lu MHz\n", mhz); r = RegCloseKey(key); if (r != ERROR_SUCCESS) { wprintf(L"Failed to close registry handle %ld", GetLastError()); return 1; } return 0; } ``` 該示例確定 CPU 速度。 ```c long r = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &key); ``` `RegOpenKeyExW()`函數用于打開提供的注??冊表項。 ```c r = RegQueryValueExW(key, L"~MHz", NULL, NULL, (LPBYTE) &mhz, &BufSize); ``` 通過`RegQueryValueExW()`函數讀取該值。 ```c r = RegCloseKey(key); ``` `RegCloseKey()`關閉注冊表句柄。 ```c C:\Users\Jano\Documents\Pelles C Projects\system\CpuSpeed>CpuSpeed.exe CPU speed: 2394 MHz ``` 這是`CpuSpeed.exe`程序的示例輸出。 在 Windows API 教程的這一部分中,我們使用了一些系統函數。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看