From jeremiebergeronlol at outlook.com Wed Jun 8 09:27:48 2022 From: jeremiebergeronlol at outlook.com (=?iso-8859-1?Q?J=E9r=E9mie_Bergeron?=) Date: Wed, 8 Jun 2022 13:27:48 +0000 Subject: [python-win32] Load custom font Message-ID: Hi, Is it possible to get the family name of a font and/or any other information from the naming table from a font file? Something like that: font = win32ui.loadFont(pathToTheFont) familyName = font.getNameId(1) Have a nice day -------------- next part -------------- An HTML attachment was scrubbed... URL: From steven at manross.net Thu Jun 9 12:31:02 2022 From: steven at manross.net (Steven Manross) Date: Thu, 9 Jun 2022 16:31:02 +0000 Subject: [python-win32] Load custom font In-Reply-To: References: Message-ID: <35dd22d09889416cb54f3a270126d9bb@manross.net> While this isn't win32ui, I hope it helps. pip install Pillow from PIL import ImageFont font = ImageFont.truetype("c:\\windows\\Fonts\\verdana.ttf", 28, encoding="unic") font.font.family font.font.height font.font.style google search revealed this: https://stackoverflow.com/questions/24085996/how-i-can-load-a-font-file-with-pil-imagefont-truetype-without-specifying-the-ab HTH Steven From: python-win32 On Behalf Of J?r?mie Bergeron Sent: Wednesday, June 8, 2022 6:28 AM To: python-win32 at python.org Subject: [python-win32] Load custom font Hi, Is it possible to get the family name of a font and/or any other information from the naming table from a font file? Something like that: font = win32ui.loadFont(pathToTheFont) familyName = font.getNameId(1) Have a nice day -------------- next part -------------- An HTML attachment was scrubbed... URL: From steven at manross.net Thu Jun 9 15:24:17 2022 From: steven at manross.net (Steven Manross) Date: Thu, 9 Jun 2022 19:24:17 +0000 Subject: [python-win32] Load custom font In-Reply-To: References: Message-ID: Here?s a more windows centric approach and it foreaches through all the Windows fonts: ** It?s interesting that it says that it was last modified today? odd, but whatever from win32com.client.dynamic import Dispatch ids = [0,1,2,3,4,5,6,9,10,19,21,25,33,34,58,62,165,166,167,170,191,192,193,195,197,203,255] shell_app = Dispatch('Shell.Application') shell_app.Namespace('c:\\windows\\fonts') folder = shell_app.Namespace('c:\\windows\\fonts') myfile = "" for font in folder.Items(): print(font.Name) #if font.Name == "Verdana": # break for num in ids: val = None val = folder.GetDetailsOf(font, num) item = folder.GetDetailsOf(None, num) if val: print("\t", num, f'{item} = {val}') ****************** partial output: ****************** Verdana 0 Name = Verdana 1 Font style = Regular; Bold; Bold Italic; Italic 2 Show/hide = Show 3 Designed for = Latin; Greek; Cyrillic 4 Category = Text 5 Designer/foundry = Carter + Cone 6 Font embeddability = Editable 10 Date modified = ?6/?9/?2022 ??11:16 AM From: python-win32 On Behalf Of J?r?mie Bergeron Sent: Wednesday, June 8, 2022 6:28 AM To: python-win32 at python.org Subject: [python-win32] Load custom font Hi, Is it possible to get the family name of a font and/or any other information from the naming table from a font file? Something like that: font = win32ui.loadFont(pathToTheFont) familyName = font.getNameId(1) Have a nice day -------------- next part -------------- An HTML attachment was scrubbed... URL: From steven at manross.net Thu Jun 9 15:26:52 2022 From: steven at manross.net (Steven Manross) Date: Thu, 9 Jun 2022 19:26:52 +0000 Subject: [python-win32] Load custom font In-Reply-To: References: Message-ID: <52f76c5671f6458996558b01327f923b@manross.net> Sorry? But, I need to Give credit where credit is due. https://superuser.com/questions/1432800/get-actual-font-name-of-ttf-file-from-command-line From: python-win32 On Behalf Of Steven Manross Sent: Thursday, June 9, 2022 12:24 PM To: J?r?mie Bergeron ; python-win32 at python.org Subject: Re: [python-win32] Load custom font Here?s a more windows centric approach and it foreaches through all the Windows fonts: ** It?s interesting that it says that it was last modified today? odd, but whatever from win32com.client.dynamic import Dispatch ids = [0,1,2,3,4,5,6,9,10,19,21,25,33,34,58,62,165,166,167,170,191,192,193,195,197,203,255] shell_app = Dispatch('Shell.Application') shell_app.Namespace('c:\\windows\\fonts') folder = shell_app.Namespace('c:\\windows\\fonts') myfile = "" for font in folder.Items(): print(font.Name) #if font.Name == "Verdana": # break for num in ids: val = None val = folder.GetDetailsOf(font, num) item = folder.GetDetailsOf(None, num) if val: print("\t", num, f'{item} = {val}') ****************** partial output: ****************** Verdana 0 Name = Verdana 1 Font style = Regular; Bold; Bold Italic; Italic 2 Show/hide = Show 3 Designed for = Latin; Greek; Cyrillic 4 Category = Text 5 Designer/foundry = Carter + Cone 6 Font embeddability = Editable 10 Date modified = ?6/?9/?2022 ??11:16 AM From: python-win32 > On Behalf Of J?r?mie Bergeron Sent: Wednesday, June 8, 2022 6:28 AM To: python-win32 at python.org Subject: [python-win32] Load custom font Hi, Is it possible to get the family name of a font and/or any other information from the naming table from a font file? Something like that: font = win32ui.loadFont(pathToTheFont) familyName = font.getNameId(1) Have a nice day -------------- next part -------------- An HTML attachment was scrubbed... URL: From cmatthew at optonline.net Fri Jun 17 02:41:48 2022 From: cmatthew at optonline.net (Craig R. Matthews) Date: Fri, 17 Jun 2022 02:41:48 -0400 Subject: [python-win32] Need a value from pywin32 Message-ID: I have a need to determine the "IDLE TIME" as provided by the Windows Query program. Sample output: C:\>query user /server:CTX202201 ?USERNAME????????????? SESSIONNAME??????? ID? STATE?? IDLE TIME LOGON TIME ?administrator???????? rdp-tcp#67????????? 2? Active?????? 1:38 6/15/2022 10:48 AM I can't find the above "IDLE TIME" anywhere in pywin32 "Python for Win32 Extensions". I need this time value, and would rather keep all the code in python without having to resort to something like subprocess to encapsulate the Windows Query program. Thank you. -------------- next part -------------- An HTML attachment was scrubbed... URL: From timr at probo.com Mon Jun 20 13:05:54 2022 From: timr at probo.com (Tim Roberts) Date: Mon, 20 Jun 2022 10:05:54 -0700 Subject: [python-win32] Need a value from pywin32 In-Reply-To: References: Message-ID: Craig R. Matthews wrote: > > I have a need to determine the "IDLE TIME" as provided by the Windows > Query program. > > Sample output: > C:\>query user /server:CTX202201 > ?USERNAME????????????? SESSIONNAME??????? ID? STATE?? IDLE TIME LOGON TIME > ?administrator???????? rdp-tcp#67????????? 2? Active?????? 1:38 > 6/15/2022 10:48 AM > > I can't find the above "IDLE TIME" anywhere in pywin32 "Python for > Win32 Extensions". > > I need this time value, and would rather keep all the code in python > without having to resort to something like subprocess to encapsulate > the Windows Query program. This is part of Windows Terminal Services.? The API to fetch the idle time is undocumented and unsupported, but you can find the information here: https://groups.google.com/g/microsoft.public.win32.programmer.kernel/c/xt2G599tJuQ?hl=en#91fc4e79a5d6c495 Because it is undocumented, it might be better to parse the output of "query user". -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. -------------- next part -------------- A non-text attachment was scrubbed... Name: smime.p7s Type: application/pkcs7-signature Size: 3389 bytes Desc: S/MIME Cryptographic Signature URL: From steven at manross.net Tue Jun 21 16:39:39 2022 From: steven at manross.net (Steven Manross) Date: Tue, 21 Jun 2022 20:39:39 +0000 Subject: [python-win32] Need a value from pywin32 In-Reply-To: References: Message-ID: <8b9f0e84116a49e6aa12d281cffc5365@manross.net> I was intrigued by this and I would like to get it to work, but I cannot... I know I'm doing something wrong, but don't know what. I will leave this for the archives, and maybe it will help someone else some day. My guess is that the issue is the conversion of the winsta_handle to a ctypes.c_ulonglong() is generating an invalid handle ID or the GetProcAddress isn't finding the correct info using that handle. I'm very new to C programming ''' Get Terminal Services Idle Time and other values from WINSTA.DLL for local server ----> THIS DOES NOT WORK!!!!! <---- ----> ONLY SENT AS STARTER CODE FOR SOMEONE TO FIGURE OUT WHAT I AM MISSING <---- Microsoft Info: https://docs.microsoft.com/en-us/previous-versions/aa383827(v=vs.85) Google Thread: https://groups.google.com/g/microsoft.public.win32.programmer.kernel/c/xt2G599tJuQ?hl=en&pli=1#91fc4e79a5d6c495 ''' import ctypes class WinStationInformation(ctypes.Structure): __fields__ = [ ('ConnectState', ctypes.c_long), ('WinStationName', ctypes.wintypes.WCHAR), ('LogonId', ctypes.c_ulong), ('ConnectTime', ctypes.wintypes.LARGE_INTEGER), ('DisconnectTime', ctypes.wintypes.LARGE_INTEGER), ('LastInputTime', ctypes.wintypes.LARGE_INTEGER), ('LogonTime', ctypes.wintypes.LARGE_INTEGER), ('Status', ctypes.c_int()), ('Domain', ctypes.wintypes.WCHAR * (17 + 1)), ('UserName', ctypes.wintypes.WCHAR * (20 + 1)), ('CurrentTime', ctypes.wintypes.LARGE_INTEGER), ] def get_wts_info(session_id): ''' Get WTS Info ''' # This only tries to work on the local server currently but I get an access violation running the WinStationQueryInformationW line Buf = ctypes.POINTER(WinStationInformation)() BufLen = 260 hWinSta = ctypes.windll.LoadLibrary("WINSTA.DLL") if hWinSta: winsta_handle = hWinSta._handle print(f'winsta_handle = {winsta_handle}') QueryInfoHandle = ctypes.windll.kernel32.GetProcAddress(ctypes.c_ulonglong(winsta_handle), b"WinStationQueryInformationW") # This handle is 0... possibly because of the numeric conversion from the winsta_handle to a ctypes.c_ulonglong ??? unsure # I had to convert it because the handle was generating an error as a regular value: # ArgumentError: argument 1: : int too long to convert # print(f'QueryInfoHandle = {QueryInfoHandle}') WinStationQueryInformationW = hWinSta._FuncPtr(QueryInfoHandle) RtnLen = ctypes.c_ulong() try: Result = WinStationQueryInformationW(0, session_id, 8, ctypes.byref(Buf), BufLen, ctypes.byref(RtnLen)) except Exception as e: print(f'Excepted running WinStationQueryInformationW: {e}') return False print(f'Result = {Result}') return True get_wts_info(11) # where 11 is a valid session id on the local RDP server as defined by: # Server Manager -> Remote Desktop Services -> Collections -> your Collection Name -> Connections # Right Click on the columns in Connections Tab and add "ID" to the list of columns My output: winsta_handle = 140703764119552 QueryInfoHandle = 0 Excepted running WinStationQueryInformationW: exception: access violation writing 0x0000000000000000 HTH Steven -----Original Message----- From: python-win32 On Behalf Of Tim Roberts Sent: Monday, June 20, 2022 10:06 AM To: python-win32 at python.org Subject: Re: [python-win32] Need a value from pywin32 Craig R. Matthews wrote: > > I have a need to determine the "IDLE TIME" as provided by the Windows > Query program. > > Sample output: > C:\>query user /server:CTX202201 > ?USERNAME????????????? SESSIONNAME??????? ID? STATE?? IDLE TIME LOGON > TIME > ?administrator???????? rdp-tcp#67????????? 2? Active?????? 1:38 > 6/15/2022 10:48 AM > > I can't find the above "IDLE TIME" anywhere in pywin32 "Python for > Win32 Extensions". > > I need this time value, and would rather keep all the code in python > without having to resort to something like subprocess to encapsulate > the Windows Query program. This is part of Windows Terminal Services.? The API to fetch the idle time is undocumented and unsupported, but you can find the information here: https://groups.google.com/g/microsoft.public.win32.programmer.kernel/c/xt2G599tJuQ?hl=en#91fc4e79a5d6c495 Because it is undocumented, it might be better to parse the output of "query user". -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From timr at probo.com Wed Jun 22 01:04:17 2022 From: timr at probo.com (Tim Roberts) Date: Tue, 21 Jun 2022 22:04:17 -0700 Subject: [python-win32] Need a value from pywin32 In-Reply-To: <8b9f0e84116a49e6aa12d281cffc5365@manross.net> References: <8b9f0e84116a49e6aa12d281cffc5365@manross.net> Message-ID: <356108f2-c363-1922-f4d1-d8b0bb9db982@probo.com> On 6/21/22 13:39, Steven Manross wrote: > I was intrigued by this and I would like to get it to work, but I cannot... I know I'm doing something wrong, but don't know what. I will leave this for the archives, and maybe it will help someone else some day. > ... > def get_wts_info(session_id): > ''' > Get WTS Info > ''' > # This only tries to work on the local server currently but I get an access violation running the WinStationQueryInformationW line > > Buf = ctypes.POINTER(WinStationInformation)() > BufLen = 260 > > hWinSta = ctypes.windll.LoadLibrary("WINSTA.DLL") > if hWinSta: > winsta_handle = hWinSta._handle > print(f'winsta_handle = {winsta_handle}') > QueryInfoHandle = ctypes.windll.kernel32.GetProcAddress(ctypes.c_ulonglong(winsta_handle), b"WinStationQueryInformationW") > > # This handle is 0... possibly because of the numeric conversion from the winsta_handle to a ctypes.c_ulonglong ??? unsure No, 0 is the error return that means the name was not found. You shouldn't need to use LoadLibrary and GetProcAddress.? ctypes does that for you automatically. ??? winsta = ctypes.WinDLL('winsta.dll') ??? winsta.WinStationQueryInformationW( 0, session_id, 8, ctypes.byref(Buf), BufLen, ctypes.byref(RtnLen)) If you have Visual Studio, you can try doing "link /dump /exports \windows\system32\winsta.dll" to make sure it has that entry point. -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. From steven at manross.net Wed Jun 22 11:42:28 2022 From: steven at manross.net (Steven Manross) Date: Wed, 22 Jun 2022 15:42:28 +0000 Subject: [python-win32] Need a value from pywin32 In-Reply-To: <356108f2-c363-1922-f4d1-d8b0bb9db982@probo.com> References: <8b9f0e84116a49e6aa12d281cffc5365@manross.net> <356108f2-c363-1922-f4d1-d8b0bb9db982@probo.com> Message-ID: <750aa46b96ab4ed19c9a72fd490f89de@manross.net> Thanks a lot for the help!!! This is no longer Excepting... But the Result is 0 and there doesn't seem to be any data in the Buf structure or indication that there were results in RtnLen... I get to debug this more.. YAY!!! Steven -----Original Message----- From: python-win32 On Behalf Of Tim Roberts Sent: Tuesday, June 21, 2022 10:04 PM To: python-win32 at python.org Subject: Re: [python-win32] Need a value from pywin32 On 6/21/22 13:39, Steven Manross wrote: > I was intrigued by this and I would like to get it to work, but I cannot... I know I'm doing something wrong, but don't know what. I will leave this for the archives, and maybe it will help someone else some day. > ... > def get_wts_info(session_id): > ''' > Get WTS Info > ''' > # This only tries to work on the local server currently but I get > an access violation running the WinStationQueryInformationW line > > Buf = ctypes.POINTER(WinStationInformation)() > BufLen = 260 > > hWinSta = ctypes.windll.LoadLibrary("WINSTA.DLL") > if hWinSta: > winsta_handle = hWinSta._handle > print(f'winsta_handle = {winsta_handle}') > QueryInfoHandle = > ctypes.windll.kernel32.GetProcAddress(ctypes.c_ulonglong(winsta_handle > ), b"WinStationQueryInformationW") > > # This handle is 0... possibly because of the numeric > conversion from the winsta_handle to a ctypes.c_ulonglong ??? unsure No, 0 is the error return that means the name was not found. You shouldn't need to use LoadLibrary and GetProcAddress.? ctypes does that for you automatically. ??? winsta = ctypes.WinDLL('winsta.dll') ??? winsta.WinStationQueryInformationW( 0, session_id, 8, ctypes.byref(Buf), BufLen, ctypes.byref(RtnLen)) If you have Visual Studio, you can try doing "link /dump /exports \windows\system32\winsta.dll" to make sure it has that entry point. -- Tim Roberts, timr at probo.com Providenza & Boekelheide, Inc. _______________________________________________ python-win32 mailing list python-win32 at python.org https://mail.python.org/mailman/listinfo/python-win32 From eryksun at gmail.com Wed Jun 22 16:27:59 2022 From: eryksun at gmail.com (Eryk Sun) Date: Wed, 22 Jun 2022 15:27:59 -0500 Subject: [python-win32] Need a value from pywin32 In-Reply-To: <8b9f0e84116a49e6aa12d281cffc5365@manross.net> References: <8b9f0e84116a49e6aa12d281cffc5365@manross.net> Message-ID: On 6/21/22, Steven Manross wrote: > > class WinStationInformation(ctypes.Structure): > __fields__ = [ > ('ConnectState', ctypes.c_long), > ('WinStationName', ctypes.wintypes.WCHAR), > ('LogonId', ctypes.c_ulong), > ('ConnectTime', ctypes.wintypes.LARGE_INTEGER), > ('DisconnectTime', ctypes.wintypes.LARGE_INTEGER), > ('LastInputTime', ctypes.wintypes.LARGE_INTEGER), > ('LogonTime', ctypes.wintypes.LARGE_INTEGER), > ('Status', ctypes.c_int()), > ('Domain', ctypes.wintypes.WCHAR * (17 + 1)), > ('UserName', ctypes.wintypes.WCHAR * (20 + 1)), > ('CurrentTime', ctypes.wintypes.LARGE_INTEGER), > ] The above definition is incorrect for `WinStationName` and `Status`. Defining the PROTOCOLSTATUS type for `Status` is tedious. Note also that the ctypes attribute to set is `_fields_`, not `__fields__`, so the above struct is actually defined with no fields (i.e. zero size). Here's an example that defines a get_idle_time() function, based on the difference between the current time and the last input time in the session. import ctypes from ctypes import wintypes winsta = ctypes.WinDLL('winsta', use_last_error=True) WINSTATIONNAME_LENGTH = 32 DOMAIN_LENGTH = 17 USERNAME_LENGTH = 20 MAX_THINWIRECACHE = 4 SERVERNAME_CURRENT = None LOGONID_CURRENT = -1 # WINSTATIONINFOCLASS WinStationInformation = 8 # WINSTATIONSTATECLASS State_Active = 0 State_Connected = 1 State_ConnectQuery = 2 State_Shadow = 3 State_Disconnected = 4 State_Idle = 5 State_Listen = 6 State_Reset = 7 State_Down = 8 State_Init = 9 class TSHARE_COUNTERS(ctypes.Structure): __slots__ = () _fields_ = ( ('Reserved', wintypes.ULONG), ) class PROTOCOLCOUNTERS(ctypes.Structure): __slots__ = () class SPECIFIC(ctypes.Union): __slots__ = () _fields_ = ( ('TShareCounters', TSHARE_COUNTERS), ('Reserved', wintypes.ULONG * 100), ) _fields_ = ( ('WdBytes', wintypes.ULONG), ('WdFrames', wintypes.ULONG), ('WaitForOutBuf', wintypes.ULONG), ('Frames', wintypes.ULONG), ('Bytes', wintypes.ULONG), ('CompressedBytes', wintypes.ULONG), ('CompressFlushes', wintypes.ULONG), ('Errors', wintypes.ULONG), ('Timeouts', wintypes.ULONG), ('AsyncFramingError', wintypes.ULONG), ('AsyncOverrunError', wintypes.ULONG), ('AsyncOverflowError', wintypes.ULONG), ('AsyncParityError', wintypes.ULONG), ('TdErrors', wintypes.ULONG), ('ProtocolType', wintypes.USHORT), ('Length', wintypes.USHORT), ('Specific', SPECIFIC), ) class THINWIRECACHE (ctypes.Structure): __slots__ = () _fields_ = ( ('CacheReads', wintypes.ULONG), ('CacheHits', wintypes.ULONG), ) class RESERVED_CACHE(ctypes.Structure): __slots__ = () _fields_ = ( ('ThinWireCache[', THINWIRECACHE * MAX_THINWIRECACHE), ) class CACHE_STATISTICS(ctypes.Structure): __slots__ = () class SPECIFIC(ctypes.Union): __slots__ = () _fields_ = ( ('ReservedCacheStats', RESERVED_CACHE), ('TShareCacheStats', wintypes.ULONG), ('Reserved', wintypes.ULONG * 20), ) _fields_ = ( ('ProtocolType', wintypes.USHORT), ('Length', wintypes.USHORT), ('Specific', SPECIFIC), ) class PROTOCOLSTATUS(ctypes.Structure): __slots__ = () _fields_ = ( ('Output', PROTOCOLCOUNTERS), ('Input', PROTOCOLCOUNTERS), ('Cache', CACHE_STATISTICS), ('AsyncSignal', wintypes.ULONG), ('AsyncSignalMask', wintypes.ULONG), ) class WINSTATIONINFORMATION(ctypes.Structure): __slots__ = () _fields_ = ( ('ConnectState', ctypes.c_long), ('WinStationName', wintypes.WCHAR * (WINSTATIONNAME_LENGTH + 1)), ('LogonId', wintypes.ULONG), ('ConnectTime', wintypes.LARGE_INTEGER), ('DisconnectTime', wintypes.LARGE_INTEGER), ('LastInputTime', wintypes.LARGE_INTEGER), ('LogonTime', wintypes.LARGE_INTEGER), ('Status', PROTOCOLSTATUS), ('Domain', wintypes.WCHAR * (DOMAIN_LENGTH + 1)), ('UserName', wintypes.WCHAR * (USERNAME_LENGTH + 1)), ('CurrentTime', wintypes.LARGE_INTEGER) ) winsta.WinStationQueryInformationW.restype = wintypes.BOOLEAN winsta.WinStationQueryInformationW.argtypes = ( wintypes.HANDLE, # ServerHandle wintypes.ULONG, # SessionId ctypes.c_long, # WinStationInformationClass wintypes.LPVOID, # pWinStationInformation wintypes.ULONG, # WinStationInformationLength wintypes.PULONG, # pReturnLength ) def get_idle_time(session_id=LOGONID_CURRENT, server_handle=SERVERNAME_CURRENT): info = WINSTATIONINFORMATION() rlen = wintypes.ULONG() if not winsta.WinStationQueryInformationW( server_handle, session_id, WinStationInformation, ctypes.byref(info), ctypes.sizeof(info), ctypes.byref(rlen)): raise ctypes.WinError(ctypes.get_last_error()) if info.ConnectState == State_Disconnected: last_input_time = info.DisconnectTime else: last_input_time = info.LastInputTime if not last_input_time: return None return (info.CurrentTime - last_input_time) / 1e7 --- Note that the `winsta` WinDLL instance is configured to capture the last error value (i.e. use_last_error=True). Thus if WinStationQueryInformationW() fails, the relevant OSError exception is ctypes.WinError(ctypes.get_last_error()). From steven at manross.net Wed Jun 22 18:17:47 2022 From: steven at manross.net (Steven Manross) Date: Wed, 22 Jun 2022 22:17:47 +0000 Subject: [python-win32] Need a value from pywin32 In-Reply-To: References: <8b9f0e84116a49e6aa12d281cffc5365@manross.net> Message-ID: We have a winner! Huge thanks from me even though I wasn?t the OP because I've wanted to know how to do this for a while and just started to get my feet wet in this arena!!! I now have a VERY GOOD EXAMPLE of how to start looking at doing a complex task like this with all the nested references. UGH! In [16]: get_idle_time(2) # session id = 2 Out[16]: 0.0156271 Please note that line wraps (and or a missing newline) in the previous email may cause copy/paste issues in Eryk's code around these lines: winsta.WinStationQueryInformationW.restype = wintypes.BOOLEAN # this needs to be a newline winsta.WinStationQueryInformationW.argtypes = ( wintypes.HANDLE, # ServerHandle wintypes.ULONG, # SessionId ctypes.c_long, # WinStationInformationClass wintypes.LPVOID, # pWinStationInformation wintypes.ULONG, # WinStationInformationLength wintypes.PULONG, # pReturnLength ) I HOPE THIS HELPS AND THANK YOU VERY MUCH! Steven -----Original Message----- From: Eryk Sun Sent: Wednesday, June 22, 2022 1:28 PM To: Steven Manross Cc: python-win32 at python.org Subject: Re: [python-win32] Need a value from pywin32 On 6/21/22, Steven Manross wrote: > > class WinStationInformation(ctypes.Structure): > __fields__ = [ > ('ConnectState', ctypes.c_long), > ('WinStationName', ctypes.wintypes.WCHAR), > ('LogonId', ctypes.c_ulong), > ('ConnectTime', ctypes.wintypes.LARGE_INTEGER), > ('DisconnectTime', ctypes.wintypes.LARGE_INTEGER), > ('LastInputTime', ctypes.wintypes.LARGE_INTEGER), > ('LogonTime', ctypes.wintypes.LARGE_INTEGER), > ('Status', ctypes.c_int()), > ('Domain', ctypes.wintypes.WCHAR * (17 + 1)), > ('UserName', ctypes.wintypes.WCHAR * (20 + 1)), > ('CurrentTime', ctypes.wintypes.LARGE_INTEGER), > ] The above definition is incorrect for `WinStationName` and `Status`. Defining the PROTOCOLSTATUS type for `Status` is tedious. Note also that the ctypes attribute to set is `_fields_`, not `__fields__`, so the above struct is actually defined with no fields (i.e. zero size). Here's an example that defines a get_idle_time() function, based on the difference between the current time and the last input time in the session. import ctypes from ctypes import wintypes winsta = ctypes.WinDLL('winsta', use_last_error=True) WINSTATIONNAME_LENGTH = 32 DOMAIN_LENGTH = 17 USERNAME_LENGTH = 20 MAX_THINWIRECACHE = 4 SERVERNAME_CURRENT = None LOGONID_CURRENT = -1 # WINSTATIONINFOCLASS WinStationInformation = 8 # WINSTATIONSTATECLASS State_Active = 0 State_Connected = 1 State_ConnectQuery = 2 State_Shadow = 3 State_Disconnected = 4 State_Idle = 5 State_Listen = 6 State_Reset = 7 State_Down = 8 State_Init = 9 class TSHARE_COUNTERS(ctypes.Structure): __slots__ = () _fields_ = ( ('Reserved', wintypes.ULONG), ) class PROTOCOLCOUNTERS(ctypes.Structure): __slots__ = () class SPECIFIC(ctypes.Union): __slots__ = () _fields_ = ( ('TShareCounters', TSHARE_COUNTERS), ('Reserved', wintypes.ULONG * 100), ) _fields_ = ( ('WdBytes', wintypes.ULONG), ('WdFrames', wintypes.ULONG), ('WaitForOutBuf', wintypes.ULONG), ('Frames', wintypes.ULONG), ('Bytes', wintypes.ULONG), ('CompressedBytes', wintypes.ULONG), ('CompressFlushes', wintypes.ULONG), ('Errors', wintypes.ULONG), ('Timeouts', wintypes.ULONG), ('AsyncFramingError', wintypes.ULONG), ('AsyncOverrunError', wintypes.ULONG), ('AsyncOverflowError', wintypes.ULONG), ('AsyncParityError', wintypes.ULONG), ('TdErrors', wintypes.ULONG), ('ProtocolType', wintypes.USHORT), ('Length', wintypes.USHORT), ('Specific', SPECIFIC), ) class THINWIRECACHE (ctypes.Structure): __slots__ = () _fields_ = ( ('CacheReads', wintypes.ULONG), ('CacheHits', wintypes.ULONG), ) class RESERVED_CACHE(ctypes.Structure): __slots__ = () _fields_ = ( ('ThinWireCache[', THINWIRECACHE * MAX_THINWIRECACHE), ) class CACHE_STATISTICS(ctypes.Structure): __slots__ = () class SPECIFIC(ctypes.Union): __slots__ = () _fields_ = ( ('ReservedCacheStats', RESERVED_CACHE), ('TShareCacheStats', wintypes.ULONG), ('Reserved', wintypes.ULONG * 20), ) _fields_ = ( ('ProtocolType', wintypes.USHORT), ('Length', wintypes.USHORT), ('Specific', SPECIFIC), ) class PROTOCOLSTATUS(ctypes.Structure): __slots__ = () _fields_ = ( ('Output', PROTOCOLCOUNTERS), ('Input', PROTOCOLCOUNTERS), ('Cache', CACHE_STATISTICS), ('AsyncSignal', wintypes.ULONG), ('AsyncSignalMask', wintypes.ULONG), ) class WINSTATIONINFORMATION(ctypes.Structure): __slots__ = () _fields_ = ( ('ConnectState', ctypes.c_long), ('WinStationName', wintypes.WCHAR * (WINSTATIONNAME_LENGTH + 1)), ('LogonId', wintypes.ULONG), ('ConnectTime', wintypes.LARGE_INTEGER), ('DisconnectTime', wintypes.LARGE_INTEGER), ('LastInputTime', wintypes.LARGE_INTEGER), ('LogonTime', wintypes.LARGE_INTEGER), ('Status', PROTOCOLSTATUS), ('Domain', wintypes.WCHAR * (DOMAIN_LENGTH + 1)), ('UserName', wintypes.WCHAR * (USERNAME_LENGTH + 1)), ('CurrentTime', wintypes.LARGE_INTEGER) ) winsta.WinStationQueryInformationW.restype = wintypes.BOOLEAN winsta.WinStationQueryInformationW.argtypes = ( wintypes.HANDLE, # ServerHandle wintypes.ULONG, # SessionId ctypes.c_long, # WinStationInformationClass wintypes.LPVOID, # pWinStationInformation wintypes.ULONG, # WinStationInformationLength wintypes.PULONG, # pReturnLength ) def get_idle_time(session_id=LOGONID_CURRENT, server_handle=SERVERNAME_CURRENT): info = WINSTATIONINFORMATION() rlen = wintypes.ULONG() if not winsta.WinStationQueryInformationW( server_handle, session_id, WinStationInformation, ctypes.byref(info), ctypes.sizeof(info), ctypes.byref(rlen)): raise ctypes.WinError(ctypes.get_last_error()) if info.ConnectState == State_Disconnected: last_input_time = info.DisconnectTime else: last_input_time = info.LastInputTime if not last_input_time: return None return (info.CurrentTime - last_input_time) / 1e7 --- Note that the `winsta` WinDLL instance is configured to capture the last error value (i.e. use_last_error=True). Thus if WinStationQueryInformationW() fails, the relevant OSError exception is ctypes.WinError(ctypes.get_last_error()). From nandinishree569 at gmail.com Thu Jun 23 02:12:37 2022 From: nandinishree569 at gmail.com (Nithyashree) Date: Thu, 23 Jun 2022 11:42:37 +0530 Subject: [python-win32] Regarding Licence Message-ID: Hi, I would like to use pywin32 Api for my project. Is this royalty free? Do I need prior permissions or is there anything of that sort? I saw the Licence in GitHub but didn't understand. Just wanted to know if this licence is freeware and the API can be used. Thanks. -------------- next part -------------- An HTML attachment was scrubbed... URL: From skippy.hammond at gmail.com Thu Jun 23 02:30:41 2022 From: skippy.hammond at gmail.com (Mark Hammond) Date: Thu, 23 Jun 2022 16:30:41 +1000 Subject: [python-win32] Regarding Licence In-Reply-To: References: Message-ID: <19c87430-0c99-5b92-0479-4fe552636ad2@gmail.com> Use is royalty free and you don't need permission - good luck with your project. Mark On 23/06/2022 4:12 pm, Nithyashree wrote: > Hi, > I would like to use pywin32 Api for my project. Is this royalty free? Do > I need prior permissions or is there anything of that sort? I saw the > Licence in GitHub but didn't understand. Just wanted to know if this > licence is freeware and the API can be used. > Thanks. > > _______________________________________________ > python-win32 mailing list > python-win32 at python.org > https://mail.python.org/mailman/listinfo/python-win32