HANDLE handle in Windows API

In the Windows API,HANDLE is a general handle type used to represent a reference or identifier to a resource. Handle is an abstract concept that can represent various types of objects, such as files, processes, threads, events, mutexes, etc. The HANDLE type is a pointer to an internal data structure used to pass and manipulate handles between applications and the operating system.

The following is a detailed explanation and examples ofHANDLE:

  • HANDLEType:
    ``HANDLE是一个指向句柄对象的指针,它被定义为void*` type. In the Windows API, it is used to represent a reference to a resource. Since handle is an abstract concept, the specific handle type is determined by the purpose and type of the handle object, such as file handle, process handle, thread handle, etc.

  • Properties of the handle:
    The handle has the following properties:

    • A handle is a number, usually an integer, that uniquely identifies and identifies an object.
    • The handle is maintained within the operating system, and the application can only operate through the handle and cannot directly access the object's internal data.
    • A handle is a mechanism that encapsulates and protects an object, allowing the operating system to control access and operation permissions on the object.
  • Examples of using handles:
    Here are some common examples of using handles:

    1. File handle:
      When operating a file, you can use the file handle to open, read, write and other operations. For example, use the CreateFile function to open a file and return a file handle.

      HANDLE hFile = CreateFile(L"example.txt", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
      if (hFile != INVALID_HANDLE_VALUE)
      {
              
              
          // 使用hFile进行文件操作
      
          // 关闭文件句柄
          CloseHandle(hFile);
      }
      
    2. Process handle:
      When operating a process, you can use the process handle to create, terminate, wait, etc. For example, use the CreateProcess function to create a new process and return a process handle.

      HANDLE hProcess;
      BOOL bSuccess = CreateProcess(L"example.exe", NULL, NULL, NULL, FALSE, 0, NULL, NULL, NULL, &si, &pi);
      if (bSuccess)
      {
              
              
          // 获取进程句柄
          hProcess = pi.hProcess;
      
          // 使用hProcess进行进程操作
      
          // 关闭进程句柄
          CloseHandle(hProcess);
      }
      
    3. Thread handle:
      When operating a thread, you can use the thread handle to create, suspend, resume and other operations. For example, use the CreateThread function to create a new thread and return a thread handle.

      HANDLE hThread = CreateThread(NULL, 0, MyThreadProc, NULL, 0, &dwThreadId);
      if (hThread != NULL)
      {
              
              
          // 使用hThread进行线程操作
      
          // 关闭线程句柄
          CloseHandle(hThread);
      }
      

    In the above example, the handle is obtained through the corresponding function call, and the handle is used to perform corresponding object operations. After the operation is completed, you need to use the CloseHandle function to close the handle to release related resources.


In summary,HANDLE is the universal handle type in the Windows API that represents a reference to a resource. It is used to uniquely identify and operate various types of objects, such as files, processes, threads, etc. Applications can interact with objects in the operating system by obtaining a handle and performing corresponding operations using the handle.

Guess you like

Origin blog.csdn.net/ultramand/article/details/135006160