Name of the user

Name of the user

Postby Natter » Wed Dec 08, 2021 7:52 am

Hi,

I'm looking through the list of processes via WMI.
Is it possible to find out the name of the user on whose behalf the process is opened?
Natter
 
Posts: 1120
Joined: Mon May 14, 2007 9:49 am

Re: Name of the user

Postby driessen » Thu Dec 09, 2021 12:24 pm

Hi,

Why don't you use this function : WNetGetUser()

Good luck.
Regards,

Michel D.
Genk (Belgium)
_____________________________________________________________________________________________
I use : FiveWin for (x)Harbour v. 24.02 - Harbour 3.2.0 (February 2024) - xHarbour Builder (January 2020) - Bcc77
User avatar
driessen
 
Posts: 1396
Joined: Mon Oct 10, 2005 11:26 am
Location: Genk, Belgium

Re: Name of the user

Postby ADutheil » Thu Dec 09, 2021 3:06 pm

driessen wrote:Hi,

Why don't you use this function : WNetGetUser()

Good luck.

I guess probably because he's looking for someone connected to the server or locking a file.

You can use the following function to start working.

Code: Select all  Expand view

FUNCTION TstUser()
LOCAL cDest := "procown.ps1"
LOCAL   nHndl

nHndl := fCreate( cDest )
fClose( nHndl )
IF ( nHndl := fOpen( cDest, 17 ) ) != -1 // write+exclusive
    fWrite( nHndl, [$proc = Get-CimInstance Win32_Process -Filter "name = 'notepad.exe'"]  + HB_OsNewline() )
    fWrite( nHndl, [Invoke-CimMethod -InputObject $proc -MethodName GetOwner >procown.txt]  + HB_OsNewline() )
    fClose( nHndl )
ENDIF
WinExec( "powershell.exe -windowstyle hidden -File procown.ps1" )
RETURN NIL
 


It creates a text file which is easy to treat to get the user name but it has 2 caveats: it opens a shell window that appears shortly and the function has to be executed as administrator dues to default Windows restriction to run PowerShell scripts.
The first caveat might be resolved by using PsRun available at https://github.com/gbuktenica/PsRun. If it is possible you can avoid the second one by changing the execution policy in registry.
If I get some spare time I'll try another approach by using OLE.
Regards,

André Dutheil
FWH 13.04 + HB 3.2 + MSVS 10
ADutheil
 
Posts: 368
Joined: Sun May 31, 2009 6:25 pm
Location: Salvador - Bahia - Brazil

Re: Name of the user

Postby Natter » Thu Dec 09, 2021 4:39 pm

Thanks, ADutheil !

To prevent the Powershell shell window from appearing, the script must be run through Wscript and cmd.exe .
Of course, I made a flag DBF, where each launching application locks its own line.
But it seemed to me that by viewing all processes through WMI, you can find out the name of the user on whose behalf a particular process is opened.
Natter
 
Posts: 1120
Joined: Mon May 14, 2007 9:49 am


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 85 guests