What’s the difference between duplicating the handle to a token and duplicating a token?

If you have a token, there are two ways to get another handle to it. One is to use Duplicate­Handle; the other is to use Duplicate­Token (or Duplicate­Token­Ex). What's the difference?

Duplicating the handle with Duplicate­Handle results in a new handle that refers to the same token as the original handle. On the other hand, duplicating the token with Duplicate­Token produces a brand new token that is a clone of the original.

The difference is significant if you try to modify the token.

If you duplicate the handle, then there is still only one underlying token, and changes to the token from one handle will be visible to the other handle since it's all the same token at the end of the day; duplicating the handle just gives you two ways to refer to the same thing.

If you duplicate the token, then you have two tokens, and changes to one token will not affect the other.

It's the difference between buying another set of keys to a car on the one hand, and buying a brand new identical car (which comes with its own set of keys) on the other. If you buy another set of keys to the car, then there's still only one car, and if you do something to the car, that change is visible regardless of which set of keys you use. On the other hand, if you buy a new identical car, then changes to one car do not affect the other.

Comments (6)
  1. DWalker says:

    Like a “deep copy” of an object-oriented instance of an object. You can duplicate the pointer to the object, or you can duplicate the object.

    1. Alex Cohn says:

      I wonder why we need a special API to copy a “pointer”

      1. Tanveer Badar says:

        Because handles are not pointers.

        1. Cristi says:

          And because their implementation might change. One does not need to know what a handle is in order to use a handle.

  2. Yukkuri says:

    This seems super obvious… I was expecting a catch of some sort.

    1. cheong00 says:

      Indeed. Like the difference between “copying the file” and “copying the handle of file. The first one gives you two file that you can change one without affecting the other, the second one gives you two handle to access the same file.

Comments are closed.

Skip to main content