@@ -743,51 +743,46 @@ private static string RetrieveProcessUserName(Process process)
743743
744744 try
745745 {
746- do
747- {
748- int error ;
749- if ( ! Win32Native . OpenProcessToken ( process . Handle , TOKEN_QUERY , out processTokenHandler ) ) { break ; }
746+ int error ;
747+ if ( ! Win32Native . OpenProcessToken ( process . Handle , TOKEN_QUERY , out processTokenHandler ) ) { return null ; }
750748
751- // Set the default length to be 256, so it will be sufficient for most cases.
752- int tokenInfoLength = 256 ;
753- tokenUserInfo = Marshal . AllocHGlobal ( tokenInfoLength ) ;
754- if ( ! Win32Native . GetTokenInformation ( processTokenHandler , Win32Native . TOKEN_INFORMATION_CLASS . TokenUser , tokenUserInfo , tokenInfoLength , out tokenInfoLength ) )
749+ // Set the default length to be 256, so it will be sufficient for most cases.
750+ int tokenInfoLength = 256 ;
751+ tokenUserInfo = Marshal . AllocHGlobal ( tokenInfoLength ) ;
752+ if ( ! Win32Native . GetTokenInformation ( processTokenHandler , Win32Native . TOKEN_INFORMATION_CLASS . TokenUser , tokenUserInfo , tokenInfoLength , out tokenInfoLength ) )
753+ {
754+ error = Marshal . GetLastWin32Error ( ) ;
755+ if ( error == Win32Native . ERROR_INSUFFICIENT_BUFFER )
755756 {
756- error = Marshal . GetLastWin32Error ( ) ;
757- if ( error == Win32Native . ERROR_INSUFFICIENT_BUFFER )
758- {
759- Marshal . FreeHGlobal ( tokenUserInfo ) ;
760- tokenUserInfo = Marshal . AllocHGlobal ( tokenInfoLength ) ;
757+ Marshal . FreeHGlobal ( tokenUserInfo ) ;
758+ tokenUserInfo = Marshal . AllocHGlobal ( tokenInfoLength ) ;
761759
762- if ( ! Win32Native . GetTokenInformation ( processTokenHandler , Win32Native . TOKEN_INFORMATION_CLASS . TokenUser , tokenUserInfo , tokenInfoLength , out tokenInfoLength ) ) { break ; }
763- }
764- else
765- {
766- break ;
767- }
760+ if ( ! Win32Native . GetTokenInformation ( processTokenHandler , Win32Native . TOKEN_INFORMATION_CLASS . TokenUser , tokenUserInfo , tokenInfoLength , out tokenInfoLength ) ) { return null ; }
768761 }
769-
770- var tokenUser = Marshal . PtrToStructure < Win32Native . TOKEN_USER > ( tokenUserInfo ) ;
771-
772- // Max username is defined as UNLEN = 256 in lmcons.h
773- // Max domainname is defined as DNLEN = CNLEN = 15 in lmcons.h
774- // The buffer length must be +1, last position is for a null string terminator.
775- int userNameLength = 257 ;
776- int domainNameLength = 16 ;
777- #pragma warning disable CA2014
778- Span < char > userNameStr = stackalloc char [ userNameLength ] ;
779- Span < char > domainNameStr = stackalloc char [ domainNameLength ] ;
780- #pragma warning restore CA2014
781- Win32Native . SID_NAME_USE accountType ;
782-
783- // userNameLength and domainNameLength will be set to actual lengths.
784- if ( ! Win32Native . LookupAccountSid ( null , tokenUser . User . Sid , userNameStr , ref userNameLength , domainNameStr , ref domainNameLength , out accountType ) )
762+ else
785763 {
786- break ;
764+ return null ;
787765 }
766+ }
767+
768+ var tokenUser = Marshal . PtrToStructure < Win32Native . TOKEN_USER > ( tokenUserInfo ) ;
769+
770+ // Max username is defined as UNLEN = 256 in lmcons.h
771+ // Max domainname is defined as DNLEN = CNLEN = 15 in lmcons.h
772+ // The buffer length must be +1, last position is for a null string terminator.
773+ int userNameLength = 257 ;
774+ int domainNameLength = 16 ;
775+ Span < char > userNameStr = stackalloc char [ userNameLength ] ;
776+ Span < char > domainNameStr = stackalloc char [ domainNameLength ] ;
777+ Win32Native . SID_NAME_USE accountType ;
788778
789- userName = string . Concat ( domainNameStr . Slice ( 0 , domainNameLength ) , "\\ " , userNameStr . Slice ( 0 , userNameLength ) ) ;
790- } while ( false ) ;
779+ // userNameLength and domainNameLength will be set to actual lengths.
780+ if ( ! Win32Native . LookupAccountSid ( null , tokenUser . User . Sid , userNameStr , ref userNameLength , domainNameStr , ref domainNameLength , out accountType ) )
781+ {
782+ return null ;
783+ }
784+
785+ userName = string . Concat ( domainNameStr . Slice ( 0 , domainNameLength ) , "\\ " , userNameStr . Slice ( 0 , userNameLength ) ) ;
791786 }
792787 catch ( NotSupportedException )
793788 {
@@ -817,7 +812,6 @@ private static string RetrieveProcessUserName(Process process)
817812 Win32Native . CloseHandle ( processTokenHandler ) ;
818813 }
819814 }
820-
821815#endif
822816 return userName ;
823817 }
0 commit comments