DuplicateHandle,为什么要复制而不是仅仅获取?
为什么一个进程想要从 Win32API 调用 DuplicateHandle,并从另一个进程获取它,而不是仅仅获取某个对象本身的句柄?
调用 DuplicateHandle 或其他东西有什么好处吗?
Why would a process want to call DuplicateHandle from the Win32API, and get it from another process instead of just acquiring the handle on some object itself?
Is there some advantage to calling DuplicateHandle or something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以在“Microsoft Windows 的应用程序编程”的第 6.8 章中找到答案。
You may find the answer in Chapter 6.8 of 'Programming Applications for Microsoft Windows'.
DuplicateHandle
的一种可能用途是在 32 位进程和 64 位进程之间复制句柄。注意:不能在 I/O 完成端口或套接字上使用。
One possible use of
DuplicateHandle
is to duplicate a handle between a 32-bit process and a 64-bit process.Note: cannot be used on I/O Completion ports or Sockets.
DuplicateHandle 的另一个用途是当文件使用 FileOptions.DeleteOnClose 时在多个进程中打开文件。 (如果使用文件路径打开文件,则此类文件无法由多个进程打开)
请参阅我的答案 https://stackoverflow .com/a/36606283/2221472
Another use of DuplicateHandle is to open a file in multiple processes when the file uses
FileOptions.DeleteOnClose
. (such a file cannot be opened by multiple processes if the file path is used to open the file)See my answer at https://stackoverflow.com/a/36606283/2221472
请参阅 MSDN 上关于 '重复句柄'。我能想到的最好的方法是这样的,如果您愿意的话,可以打个比方 - 假设您使用 CreateHandle 例程打开一个仅用于写入的文件,然后调用 DuplicateHandle 将句柄传递到另一个线程,在该线程中该线程将从文件中,只有句柄被复制,因此线程不必再次调用 CreateHandle...
See here on the MSDN what it has to say about the usage of 'DuplicateHandle'. The best way I can think of it is this way, an analogy if you like - suppose you open a file using the CreateHandle routine for writing only, then you call DuplicateHandle to pass the handle onto another thread in which the thread will read from the file, only the handle is duplicated hence the thread does not have to call CreateHandle again...
当不存在句柄的任何“正常获取”时,经常使用
DuplicateHandle
:DuplicateHandle
is frequently used when there doesn't exist any "normal acquisition" of the handle: