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

Date:May 11, 2016 / year-entry #99
Tags:code
Orig Link:https://blogs.msdn.microsoft.com/oldnewthing/20160511-00/?p=93446
Comments:    6
Summary:Are you sharing an object or are you creating two objects?

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.


*DISCLAIMER: I DO NOT OWN THIS CONTENT. If you are the owner and would like it removed, please contact me. The content herein is an archived reproduction of entries from Raymond Chen's "Old New Thing" Blog (most recent link is here). It may have slight formatting modifications for consistency and to improve readability.

WHY DID I DUPLICATE THIS CONTENT HERE? Let me first say this site has never had anything to sell and has never shown ads of any kind. I have nothing monetarily to gain by duplicating content here. Because I had made my own local copy of this content throughout the years, for ease of using tools like grep, I decided to put it online after I discovered some of the original content previously and publicly available, had disappeared approximately early to mid 2019. At the same time, I present the content in an easily accessible theme-agnostic way.

The information provided by Raymond's blog is, for all practical purposes, more authoritative on Windows Development than Microsoft's own MSDN documentation and should be considered supplemental reading to that documentation. The wealth of missing details provided by this blog that Microsoft could not or did not document about Windows over the years is vital enough, many would agree an online "backup" of these details is a necessary endeavor. Specifics include:

<-- Back to Old New Thing Archive Index