欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > Windows 编程中常见的数据类型

Windows 编程中常见的数据类型

2025/5/7 11:54:52 来源:https://blog.csdn.net/book_dw5189/article/details/141286475  浏览:    关键词:Windows 编程中常见的数据类型

在 Windows 编程中,尤其是使用 Win32 API 时,常见的数据类型包括:

1. 整型类型

  • BYTE: 8 位无符号整数,范围是 0 到 255。
  • SHORT: 16 位有符号整数,范围是 -32,768 到 32,767。
  • WORD: 16 位无符号整数,范围是 0 到 65,535。
  • DWORD: 32 位无符号整数,范围是 0 到 4,294,967,295。
  • LONG: 32 位有符号整数,范围是 -2,147,483,648 到 2,147,483,647。
  • LONGLONG: 64 位有符号整数,范围是 -9,223,372,036,854,775,808 到 9,223,372,036,854,775,807。
  • ULONG: 32 位无符号整数,等同于 DWORD
  • ULONGLONG: 64 位无符号整数,等同于 LONGLONG

2. 字符类型

  • CHAR: 8 位字符,通常用于 ASCII 字符。
  • WCHAR: 16 位字符,通常用于 Unicode 字符。
  • TCHAR: 根据编译环境的设置(例如 _UNICODE 宏)可以是 char 或 wchar_t,用于编写可以在 ANSI 和 Unicode 模式下编译的代码。

3. 布尔类型

  • BOOL: 32 位布尔值,通常是 TRUE 或 FALSE。在 Windows API 中,BOOL 实际上是一个 32 位的整数类型,TRUE 和 FALSE 分别对应 1 和 0。

4. 指针类型

  • LPVOID: 指向任意类型的指针,通常用于函数参数或返回值,允许处理不同类型的数据。
  • LPCVOID: 指向常量的 LPVOID,表示只读内存区域。
  • LPSTR: 指向字符数组的指针,用于 ANSI 字符串。
  • LPCSTR: 指向常量字符数组的指针,用于 ANSI 字符串。
  • LPWSTR: 指向宽字符数组的指针,用于 Unicode 字符串。
  • LPCWSTR: 指向常量宽字符数组的指针,用于 Unicode 字符串。

5. 句柄类型

  • HANDLE: 通用的句柄类型,用于表示对象的引用,如文件、窗口、进程等。具体类型通常需要强制转换为正确的类型(如 HANDLEHWNDHINSTANCE 等)。

6. 其他类型

  • DWORD_PTR: 适用于指针或整数的无符号数据类型,其大小根据平台(32 位或 64 位)不同。
  • LONG_PTR: 适用于指针或整数的有符号数据类型,其大小根据平台(32 位或 64 位)不同。
  • SIZE_T: 用于表示对象大小的类型,其大小根据平台(32 位或 64 位)不同。
  • SSIZE_T: 与 SIZE_T 类似,但为有符号类型,用于表示可能为负的大小值。

这些类型定义在 Windows 的头文件中,如 <windows.h>,可以方便地用于各种系统级编程任务。

------------

The C Standard Library provides a collection of functions, macros, and types that simplify common programming tasks. It is divided into several header files, each offering different functionalities. Here’s a quick overview:

  • <stdio.h>: Functions for input and output (e.g., printfscanffopen).
  • <stdlib.h>: Utilities for memory allocation, process control, and conversions (e.g., mallocfreeexit).
  • <string.h>: String manipulation functions (e.g., strcpystrlenstrcmp).
  • <math.h>: Mathematical functions (e.g., sqrtpowsin).
  • <ctype.h>: Character classification and conversion (e.g., isalphatoupper).
  • <time.h>: Time and date functions (e.g., timelocaltimestrftime).
  • <limits.h>: Limits of integer types (e.g., INT_MAXCHAR_BIT).
  • <float.h>: Limits of floating-point types (e.g., FLT_MAXDBL_EPSILON).
  • <assert.h>: Debugging aids (e.g., assert).
  • <errno.h>: Error handling (e.g., errnoperror).
  • <locale.h>: Locale-specific information (e.g., setlocalelocaleconv).
  • <stdarg.h>: Handling variable argument lists (e.g., va_listva_start).
  • <signal.h>: Signal handling (e.g., signalraise).
  • <stdnoreturn.h>: Specifies functions that do not return (e.g., noreturn).

These headers and their functions help manage tasks like memory management, file operations, mathematical computations, and more, providing a foundation for C programming.

-----------

Windows API 的头文件包括了各种功能模块。以下是一些常用的头文件及其功能:

  • windows.h:包含了基本的 Windows API 函数、数据类型和宏。几乎所有的 Windows 应用程序都需要这个头文件。

  • winuser.h:定义了与窗口、消息和用户界面相关的函数和数据类型。

  • winbase.h:提供了基本的 Windows 基础函数,如文件操作、进程管理等。

  • winsock2.h:用于网络编程,包含了与套接字(Socket)相关的函数和数据类型。

  • shellapi.h:提供了与 Windows Shell 相关的函数和数据类型。

  • commctrl.h:定义了常用控件的函数和消息,如按钮、列表框等。

  • processthreadsapi.h:包含了进程和线程管理相关的函数。

这些头文件通常由 windows.h 自动包含,但你也可以根据需要单独包含它们。如果你使用特定功能或模块时,确保包含相应的头文件。

------------------------

#include <windows.h>
#include <stdio.h>int main() {HKEY hKey;LONG result;char value[256];DWORD valueLength = sizeof(value);// 打开注册表键result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", 0, KEY_READ, &hKey);if (result != ERROR_SUCCESS) {printf("无法打开注册表键。错误代码: %ld\n", result);return 1;}// 查询注册表值result = RegQueryValueEx(hKey, "ProductName", NULL, NULL, (LPBYTE)value, &valueLength);if (result == ERROR_SUCCESS) {printf("Windows 产品名称: %s\n", value);} else {printf("无法读取注册表值。错误代码: %ld\n", result);}// 关闭注册表键RegCloseKey(hKey);return 0;
}
#include <windows.h>
#include <stdio.h>void list_files(const char *path) {WIN32_FIND_DATA findFileData;HANDLE hFind = FindFirstFile(path, &findFileData);if (hFind == INVALID_HANDLE_VALUE) {// Correct format specifier for DWORD (unsigned long)printf("FindFirstFile failed (%lu)\n", (unsigned long)GetLastError());return;}do {// 检查文件是否是目录if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {printf("Directory: %s\n", findFileData.cFileName);} else {printf("File: %s\n", findFileData.cFileName);}} while (FindNextFile(hFind, &findFileData) != 0);FindClose(hFind);
}int main() {list_files("C:\\Users\\Administrator\\Documents\\*"); // 使用 * 列出所有文件和目录return 0;
}// WIN32_FIND_DATA 结构体定义
// typedef struct _WIN32_FIND_DATA {
//     DWORD dwFileAttributes;       // 文件的属性
//     FILETIME ftCreationTime;      // 文件的创建时间
//     FILETIME ftLastAccessTime;    // 文件的最后访问时间
//     FILETIME ftLastWriteTime;     // 文件的最后写入时间
//     DWORD nFileSizeHigh;          // 文件大小的高32位
//     DWORD nFileSizeLow;           // 文件大小的低32位
//     DWORD dwReserved0;            // 保留字段
//     DWORD dwReserved1;            // 保留字段
//     TCHAR cFileName[MAX_PATH];    // 文件名
//     TCHAR cAlternateFileName[14]; // 文件的备用名称
// } WIN32_FIND_DATA;// 字段说明
// dwFileAttributes:
// 文件或目录的属性标志。可以是下列之一(或其组合):
// FILE_ATTRIBUTE_DIRECTORY:这是一个目录。
// FILE_ATTRIBUTE_ARCHIVE:文件已更改,自上次备份以来。
// FILE_ATTRIBUTE_HIDDEN:文件是隐藏文件。
// FILE_ATTRIBUTE_READONLY:文件是只读的。
// FILE_ATTRIBUTE_SYSTEM:文件是系统文件。
// FILE_ATTRIBUTE_NORMAL:文件没有其他属性(除了存储和读/写访问)。
// ftCreationTime:
// 文件或目录的创建时间。类型为 FILETIME 结构体,用于表示日期和时间。
// ftLastAccessTime:
// 文件或目录的最后访问时间。类型为 FILETIME 结构体。
// ftLastWriteTime:
// 文件或目录的最后写入时间。类型为 FILETIME 结构体。
// nFileSizeHigh 和 nFileSizeLow:
// 文件的大小。由于文件大小可能大于 2GB,因此它被分为高 32 位 (nFileSizeHigh) 和低 32 位 (nFileSizeLow)。
// dwReserved0 和 dwReserved1:
// 保留字段,通常不需要使用或设置这些字段。
// cFileName:
// 文件的名称。这个字段是一个数组,最大长度为 MAX_PATH(通常为 260 个字符)。
// cAlternateFileName:
// 文件的备用名称,通常用于 DOS 兼容性。这个字段的长度为 14 个字符。

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>int main() {// 使用 <stdio.h> 的函数printf("Hello, World!\n");// 使用 <stdlib.h> 的函数// Allocate memory for a character array instead of an integer arraychar *arr = (char *)malloc(100 * sizeof(char)); // Allocate memory for a stringif (arr == NULL) {printf("Memory allocation failed\n");return 1;}// 使用 <string.h> 的函数strcpy(arr, "Example string");printf("String: %s\n", arr);// 使用 <math.h> 的函数double result = sqrt(25.0);printf("Square root of 25 is: %f\n", result);// 释放内存free(arr);return 0;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词