PostQueuedCompletionStatus 發送自定義 事件 觸發 GetQueuedCompletionStatus 的問題解決記錄

首先 這兩個函數 有這樣的對應關係:
BOOL WINAPI GetQueuedCompletionStatus(
  _In_   HANDLE CompletionPort,
  _Out_  LPDWORD lpNumberOfBytes,
  _Out_  PULONG_PTR lpCompletionKey,
  _Out_  LPOVERLAPPED *lpOverlapped,
  _In_   DWORD dwMilliseconds
);

BOOL WINAPI PostQueuedCompletionStatus(
  _In_      HANDLE CompletionPort,
  _In_      DWORD dwNumberOfBytesTransferred,
  _In_      ULONG_PTR dwCompletionKey,
  _In_opt_  LPOVERLAPPED lpOverlapped
);
 
CompletionPort [in]
A handle to an I/O completion port to which the I/O completion packet is to be posted.

dwNumberOfBytesTransferred [in]
The value to be returned through the lpNumberOfBytesTransferredparameter of the GetQueuedCompletionStatus function.

dwCompletionKey [in]
The value to be returned through the lpCompletionKey parameter of theGetQueuedCompletionStatus function.

lpOverlapped [in, optional]
The value to be returned through the lpOverlapped parameter of theGetQueuedCompletionStatus function.
 

此間主要有兩個問題沒有理清楚:api

1.沒有想明白 第三個參數 _In_  ULONG_PTR dwCompletionKey, 能夠是自定義的數據類型, 例如int, 個人工程中傳值爲 iocp_api::Accept(=2), 即簡單的 int 數值 值爲 2, 那麼在app

BOOL WINAPI GetQueuedCompletionStatus(
  _In_   HANDLE CompletionPort,
  _Out_  LPDWORD lpNumberOfBytes,
  _Out_  PULONG_PTR lpCompletionKey,
  _Out_  LPOVERLAPPED *lpOverlapped,
  _In_   DWORD dwMilliseconds
);
函數 的 _Out_  PULONG_PTR lpCompletionKey 的值就會是 2, 個人問題出在「進行普通的 GetQueuedCompletionStatus() 處理時, 參數是返回地址指針類型「 , 沒有意識到 該指針的值 就是 0x00000002

2.沒有注意到 兩個函數的 微妙非堆成區別:
BOOL WINAPI GetQueuedCompletionStatus( ...,  _Out_  LPOVERLAPPED *lpOverlapped, ... );
BOOL WINAPI PostQueuedCompletionStatus( ...,  _In_opt_  LPOVERLAPPED lpOverlapped );
GetQueuedCompletionStatus() 要求的是 LPOVERLAPPED * 
而 PostQueuedCompletionStatus() 是 LPOVERLAPPED
相關文章
相關標籤/搜索