DuplicateHandle(GetCurrentProcess(),GetCurrentProcess(),
GetCurrentProcess(),&hReadDup,
0, FALSE,DUPLICATE_SAME_ACCESS)
的用处就不小:)
谁能解释下上面代码的用处?
6 个解决方案
#1
查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.
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.
#2
汗`
#3
极有可能为了跨进程共享handle(kernel object)
#4
复制句柄的目的主要是对句柄权限赋值和修改。
#5
复制句柄
有一个用途是:
若要得到一个线程的退出码,则可以用DuplicateHandle在创建此线程后来复制线程的句柄,
这样在线程退出后,可以使用这个复制出来的线程句柄来得到原线程的退出码(因为这个时候原线程已经退出,他本身的句柄已经失效了,而这个复制出来的句柄却可以继续使用。)
有一个用途是:
若要得到一个线程的退出码,则可以用DuplicateHandle在创建此线程后来复制线程的句柄,
这样在线程退出后,可以使用这个复制出来的线程句柄来得到原线程的退出码(因为这个时候原线程已经退出,他本身的句柄已经失效了,而这个复制出来的句柄却可以继续使用。)
#6
复制一个对象句柄!
#1
查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.
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.
#2
汗`
#3
极有可能为了跨进程共享handle(kernel object)
#4
复制句柄的目的主要是对句柄权限赋值和修改。
#5
复制句柄
有一个用途是:
若要得到一个线程的退出码,则可以用DuplicateHandle在创建此线程后来复制线程的句柄,
这样在线程退出后,可以使用这个复制出来的线程句柄来得到原线程的退出码(因为这个时候原线程已经退出,他本身的句柄已经失效了,而这个复制出来的句柄却可以继续使用。)
有一个用途是:
若要得到一个线程的退出码,则可以用DuplicateHandle在创建此线程后来复制线程的句柄,
这样在线程退出后,可以使用这个复制出来的线程句柄来得到原线程的退出码(因为这个时候原线程已经退出,他本身的句柄已经失效了,而这个复制出来的句柄却可以继续使用。)
#6
复制一个对象句柄!