哪位兄弟帮我查查::DuplicateHandle()的用法

foenix 2000-07-21 04:55:00
我这里先谢谢了!
...全文
414 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
土豆 2000-07-22
  • 打赏
  • 举报
回复
一句话,用于复制进程或线程句柄
guojin 2000-07-22
  • 打赏
  • 举报
回复
我想,你大概是想知道DuplicateHandle()该如何用.
在系统中,对象分两类:核心对象和用户对象.如进程对象,线程对象,文件映射
对象等就是核心对象;而向窗口,菜单等都是用户对象.
两者是有差别的,用于标示用户对象的句柄是系统唯一的,也就是说,一个进程
完全可以对另外一个进程中的用户对象进行操作,比如两个进程间通信的方法之一,
就是发送消息.正是由于窗口是用户对象,所以句柄是系统唯一,通过FindWindow(),
得到另外一个进程的窗口句柄,然后用SendMessage(),让hWnd的窗口过程来处理消
息,实现了进程间的通信.因此,对于用户对象,你根本不用DuplicateHandle(),直接
把句柄拿来用就行了.
而核心对象则不一样.核心对象是为了加强系统的稳定性,因此,核心对象句柄是
进程相关的,在每一个进程中都有一个核心对象表,每一个对象的索引(不完全是)作为内和对象的句柄,从而实现进程相关.同一个对象在不同的进程中可能有不同的索引,即句柄.对核心对象进行操作时,系统还要进行安全检验,看一下你是否有权来操作这个对象.因此你不能同用户对象一样,直接把句柄拿过来用.比方说,你想操作另一个进程中的文件映射对象,这个文件映射对象句柄在那个进程中假设是0x000001,但在你的进程中,很有可能0x00000001时表示另一个核心对象,此时的操作就永远不会成功,甚至会产生灾难性的后果.此时,就有必要用DuplicateHandle().
DuplicateHandle(),要涉及到三个进程,(一般很少有这种情况,最多的是两个进程).
第一个是你调用DuplicateHandle()函数的线程.第二个是你的源进程,第三个是目的进程.
hSourceHandle,是你要复制的核心对象句柄.调用这个函数后,系统将检索对象名空间,
找到内核对象,增加计数,然后在你的目的进程中的核心对象表中,为这个对象分配一个
索引,这个索引作为目的进程中该内核对象的句柄.然后你还要通知(发消息)目的进程说
"喂,你现在可以用这个核心对象了",目的进程才可以操作该对象.
不知道,我回答的清不清楚?


zzh 2000-07-21
  • 打赏
  • 举报
回复
查MSDN:用法如下
The DuplicateHandle function duplicates an object handle.

BOOL DuplicateHandle(
HANDLE hSourceProcessHandle, // handle to the source process
HANDLE hSourceHandle, // handle to duplicate
HANDLE hTargetProcessHandle, // handle to process to duplicate to
LPHANDLE lpTargetHandle, // pointer to duplicate handle
DWORD dwDesiredAccess, // access for duplicate handle
BOOL bInheritHandle, // handle inheritance flag
DWORD dwOptions // optional actions
);

Parameters
hSourceProcessHandle
Handle to the process with the handle to duplicate.
Windows NT: The handle must have PROCESS_DUP_HANDLE access.

hSourceHandle
Handle to duplicate. This is an open object handle that is valid in the context of the source process. For a list of objects whose handles can be duplicated, see the following Remarks section.
hTargetProcessHandle
Handle to the process that is to receive the duplicated handle. The handle must have PROCESS_DUP_HANDLE access.
lpTargetHandle
Pointer to a variable receiving the value of the duplicate handle. This handle value is valid in the context of the target process.
If lpTargetHandle is NULL, the function duplicates the handle, but does not return the duplicate handle value to the caller. This behavior exists only for backward compatibility with previous versions of this function. You should not use this feature, as you will lose system resources until the target process terminates.

dwDesiredAccess
Specifies the access requested for the new handle. This parameter is ignored if the dwOptions parameter specifies the DUPLICATE_SAME_ACCESS flag. Otherwise, the flags that can be specified depend on the type of object whose handle is being duplicated. For the flags that can be specified for each object type, see the following Remarks section. Note that the new handle can have more access than the original handle.
bInheritHandle
Indicates whether the handle is inheritable. If TRUE, the duplicate handle can be inherited by new processes created by the target process. If FALSE, the new handle cannot be inherited.
dwOptions
Specifies optional actions. This parameter can be zero, or any combination of the following flags: Value Meaning
DUPLICATE_CLOSE_SOURCE Closes the source handle. This occurs regardless of any error status returned.
DUPLICATE_SAME_ACCESS Ignores the dwDesiredAccess parameter. The duplicate handle has the same access as the source handle.


Return Values
If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.
hyzx2000 2000-07-21
  • 打赏
  • 举报
回复
PB中原型:
FUNCTION ulong DuplicateHandle(ulong hSourceProcessHandle,ulong hSourceHandle,ulong hTargetProcessHandle,ref ulong lpTargetHandle,ulong dwDesiredAccess,ulong bInheritHandle,ulong dwOptions) LIBRARY "kernel32.dll"

具体用法:关注

16,466

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧