Search code examples
c++winapiwindows-kernel

How to share kernel objects, such as Waitable Timers, between processes?


I am creating an object, for example a Waitable Timer, and I want to share my object with another process:

// In process 1:
HANDLE hTimerProcess1 = CreateWaitableTimer(NULL, FALSE, L"Time1");
// In process 2:
HANDLE hTimerProcess2 = CreateWaitableTimer(NULL, FALSE, L"Time1");
  1. As the third parameter, I am passing the same name "Time1", does that mean that I am creating a new kernel object hTimerProcess2 but with the same descriptor table entry as hTimerProcess1, or is hTimerProcess2 just a reference to the existing object (hTimerProcess1)?
  2. How can I prove that hTimerProcess1 and hTimerProcess2 are the same kernel object? I tried getting their address but they are different.

I'm reading M. Richter's "Advanced Win32 Programming"


Solution

  • From the docs to CreateWaitableTimer:

    If the named timer object exists before the function call, the function returns a handle to the existing object and GetLastError returns ERROR_ALREADY_EXISTS.

    So the answer to your question #1 is no, you're not creating a new kernel object - if both calls succeed, they'll refer to the same timer.

    How to prove they're the same? You could probably do this by calling SetWaitableTimer in one process, and waiting on it in the other.

    If you want to make extra sure that you get the same timer object in the second process, call OpenWaitableTimer instead - since this only succeeds if the timer already exists.