赞
踩
(1)函数的概述
GetFileTime 是一个Windows API函数,它用于检索指定文件或目录的创建、访问和最后写入时间。这些时间以FILETIME结构的形式返回,该结构表示自1601年1月1日(UTC)起的100纳秒间隔数。
(2)函数所在的动态链接库
GetFileTime 函数位于 Kernel32.dll 动态链接库中。
(3)函数的原型
- BOOL GetFileTime(
- HANDLE hFile,
- LPFILETIME lpCreationTime,
- LPFILETIME lpLastAccessTime,
- LPFILETIME lpLastWriteTime
- );
(4)各参数及返回值的详细解释
(5)函数的详细作用
GetFileTime函数用于检索与指定文件或目录句柄相关联的创建、访问和最后写入时间。这些时间戳对于文件管理和监视文件更改非常有用。
(6)函数的C++示例
- #include <windows.h>
- #include <iostream>
- #include <ctime>
- #include <iomanip>
-
- void ConvertFileTimeToLocalTime(const FILETIME& ft, SYSTEMTIME& st) {
- FILETIME localFileTime;
- FileTimeToLocalFileTime(&ft, &localFileTime);
- FileTimeToSystemTime(&localFileTime, &st);
- }
-
- void PrintFileTime(const FILETIME& ft) {
- SYSTEMTIME st;
- ConvertFileTimeToLocalTime(ft, st);
-
- std::cout << std::put_time(&st, "%Y-%m-%d %H:%M:%S") << std::endl;
- }
-
- int main() {
- HANDLE hFile = CreateFileA(
- "C:\\example\\test.txt",
- GENERIC_READ,
- FILE_SHARE_READ,
- NULL,
- OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL,
- NULL
- );
-
- if (hFile == INVALID_HANDLE_VALUE) {
- std::cerr << "CreateFile failed." << std::endl;
- return 1;
- }
-
- FILETIME ftCreate, ftAccess, ftWrite;
- if (GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite)) {
- std::cout << "Creation time: ";
- PrintFileTime(ftCreate);
- std::cout << "Last access time: ";
- PrintFileTime(ftAccess);
- std::cout << "Last write time: ";
- PrintFileTime(ftWrite);
- } else {
- std::cerr << "GetFileTime failed." << std::endl;
- }
-
- CloseHandle(hFile);
- return 0;
- }
(7)使用时的注意事项
(1)函数的概述
GetFileType 是一个Windows API函数,用于确定指定文件或设备的类型。它可以用来检测文件是磁盘文件、磁盘驱动器、字符设备、管道还是未知类型。
(2)函数所在的动态链接库
GetFileType 函数通常位于 Kernel32.dll 动态链接库中。
(3)函数的原型
- DWORD GetFileType(
- HANDLE hFile
- );
(4)各参数及返回值的详细解释
(5)函数的详细作用
GetFileType 函数用于确定文件或设备的类型。这对于需要区分不同文件类型的应用程序或系统服务非常有用。例如,一个备份软件可能需要知道一个文件是否是一个磁盘文件,以便决定如何备份它。
(6)函数的C++示例
-
- #include <windows.h>
- #include <iostream>
-
- int main() {
- HANDLE hFile = CreateFileA("C:\\example.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if (hFile == INVALID_HANDLE_VALUE) {
- std::cerr << "Failed to open file." << std::endl;
- return 1;
- }
-
- DWORD fileType = GetFileType(hFile);
- switch (fileType) {
- case FILE_TYPE_DISK:
- std::cout << "The file is a disk file." << std::endl;
- break;
- case FILE_TYPE_CHAR:
- std::cout << "The file is a character device." << std::endl;
- break;
- case FILE_TYPE_PIPE:
- std::cout << "The file is a named pipe." << std::endl;
- break;
- case FILE_TYPE_UNKNOWN:
- default:
- std::cout << "The file type is unknown." << std::endl;
- break;
- }
-
- CloseHandle(hFile);
- return 0;
- }
(7)使用时的注意事项
(1)函数的概述
GetFileVersionInfo 是一个Windows API函数,用于检索有关文件版本信息的详细数据。它提供了关于二进制文件(如DLL、EXE、OCX等)的版本信息,这些信息通常用于软件分发、安装和更新。
(2)函数所在的动态链接库
GetFileVersionInfo 函数通常位于 Version.dll 动态链接库中。
(3)函数的原型
- BOOL GetFileVersionInfo(
- LPCTSTR lptstrFilename,
- DWORD dwHandle,
- DWORD dwLen,
- void *lpData
- );
(4)各参数及返回值的详细解释
(5)函数的详细作用
GetFileVersionInfo函数用于检索指定文件的版本信息。这些信息通常包括文件的主要版本、次要版本、构建号和修订号,以及可能的其他字符串信息,如公司名称、产品名称、文件描述等。这些信息通常被存储在文件的资源部分,并遵循特定的版本信息格式。
(6)函数的C++示例
-
- #include <windows.h>
- #include <verrsrc.h>
- #include <stdio.h>
-
- #pragma comment(lib, "version.lib")
-
- int main() {
- const char* filePath = "C:\\example.exe";
- DWORD dwHandle = 0;
- DWORD dwInfoSize = GetFileVersionInfoSize(filePath, &dwHandle);
- if (dwInfoSize == 0) {
- printf("Failed to get file version info size.\n");
- return 1;
- }
-
- void* pVersionInfo = malloc(dwInfoSize);
- if (!GetFileVersionInfo(filePath, dwHandle, dwInfoSize, pVersionInfo)) {
- printf("Failed to get file version info.\n");
- free(pVersionInfo);
- return 1;
- }
-
- // 这里可以添加代码来解析pVersionInfo缓冲区中的版本信息
- // ...
-
- free(pVersionInfo);
- return 0;
- }
// 注意:上面的示例只演示了如何获取版本信息的大小并分配缓冲区,
// 但没有展示如何解析版本信息。这通常需要调用VerQueryValue函数。
(7)使用时的注意事项
(1)函数的概述
GetFileVersionInfoSize 是一个Windows API函数,用于确定为了包含文件的版本信息,需要为GetFileVersionInfo函数分配多大的缓冲区。这个函数为调用者提供了一个预先确定缓冲区大小的方法,从而可以确保有足够的空间来接收版本信息。
(2)函数所在的动态链接库
GetFileVersionInfoSize 函数通常位于 Version.dll 动态链接库中。
(3)函数的原型
- DWORD GetFileVersionInfoSize(
- LPCTSTR lptstrFilename,
- LPDWORD lpdwHandle
- );
(4)各参数及返回值的详细解释
(5)函数的详细作用
GetFileVersionInfoSize 函数的作用是确定为了检索特定文件的版本信息,需要为GetFileVersionInfo函数分配多大的缓冲区。这是因为在某些情况下,文件的版本信息可能非常大,因此预先确定所需的大小可以避免缓冲区溢出。
(6)函数的C++示例
-
- #include <windows.h>
- #include <stdio.h>
-
- #pragma comment(lib, "version.lib")
-
- int main() {
- const char* filePath = "C:\\example.exe";
- DWORD dwHandle = 0; // 在此示例中,我们不需要句柄,因此将其设置为0
- DWORD dwInfoSize = GetFileVersionInfoSize(filePath, &dwHandle);
- if (dwInfoSize == 0) {
- DWORD dwError = GetLastError();
- printf("Failed to get file version info size. Error code: %lu\n", dwError);
- return 1;
- }
-
- // 现在,我们可以为GetFileVersionInfo分配一个足够大的缓冲区
- void* pVersionInfo = malloc(dwInfoSize);
- if (pVersionInfo == NULL) {
- printf("Failed to allocate memory for file version info.\n");
- return 1;
- }
-
- // 接下来,你可以使用GetFileVersionInfo来检索版本信息
- // ...
-
- // 释放内存
- free(pVersionInfo);
-
- return 0;
- }
(7)使用时的注意事项
(1)函数的概述
GetFullPathName 是一个Windows API函数,它用于检索指定文件或目录的完整路径名。这个函数会解析任何路径中的相对路径部分(如.、..)和符号链接,并返回包含绝对路径的字符串。
(2)函数所在的动态链接库
GetFullPathName 函数通常位于 Kernel32.dll 动态链接库中。
(3)函数的原型
- DWORD GetFullPathName(
- LPCTSTR lpFileName,
- LPDWORD lpnBufferLength,
- LPTSTR lpBuffer,
- LPTSTR *lpFilePart
- );
(4)各参数及返回值的详细解释
(5)函数的详细作用
GetFullPathName 的主要作用是获取指定文件或目录的完整路径名。这在处理相对路径、符号链接或需要确定文件或目录的确切位置时非常有用。它还可以用来检查路径是否存在或验证路径的语法是否正确。
(6)函数的C++示例
- #include <windows.h>
- #include <iostream>
-
- int main() {
- const char* filePath = "..\\example.txt"; // 相对路径
- DWORD dwBufferSize = MAX_PATH; // 假设缓冲区足够大以容纳完整路径
- char buffer[MAX_PATH];
- char* filePart;
-
- DWORD result = GetFullPathName(filePath, &dwBufferSize, buffer, &filePart);
- if (result == 0) {
- std::cerr << "GetFullPathName failed with error: " << GetLastError() << std::endl;
- return 1;
- }
-
- std::cout << "Full path: " << buffer << std::endl;
- std::cout << "File part: " << filePart << std::endl;
-
- return 0;
- }
(7)使用时的注意事项
(1)函数的概述
GetLogicalDrives 是一个Windows API函数,用于检索当前系统上的逻辑磁盘驱动器。它返回一个位掩码,其中每个位代表一个有效的逻辑驱动器。
(2)函数所在的动态链接库
这个函数位于kernel32.dll中。
(3)函数的原型
DWORD GetLogicalDrives(void);
(4)各参数及返回值的详细解释
(5)函数的详细作用
GetLogicalDrives 函数的主要作用是检测并返回系统上所有逻辑驱动器的位掩码。这可以用于枚举所有存在的逻辑驱动器,或者检查特定的逻辑驱动器是否存在。
(6)函数的C++示例
- #include <windows.h>
- #include <iostream>
-
- int main() {
- DWORD drives = GetLogicalDrives();
-
- char driveLetters[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // 26个驱动器加上终止符
- for (int i = 0; i < 26; ++i) {
- if (drives & (1 << i)) { // 检查第i位是否被设置
- std::cout << "Drive " << driveLetters[i] << " exists." << std::endl;
- }
- }
-
- return 0;
- }
(7)使用时的注意事项
(1)函数的概述
GetLogicalDriveStrings 是一个Windows API函数,用于检索当前系统上所有逻辑磁盘驱动器的字符串。这个函数返回一个以空字符('\0')分隔的双字节字符串数组,其中每个字符串代表一个逻辑驱动器的根目录路径(例如 "C:")。
(2)函数所在的动态链接库
这个函数位于kernel32.dll中。
(3)函数的原型
- DWORD GetLogicalDriveStrings(
- DWORD nBufferLength,
- LPTSTR lpBuffer
- );
(4)各参数及返回值的详细解释
(6)函数的C++示例
- #include <windows.h>
- #include <iostream>
- #include <tchar.h>
-
- int main() {
- // 假设最大有32个驱动器(A: 到 Z:),每个驱动器名最多为4个字符(例如 "C:\"),加上空字符分隔和结尾的双空字符
- TCHAR buffer[32 * 4 + 2 * (32 - 1) + 2]; // 缓冲区大小足够大
- DWORD result = GetLogicalDriveStrings(sizeof(buffer) / sizeof(TCHAR), buffer);
-
- if (result > 0 && result <= sizeof(buffer) / sizeof(TCHAR)) {
- // 遍历返回的字符串数组
- for (TCHAR* p = buffer; *p != _T('\0'); p += _tcslen(p) + 1) {
- std::wcout << p << std::endl; // 假设使用宽字符版本,输出逻辑驱动器路径
- }
- } else {
- // 处理错误
- std::cerr << "Failed to get logical drive strings. Error: " << GetLastError() << std::endl;
- }
-
- return 0;
- }
(7)使用时的注意事项
(1)函数的概述
GetOverlappedResult 是一个Windows API函数,用于获取重叠I/O操作的结果。重叠I/O是一种允许应用程序在等待I/O操作完成的同时执行其他操作的机制。GetOverlappedResult 可以检索一个重叠I/O操作的状态,包括操作是否完成、传输的字节数以及是否发生错误。
(2)函数所在的动态链接库
这个函数位于kernel32.dll中。
(3)函数的原型
- BOOL GetOverlappedResult(
- HANDLE hFile,
- LPOVERLAPPED lpOverlapped,
- LPDWORD lpNumberOfBytesTransferred,
- BOOL bWait
- );
(4)各参数及返回值的详细解释
(5)函数的详细作用
GetOverlappedResult函数用于检索与指定文件或I/O设备句柄相关联的重叠I/O操作的结果。如果bWait参数为TRUE,则函数将等待操作完成并返回结果。如果bWait参数为FALSE,则函数将立即返回操作的状态,而不会等待其完成。
(6)函数的C++示例
- #include <windows.h>
- #include <iostream>
-
- int main() {
- HANDLE hFile = CreateFile(
- TEXT("example.txt"), // 文件名
- GENERIC_WRITE, // 打开以写入
- 0, // 不共享
- NULL, // 默认安全
- OPEN_ALWAYS, // 打开或创建
- FILE_ATTRIBUTE_NORMAL, // 正常文件
- NULL // 不使用模板文件
- );
-
- if (hFile == INVALID_HANDLE_VALUE) {
- std::cerr << "CreateFile failed: " << GetLastError() << std::endl;
- return 1;
- }
-
- OVERLAPPED overlapped = { 0 };
- DWORD bytesWritten = 0;
- BOOL result = WriteFileEx(
- hFile,
- "Hello, World!", // 要写入的数据
- 13, // 数据的大小
- &overlapped, // 重叠结构
- NULL // 不使用完成例程
- );
-
- if (!result) {
- std::cerr << "WriteFileEx failed: " << GetLastError() << std::endl;
- CloseHandle(hFile);
- return 1;
- }
-
- // 等待操作完成并获取结果
- result = GetOverlappedResult(hFile, &overlapped, &bytesWritten, TRUE);
- if (!result) {
- std::cerr << "GetOverlappedResult failed: " << GetLastError() << std::endl;
- } else {
- std::cout << "Bytes written: " << bytesWritten << std::endl;
- }
-
- CloseHandle(hFile);
- return 0;
- }
(7)使用时的注意事项
(1)函数的概述
GetPrivateProfileInt 是一个Windows API函数,用于从初始化文件(.ini文件)中读取一个整数值。这些文件通常用于存储应用程序的配置信息。
(2)函数所在的动态链接库
GetPrivateProfileInt 函数位于 Kernel32.dll 动态链接库中。
(3)函数的原型
- UINT GetPrivateProfileInt(
- LPCTSTR lpAppName,
- LPCTSTR lpKeyName,
- INT nDefault,
- LPCTSTR lpFileName
- );
(4)各参数及返回值的详细解释
(5)函数的详细作用
GetPrivateProfileInt 函数用于从指定的初始化文件中读取一个整数值。它首先查找指定的节和键,如果找到,则返回与该键关联的值(作为整数)。如果节或键不存在,则返回默认值。
(6)函数的C++示例
- #include <windows.h>
- #include <iostream>
-
- int main() {
- const char* fileName = "example.ini";
- const char* section = "Settings";
- const char* key = "Value";
- int defaultValue = 0;
-
- int value = GetPrivateProfileInt(section, key, defaultValue, fileName);
- std::cout << "Value from INI file: " << value << std::endl;
-
- return 0;
- }
(7)使用时的注意事项
(1)函数的概述
GetPrivateProfileSection 是一个Windows API函数,它用于从指定的初始化文件(.ini文件)中检索指定节(section)的所有键(key)和值(value),并将它们存储在一个由调用者提供的缓冲区中。
(2)函数所在的动态链接库
GetPrivateProfileSection 函数位于 Kernel32.dll 动态链接库中。
(3)函数的原型
- DWORD GetPrivateProfileSection(
- LPCTSTR lpAppName,
- LPTSTR lpReturnedString,
- DWORD nSize,
- LPCTSTR lpFileName
- );
(4)各参数及返回值的详细解释
(6)函数的C++示例
- #include <windows.h>
- #include <iostream>
-
- int main() {
- const char* fileName = "example.ini";
- const char* section = "Settings";
- char buffer[1024]; // 假设缓冲区足够大以容纳整个节的内容
- DWORD result;
-
- result = GetPrivateProfileSection(section, buffer, sizeof(buffer), fileName);
- if (result > 0 && result < sizeof(buffer)) {
- std::cout << "Section contents:\n" << buffer << std::endl;
- } else if (result == sizeof(buffer)) {
- std::cerr << "Buffer too small to hold the entire section." << std::endl;
- } else {
- std::cerr << "Section not found or other error occurred." << std::endl;
- }
-
- return 0;
- }
(7)使用时的注意事项
(1)函数的概述
GetPrivateProfileString 是一个Windows API函数,用于从指定的初始化文件(.ini文件)中检索指定节(section)下的指定键(key)的值(value),并将该值复制到由调用者提供的缓冲区中。
(2)函数所在的动态链接库
GetPrivateProfileString 函数位于 Kernel32.dll 动态链接库中。
(3)函数的原型
- DWORD GetPrivateProfileString(
- LPCTSTR lpAppName,
- LPCTSTR lpKeyName,
- LPCTSTR lpDefault,
- LPTSTR lpReturnedString,
- DWORD nSize,
- LPCTSTR lpFileName
- );
(4)各参数及返回值的详细解释
(6)函数的C++示
- #include <windows.h>
- #include <iostream>
-
- int main() {
- const char* fileName = "example.ini";
- const char* section = "Settings";
- const char* key = "MyKey";
- const char* defaultValue = "DefaultValue";
- char buffer[256];
- DWORD result;
-
- result = GetPrivateProfileString(section, key, defaultValue, buffer, sizeof(buffer), fileName);
- if (result > 0 && result < sizeof(buffer)) {
- std::cout << "Value for key '" << key << "': " << buffer << std::endl;
- } else if (result == sizeof(buffer) - 1) { // 减去1,因为需要为null终止符留出空间
- std::cerr << "Buffer too small to hold the entire value." << std::endl;
- } else {
- std::cerr << "Key not found or other error occurred. Using default value: " << defaultValue << std::endl;
- // 在这里可以使用defaultValue作为备选值
- }
-
- return 0;
- }
(7)使用时的注意事项
(1)函数的概述
GetProfileInt 是一个Windows API函数,用于从指定的初始化文件(.ini文件)中检索指定节(section)下的指定键(key)的整数值。如果键不存在或无法转换为整数,则返回一个默认值。
(2)函数所在的动态链接库
GetProfileInt 函数位于 Kernel32.dll 动态链接库中。
(3)函数的原型
- UINT GetProfileInt(
- LPCTSTR lpAppName,
- LPCTSTR lpKeyName,
- INT nDefault
- );
(4)各参数及返回值的详细解释
(5)函数的详细作用
GetProfileInt 函数尝试从指定的初始化文件中读取指定节和键下的字符串值,并将其转换为一个整数。如果无法读取键的值,或者该值不是一个有效的整数,则函数返回nDefault参数指定的默认值。
(6)函数的C++示例
- #include <windows.h>
- #include <iostream>
-
- int main() {
- const char* fileName = "example.ini"; // 注意:GetProfileInt不直接接受文件名,它使用默认的WIN.INI或应用程序的.INI文件
- const char* section = "Settings";
- const char* key = "MyIntegerKey";
- int defaultValue = 123;
-
- int result = GetProfileInt(section, key, defaultValue);
- std::cout << "Value for key '" << key << "': " << result << std::endl;
-
- return 0;
- }
注意:在上面的示例中,GetProfileInt 实际上不会直接读取fileName指定的文件,因为它默认读取WIN.INI或应用程序的.INI文件(如果存在)。如果你需要读取特定文件,应使用GetPrivateProfileInt函数。
(7)使用时的注意事项
(1)函数的概述
GetProfileSection 是一个Windows API函数,用于从指定的初始化文件(.ini文件)中检索指定节(section)的所有键和值,并将它们复制到由调用者提供的缓冲区中。
(2)函数所在的动态链接库
GetProfileSection 函数位于 Kernel32.dll 动态链接库中。
(3)函数的原型
- DWORD GetProfileSection(
- LPCTSTR lpAppName,
- LPTSTR lpReturnedString,
- DWORD nSize
- );
(4)各参数及返回值的详细解释
(5)函数的详细作用
GetProfileSection 函数用于从指定的初始化文件中检索一个完整节的内容,并将这些内容复制到提供的缓冲区中。如果节不存在,则不会向缓冲区中写入任何内容。
(6)函数的C++示例
- #include <windows.h>
- #include <iostream>
-
- int main() {
- const char* section = "Settings";
- char buffer[1024]; // 假设缓冲区足够大以容纳整个节的内容
- DWORD result;
-
- result = GetProfileSection(section, buffer, sizeof(buffer));
- if (result > 0 && result < sizeof(buffer) - 1) { // 减去1以确保有足够的空间容纳终止的双null字符
- std::cout << "Contents of section '" << section << "':\n" << buffer << std::endl;
- } else if (result == sizeof(buffer) - 2) { // 减去2以考虑终止的双null字符
- std::cerr << "Buffer too small to hold the entire section." << std::endl;
- } else {
- std::cerr << "Section not found or other error occurred." << std::endl;
- }
-
- return 0;
- }
注意:在输出buffer的内容时,你可能需要添加一些额外的逻辑来正确显示null字符分隔的键和值对。
(7)使用时的注意事项
(1)函数的概述
GetProfileString 是一个Windows API函数,用于从指定的初始化文件(.ini文件)中检索指定节(section)下的指定键(key)的字符串值。如果键不存在,则返回默认值。
(2)函数所在的动态链接库
GetProfileString 函数位于 Kernel32.dll 动态链接库中。
(3)函数的原型
- DWORD GetProfileString(
- LPCTSTR lpAppName,
- LPCTSTR lpKeyName,
- LPCTSTR lpDefault,
- LPTSTR lpReturnedString,
- DWORD nSize
- );
(4)各参数及返回值的详细解释
(5)函数的详细作用
GetProfileString 函数用于从指定的初始化文件中检索指定节下的指定键的字符串值。如果键不存在,则返回默认值。检索到的值(或默认值)将被复制到提供的缓冲区中。
(6)函数的C++示例
-
- #include <windows.h>
- #include <iostream>
-
- int main() {
- const char* fileName = "example.ini"; // 注意:GetProfileString不直接接受文件名,它使用默认的WIN.INI或应用程序的.INI文件
- const char* section = "Settings";
- const char* key = "MyStringKey";
- const char* defaultValue = "DefaultValue";
- char buffer[256];
-
- DWORD result = GetProfileString(section, key, defaultValue, buffer, sizeof(buffer));
- if (result > 0 && result < sizeof(buffer)) {
- std::cout << "Value for key '" << key << "': " << buffer << std::endl;
- } else {
- std::cerr << "Error occurred or buffer was too small." << std::endl;
- }
-
- return 0;
- }
注意:在上面的示例中,GetProfileString 实际上不会直接读取fileName指定的文件,它默认读取WIN.INI文件或应用程序的.INI文件(如果存在)。如果你需要读取特定文件,应使用GetPrivateProfileString函数。
(7)使用时的注意事项
(1)函数的概述
GetShortPathName 是一个Windows API函数,用于获取指定文件或目录的短路径名。在某些情况下,尤其是在旧的Windows系统或不支持长文件名(超过8.3格式)的某些API中,可能需要使用短路径名。
(2)函数所在的动态链接库
GetShortPathName 函数位于 Kernel32.dll 动态链接库中。
(3)函数的原型
- WORD GetShortPathName(
- LPCTSTR lpszLongPath,
- LPTSTR lpszShortPath,
- DWORD cchBuffer
- );
(4)各参数及返回值的详细解释
(5)函数的详细作用
GetShortPathName 函数将指定的长路径名转换为短路径名(8.3格式),并将结果存储在提供的缓冲区中。如果文件或目录没有对应的短路径名(例如,在支持长文件名的系统上),则该函数可能会返回原始的长路径名。
(6)函数的C++示例
- #include <windows.h>
- #include <iostream>
-
- int main() {
- const char* longPath = "C:\\Program Files\\Example\\LongFileName.txt";
- char shortPath[MAX_PATH]; // MAX_PATH 是预定义的最大路径长度
-
- DWORD result = GetShortPathNameA(longPath, shortPath, MAX_PATH);
- if (result != 0 && result < MAX_PATH) {
- std::cout << "Short path: " << shortPath << std::endl;
- } else {
- std::cerr << "Error occurred or buffer was too small." << std::endl;
- if (result == 0) {
- // 获取具体的错误代码
- DWORD errorCode = GetLastError();
- std::cerr << "Error code: " << errorCode << std::endl;
- }
- }
-
- return 0;
- }
注意:这里使用了GetShortPathNameA,它是GetShortPathName的ANSI版本。如果你正在处理宽字符(Unicode)字符串,请使用GetShortPathNameW。
(7)使用时的注意事项
(1)函数的概述
GetSystemDirectory 函数是一个Windows API函数,用于检索Windows系统目录(如C:\Windows\System32)的完全限定路径。
(2)函数所在的动态链接库
GetSystemDirectory 函数位于 Kernel32.dll 动态链接库中。
(3)函数的原型
- UINT GetSystemDirectory(
- LPTSTR lpBuffer,
- UINT uSize
- );
(4)各参数及返回值的详细解释
(5)函数的详细作用
GetSystemDirectory 函数将Windows系统目录的路径复制到指定的缓冲区中。这个路径通常是固定的,如C:\Windows\System32(对于32位Windows系统上的32位应用)或C:\Windows\SysWOW64(对于32位Windows系统上的64位应用),但在某些情况下,它可能因安装或配置而异。
(6)函数的C++示
- #include <windows.h>
- #include <iostream>
-
- int main() {
- TCHAR systemDir[MAX_PATH]; // MAX_PATH 是预定义的最大路径长度
- UINT result = GetSystemDirectory(systemDir, MAX_PATH);
-
- if (result != 0 && result < MAX_PATH) {
- std::wcout << L"System directory: " << systemDir << std::endl;
- } else {
- std::cerr << "Error occurred or buffer was too small." << std::endl;
- if (result == 0) {
- // 获取具体的错误代码
- DWORD errorCode = GetLastError();
- std::cerr << "Error code: " << errorCode << std::endl;
- }
- }
-
- return 0;
- }
注意:上述代码使用宽字符(Unicode)版本的GetSystemDirectory函数(隐式地通过TCHAR和MAX_PATH)。如果你正在使用ANSI版本的API,请确保你的项目设置和代码与之兼容。
(7)使用时的注意事项
(1)函数的概述
GetTempFileName 是一个Windows API函数,用于在指定的目录下创建一个具有唯一文件名的临时文件。这个函数不仅返回文件名的字符串,而且如果指定了CREATE_NEW标志,它还会在文件系统中实际创建该文件。
(2)函数所在的动态链接库
GetTempFileName 函数位于 Kernel32.dll 动态链接库中。
(3)函数的原型
- UINT GetTempFileName(
- LPCTSTR lpPathName,
- LPCTSTR lpPrefixString,
- UINT uUnique,
- LPTSTR lpTempFileName
- );
(4)各参数及返回值的详细解释
(5)函数的详细作用
GetTempFileName函数的主要作用是创建一个具有唯一文件名的临时文件。它确保了即使在同一目录下同时有多个应用程序或进程运行,也不会因为文件名冲突而导致问题。此外,如果指定了CREATE_NEW标志(尽管这个标志不是GetTempFileName的直接参数,但可以在调用CreateFile时使用返回的文件名时指定),则该函数还会在文件系统中实际创建该文件。
(6)函数的C++示
- #include <windows.h>
- #include <iostream>
-
- int main() {
- TCHAR tempPath[MAX_PATH];
- if (GetTempPath(MAX_PATH, tempPath) == 0) {
- std::cerr << "GetTempPath failed: " << GetLastError() << std::endl;
- return 1;
- }
-
- TCHAR tempFileName[MAX_PATH];
- UINT result = GetTempFileName(tempPath, _T("MYAPP"), 0, tempFileName);
- if (result == 0) {
- std::cerr << "GetTempFileName failed: " << GetLastError() << std::endl;
- return 1;
- }
-
- std::wcout << L"Temporary file created: " << tempFileName << std::endl;
-
- // 如果需要,可以在这里使用tempFileName来打开或创建文件
-
- return 0;
- }
(7)使用时的注意事项
(1)函数的概述
GetTempPath 函数是一个Windows API函数,用于检索当前用户临时文件目录的路径。这个目录通常用于存储应用程序在运行时创建的临时文件。
(2)函数所在的动态链接库
GetTempPath 函数位于 Kernel32.dll 动态链接库中。
(3)函数的原型
- DWORD GetTempPath(
- DWORD nBufferLength,
- LPTSTR lpBuffer
- );
(4)各参数及返回值的详细解释
(5)函数的详细作用
GetTempPath 函数的主要作用是检索当前用户的临时文件目录路径。这个目录通常用于存储不需要永久保留的文件,如缓存文件、备份文件等。由于不同的用户可能有不同的临时文件目录(例如,出于安全或配置的原因),因此使用GetTempPath可以确保应用程序将临时文件存储在正确的位置。
(6)函数的C++示
- #include <windows.h>
- #include <iostream>
-
- int main() {
- TCHAR tempPath[MAX_PATH];
- DWORD result = GetTempPath(MAX_PATH, tempPath);
- if (result == 0 || result > MAX_PATH) {
- std::cerr << "GetTempPath failed: " << GetLastError() << std::endl;
- return 1;
- }
-
- std::wcout << L"Temporary path is: " << tempPath << std::endl;
-
- // 可以在这里使用tempPath来创建临时文件或目录
-
- return 0;
- }
(7)使用时的注意事项
(1)函数的概述
GetVolumeInformation 是一个Windows API函数,用于获取有关卷的信息,如卷的名称、序列号、文件系统类型、是否支持压缩等。这些信息通常用于文件管理和系统监控等任务。
(2)函数所在的动态链接库
GetVolumeInformation 函数位于 Kernel32.dll 动态链接库中。
(3)函数的原型
- BOOL GetVolumeInformation(
- LPCTSTR lpRootPathName,
- LPTSTR lpVolumeNameBuffer,
- DWORD nVolumeNameSize,
- LPDWORD lpVolumeSerialNumber,
- LPDWORD lpMaximumComponentLength,
- LPDWORD lpFileSystemFlags,
- LPTSTR lpFileSystemNameBuffer,
- DWORD nFileSystemNameSize
- );
(4)各参数及返回值的详细解释
(5)函数的详细作用
GetVolumeInformation 函数用于获取指定卷的详细信息,包括卷的名称、序列号、最大文件名组件长度、文件系统类型以及支持的文件系统特性(如是否支持压缩、磁盘配额等)。这些信息对于应用程序来说可能是有用的,例如,在创建或管理文件时,了解文件系统的限制和特性可以帮助避免错误和提高效率。
(6)函数的C++示
- #include <windows.h>
- #include <iostream>
- #include <string>
-
- int main() {
- TCHAR volumeName[MAX_PATH];
- DWORD volumeSerialNumber;
- DWORD maxComponentLength;
- DWORD fileSystemFlags;
- TCHAR fileSystemName[MAX_PATH];
-
- if (GetVolumeInformation(TEXT("C:\\"), volumeName, MAX_PATH, &volumeSerialNumber,
- &maxComponentLength, &fileSystemFlags, fileSystemName, MAX_PATH)) {
- std::wcout << L"Volume Name: " << volumeName << std::endl;
- std::wcout << L"Volume Serial Number: " << volumeSerialNumber << std::endl;
- std::wcout << L"Max Component Length: " << maxComponentLength << std::endl;
- std::wcout << L"File System Flags: " << fileSystemFlags << std::endl;
- std::wcout << L"File System Name: " << fileSystemName << std::endl;
- } else {
- std::cerr << "GetVolumeInformation failed: " << GetLastError() << std::endl;
- }
-
- return 0;
- }
(7)使用时的注意事项
(1)函数的概述
GetWindowsDirectory 函数用于检索Windows操作系统的安装目录(通常是 C:\Windows,但也可以是其他路径,如 D:\Windows 等,取决于安装时的选择)。
(2)函数所在的动态链接库
GetWindowsDirectory 函数位于 Kernel32.dll 动态链接库中。
(3)函数的原型
- UINT GetWindowsDirectory(
- LPTSTR lpBuffer,
- UINT uSize
- );
(4)各参数及返回值的详细解释
(5)函数的详细作用
GetWindowsDirectory 函数的主要作用是检索Windows系统的安装目录,并将该目录的路径复制到提供的缓冲区中。这对于那些需要访问或引用Windows目录下文件或文件夹的应用程序来说是非常有用的。
(6)函数的C++示
- #include <windows.h>
- #include <iostream>
- #include <string>
-
- int main() {
- TCHAR windowsDir[MAX_PATH];
- UINT result = GetWindowsDirectory(windowsDir, MAX_PATH);
-
- if (result == 0 || result > MAX_PATH) {
- std::cerr << "GetWindowsDirectory failed: " << GetLastError() << std::endl;
- return 1;
- }
-
- std::wcout << L"Windows Directory: " << windowsDir << std::endl;
-
- // 可以在这里使用windowsDir变量
-
- return 0;
- }
(7)使用时的注意事项
(1)函数的概述
MapViewOfFile 函数用于将一个文件映射对象(由CreateFileMapping创建)映射到调用进程的地址空间中,从而使得应用程序能够像访问内存一样直接访问文件内容。这种方式通常用于高效地读取和写入大文件,因为它允许文件被随机访问,而无需每次都进行磁盘I/O操作。
(2)函数所在的动态链接库
MapViewOfFile 函数位于 Kernel32.dll 动态链接库中。
(3)函数的原型
- LPVOID MapViewOfFile(
- HANDLE hFileMappingObject,
- DWORD dwDesiredAccess,
- DWORD dwFileOffsetHigh,
- DWORD dwFileOffsetLow,
- SIZE_T dwNumberOfBytesToMap
- );
(4)各参数及返回值的详细解释
(5)函数的详细作用
MapViewOfFile函数的主要作用是将文件映射对象的一部分或全部映射到调用进程的地址空间中,并返回一个指向该映射区域的指针。映射后,应用程序可以直接通过指针访问文件内容,就像访问内存一样。这种方式可以提高大文件的访问效率,因为它允许随机访问文件,并且减少了磁盘I/O操作的次数。
(6)函数的C++示例
-
- #include <windows.h>
- #include <iostream>
-
- int main() {
- // 假设hFileMappingObject已经通过CreateFileMapping创建
- HANDLE hFileMappingObject = /* ... */;
-
- // 映射文件映射对象的前4096字节到进程的地址空间
- const SIZE_T dwNumberOfBytesToMap = 4096;
- LPVOID pMapView = MapViewOfFile(hFileMappingObject, FILE_MAP_READ, 0, 0, dwNumberOfBytesToMap);
-
- if (pMapView == NULL) {
- std::cerr << "MapViewOfFile failed: " << GetLastError() << std::endl;
- return 1;
- }
-
- // 假设现在你可以通过pMapView指针访问文件内容
- // ...
-
- // 当你完成对映射区域的访问后,应使用UnmapViewOfFile函数来解除映射
- if (!UnmapViewOfFile(pMapView)) {
- std::cerr << "UnmapViewOfFile failed: " << GetLastError() << std::endl;
- return 1;
- }
-
- // 关闭hFileMappingObject句柄(如果不再需要)
- // CloseHandle(hFileMappingObject);
-
- return 0;
- }
(7)使用时的注意事项
(1)函数的概述
MoveFile 是一个Windows API函数,用于移动(重命名)文件或目录。如果目标位置在不同的驱动器上,它还会将文件从一个位置移动到另一个位置。
(2)函数所在的动态链接库
MoveFile 函数位于 kernel32.dll 动态链接库中。
(3)函数的原型
- BOOL MoveFile(
- LPCTSTR lpExistingFileName,
- LPCTSTR lpNewFileName
- );
-
- BOOL MoveFileEx(
- LPCTSTR lpExistingFileName,
- LPCTSTR lpNewFileName,
- DWORD dwFlags
- );
注意:这里提供了两个版本,MoveFile 和 MoveFileEx。MoveFileEx 是扩展版本,提供了更多的选项。这里我们主要讨论 MoveFile,但 MoveFileEx 提供了 MOVEFILE_REPLACE_EXISTING、MOVEFILE_COPY_ALLOWED 等额外的标志。
(4)各参数及返回值的详细解释
(5)函数的详细作用
MoveFile 函数的主要作用是将一个文件或目录重命名到新的位置(或只是重命名,如果源和目标在同一个目录中)。它不会复制文件,而是移动文件。如果目标位置已经存在同名文件,MoveFile 会失败(除非使用 MoveFileEx 并指定 MOVEFILE_REPLACE_EXISTING 标志)。
(6)函数的C++示
- #include <windows.h>
- #include <iostream>
-
- int main() {
- // 源文件路径
- const char* sourceFilePath = "C:\\path\\to\\source\\file.txt";
- // 目标文件路径
- const char* destinationFilePath = "C:\\path\\to\\destination\\file.txt";
-
- // 调用 MoveFile 函数
- if (MoveFile(sourceFilePath, destinationFilePath)) {
- std::cout << "File moved successfully." << std::endl;
- } else {
- std::cerr << "Failed to move file: " << GetLastError() << std::endl;
- }
-
- return 0;
- }
(7)使用时的注意事项
(1)函数的概述
OpenFileMapping 函数是Windows API的一部分,它用于打开一个已存在的命名文件映射对象。命名文件映射对象允许进程间共享内存。文件映射对象包含文件或内存对象的视图,这些对象可以被映射到进程的地址空间中。
(2)函数所在的动态链接库
OpenFileMapping 函数位于 kernel32.dll 动态链接库中。
(3)函数的原型
- HANDLE OpenFileMapping(
- DWORD dwDesiredAccess,
- BOOL bInheritHandle,
- LPCWSTR lpName
- );
(4)各参数及返回值的详细解释
(5)函数的详细作用
OpenFileMapping函数的主要作用是打开一个已存在的命名文件映射对象,以便在调用进程中访问该文件映射对象。一旦文件映射对象被打开,可以使用MapViewOfFile函数将其映射到进程的地址空间中,从而允许进程读取、写入或修改文件映射对象中的数据。
(6)函数的C++示
- #include <windows.h>
- #include <iostream>
-
- int main() {
- // 假设存在一个名为"MyFileMapping"的文件映射对象
- const wchar_t* mappingName = L"MyFileMapping";
-
- // 打开文件映射对象
- HANDLE hFileMap = OpenFileMapping(FILE_MAP_READ | FILE_MAP_WRITE, FALSE, mappingName);
- if (hFileMap == NULL) {
- std::wcerr << L"Error opening file mapping: " << GetLastError() << std::endl;
- return 1;
- }
-
- // 接下来,可以使用MapViewOfFile函数将文件映射对象映射到地址空间中
- // ...
-
- // 关闭文件映射对象句柄
- CloseHandle(hFileMap);
-
- return 0;
- }
(7)使用时的注意事项
(1)函数的概述
QueryDosDevice 是一个Windows API函数,用于查询MS-DOS设备名的映射。在Windows中,某些设备名(如 CON, PRN, COM1, LPT1 等)被保留用于与MS-DOS兼容。这些设备名通常映射到Windows设备名(如 \Device\Console, \Device\Lpt1 等)。QueryDosDevice 函数允许你查询这些映射。
(2)函数所在的动态链接库
QueryDosDevice 函数位于 kernel32.dll 动态链接库中。
(3)函数的原型
- DWORD QueryDosDevice(
- LPCTSTR lpDeviceName,
- LPTSTR lpTargetPath,
- DWORD ucchMax;
- );
(4)各参数及返回值的详细解释
(5)函数的详细作用
QueryDosDevice 函数的作用是获取指定MS-DOS设备名对应的Windows设备名。这在需要知道MS-DOS设备名在Windows内部如何表示时非常有用,例如,在编写与MS-DOS设备交互的底层代码时。
(6)函数的C++示例
- #include <windows.h>
- #include <iostream>
-
- int main() {
- TCHAR deviceName[] = TEXT("CON");
- TCHAR targetPath[MAX_PATH];
-
- DWORD result = QueryDosDevice(deviceName, targetPath, MAX_PATH);
- if (result == 0) {
- std::cerr << "Error querying DOS device: " << GetLastError() << std::endl;
- return 1;
- }
-
- // 注意:QueryDosDevice不包括结束的空字符,所以需要手动添加
- targetPath[result] = TEXT('\0');
-
- std::wcout << TEXT("DOS Device: ") << deviceName << TEXT("\n");
- std::wcout << TEXT("Windows Device: ") << targetPath << std::endl;
-
- return 0;
- }
(7)使用时的注意事项
(1)函数的概述
ReadFile 是一个Windows API函数,用于从文件或输入/输出(I/O)设备中读取数据。它通常用于读取文件的内容,但也可以用于读取套接字、管道、命名管道、控制台、通信端口等。
(2)函数所在的动态链接库
ReadFile 函数位于 kernel32.dll 动态链接库中。
(3)函数的原型
- BOOL ReadFile(
- HANDLE hFile,
- LPVOID lpBuffer,
- DWORD nNumberOfBytesToRead,
- LPDWORD lpNumberOfBytesRead,
- LPOVERLAPPED lpOverlapped
- );
(4)各参数及返回值的详细解释
(5)函数的详细作用
ReadFile 函数用于从指定的文件或设备中读取数据。它可以用于同步读取(等待操作完成)或异步读取(不等待操作完成,而是立即返回)。在同步读取中,函数会阻塞调用线程,直到读取操作完成或发生错误。在异步读取中,函数会立即返回,而读取操作会在后台进行。
(6)函数的C++示例
-
- #include <windows.h>
- #include <iostream>
-
- int main() {
- HANDLE hFile = CreateFile(
- TEXT("example.txt"), // 文件名
- GENERIC_READ, // 访问权限
- FILE_SHARE_READ, // 共享模式
- NULL, // 安全描述符
- OPEN_EXISTING, // 如何创建
- FILE_ATTRIBUTE_NORMAL, // 文件属性
- NULL // 模板文件句柄
- );
-
- if (hFile == INVALID_HANDLE_VALUE) {
- std::cerr << "Error opening file." << std::endl;
- return 1;
- }
-
- char buffer[1024];
- DWORD bytesRead;
- BOOL result = ReadFile(
- hFile, // 文件句柄
- buffer, // 接收数据的缓冲区
- sizeof(buffer) - 1, // 要读取的字节数
- &bytesRead, // 接收实际读取字节数的变量
- NULL // 不使用异步操作
- );
-
- if (!result) {
- std::cerr << "Error reading file." << std::endl;
- CloseHandle(hFile);
- return 1;
- }
-
- buffer[bytesRead] = '\0'; // 确保字符串以null终止
- std::cout << "Read " << bytesRead << " bytes: " << buffer << std::endl;
-
- CloseHandle(hFile);
- return 0;
- }
(7)使用时的注意事项
其它API链接:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。