def main():
sPipeName = r'\\.\pipe\test-pipe-123'
hNamedPipe = pywin32_CreateNamedPipe(sPipeName)
iState = ctypes_GetNamedPipeHandleStateW(hNamedPipe.handle)
print( f'ctypes_GetNamedPipeHandleStateW() : iState = {iState}' )
print( 'pywin32_GetNamedPipeHandleState()', pywin32_GetNamedPipeHandleState(hNamedPipe) )
def pywin32_GetNamedPipeHandleState(hPipe):
# https://mhammond.github.io/pywin32/win32pipe__GetNamedPipeHandleState_meth.html
# https://docs.microsoft.com/en-us/windows/win32/api/namedpipeapi/nf-namedpipeapi-getnamedpipehandlestatew
import win32pipe as wp
return wp.GetNamedPipeHandleState(hPipe)
def ctypes_GetNamedPipeHandleStateW(hNamedPipe):
# https://docs.microsoft.com/en-us/windows/win32/api/namedpipeapi/nf-namedpipeapi-getnamedpipehandlestatew
import ctypes as ct
from ctypes import wintypes as wt
kernel32_dll = ct.WinDLL('kernel32.dll', use_last_error=True)
fn = kernel32_dll.GetNamedPipeHandleStateW
fn.restype = wt.BOOL # return type
fn.argtypes = (
wt.HANDLE, # 1 [in] HANDLE hNamedPipe
wt.LPDWORD, # 2 [out] LPDWORD lpState
wt.LPDWORD, # 3 [out] LPDWORD lpCurInstances
wt.LPDWORD, # 4 [out] LPDWORD lpMaxCollectionCount
wt.LPDWORD, # 5 [out] LPDWORD lpCollectDataTimeout
wt.LPWSTR, # 6 [out] LPWSTR lpUserName
wt.DWORD, # 7 [in] DWORD nMaxUserNameSize
)
hNamedPipe_in = hNamedPipe # 1 [in] HANDLE hNamedPipe
lpState____out = wt.DWORD() # 2 [out] LPDWORD lpState
# 3 [out] LPDWORD lpCurInstances
# 4 [out] LPDWORD lpMaxCollectionCount
# 5 [out] LPDWORD lpCollectDataTimeout
# 6 [out] LPWSTR lpUserName
# 7 [in] DWORD nMaxUserNameSize
err = fn(
hNamedPipe_in , # 1 [in] HANDLE hNamedPipe
lpState____out, # 2 [out] LPDWORD lpState
None , # 3 [out] LPDWORD lpCurInstances
None , # 4 [out] LPDWORD lpMaxCollectionCount
None , # 5 [out] LPDWORD lpCollectDataTimeout
None , # 6 [out] LPWSTR lpUserName
0 , # 7 [in] DWORD nMaxUserNameSize
)
return lpState____out.value
def pywin32_CreateNamedPipe(sPipeName):
# https://mhammond.github.io/pywin32/win32pipe__CreateNamedPipe_meth.html
# https://docs.microsoft.com/en-us/windows/win32/api/namedpipeapi/nf-namedpipeapi-createnamedpipew
import win32pipe as wp
hNamedPipe = wp.CreateNamedPipe(
sPipeName , # pipeName
wp.PIPE_ACCESS_DUPLEX , # openMode
wp.PIPE_TYPE_MESSAGE | wp.PIPE_READMODE_MESSAGE , # pipeMode
1 , # nMaxInstances
0 , # nOutBufferSize
0 , # nInBufferSize
0 , # nDefaultTimeOut
None , # sa
)
return hNamedPipe
if __name__ == '__main__':
main()
actual behavior :
win32pipe.GetNamedPipeHandleState()
returns always errors no matter what i've tried .
Expected behavior :
to work like GetNamedPipeHandleStateW() via ctypes
Steps to reproduce the problem :
run the following test.py .
ctypes_GetNamedPipeHandleStateW() returns the correct
iState value 2 (= PIPE_READMODE_MESSAGE) .
but pywin32_GetNamedPipeHandleState() exits with the error
"Unable to impersonate using a named pipe
until data has been read from that pipe." ,
but this error is misleading , because via ctypes it works fine
and if i read from the pipe before , then
pywin32_GetNamedPipeHandleState() gives the error
"Incorrect function." , via ctypes again no problems .
test.py :
pywin32 : 304
python 64bit : 3.10.5 , 3.9.10
os : Win10 64bit enterprize 21H2