The WaitForMultipleObjects() function does not work correctly with pseudohandles in Windows XP, in Windows Server 2003, or in Windows Vista

Article translations Article translations
Article ID: 951322 - View products that this article applies to.
Expand all | Collapse all

SYMPTOMS

The WaitForMultipleObjects() function does not work correctly together with pseudohandles in Windows XP, in Windows Server 2003, or in Windows Vista.

Consider the following sample code.
int _tmain(int argc, _TCHAR* argv[])
{
      
      HANDLE hnd[1]; 
	long res; 
	hnd[0] = GetCurrentProcess(); 

        res = WaitForMultipleObjects(1, hnd, FALSE, 3000);
      return 0;
}
After you run the sample code, you expect the function to return after three seconds. However, it returns a "WAIT_FAILED" error, and it does not wait.

WORKAROUND

To work around this issue, use the DuplicateHandle() function to convert the pseudohandles to a real process handle.

For example, use the following sample code.
int _tmain(int argc, _TCHAR* argv[])
{
      
      HANDLE hnd[1]={0};
      long res; 
      HANDLE Threadhandle = ::GetCurrentThread();
      
      DuplicateHandle(GetCurrentProcess(), Threadhandle, 
      GetCurrentProcess(),
      &hnd[0], 
      0,
      FALSE,
      DUPLICATE_SAME_ACCESS);

      res = WaitForMultipleObjects(1, hnd, TRUE, 10000);

      return 0;
}

MORE INFORMATION

This issue occurs because the pseudohandle is a special constant that is interpreted as the current thread or the process handle. The calling thread can use this handle to specify itself when a thread handle is required. These handles cannot be referred by other threads. A pseudohandle is always interpreted as referring to the thread that is using it.

The pseudohandle does not have to be closed when it is no longer needed. Calling the CloseHandle() function for this handle has no effect.

A process uses the GetCurrentProcess() function or the GetCurrentHandle() function to obtain a handle to itself. This handle is a pseudohandle. However, the DuplicateHandle() function converts it to a real process handle.

Properties

Article ID: 951322 - Last Review: April 30, 2008 - Revision: 1.0
APPLIES TO
  • Microsoft Windows Server 2003, Standard Edition (32-bit x86)
  • Microsoft Windows Server 2003, Enterprise Edition (32-bit x86)
  • Microsoft Windows Server 2003, Datacenter Edition (32-bit x86)
  • Microsoft Windows Server 2003, Standard x64 Edition
  • Microsoft Windows Server 2003, Enterprise x64 Edition
  • Microsoft Windows Server 2003, Datacenter x64 Edition
  • Microsoft Windows Server 2003, Enterprise Edition for Itanium-based Systems
  • Microsoft Windows Server 2003, Datacenter Edition for Itanium-Based Systems
  • Microsoft Windows XP Professional
  • Microsoft Windows XP Home Edition
  • Windows Vista Business
  • Windows Vista Enterprise
  • Windows Vista Home Basic
  • Windows Vista Home Premium
  • Windows Vista Ultimate
  • Windows Vista Business 64-bit Edition
  • Windows Vista Enterprise 64-bit Edition
  • Windows Vista Home Basic 64-bit Edition
  • Windows Vista Home Premium 64-bit Edition
  • Windows Vista Ultimate 64-bit Edition
Keywords: 
kbexpertiseinter kbtshoot kbprb KB951322

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com