Name of the user

Post Reply
Natter
Posts: 1244
Joined: Mon May 14, 2007 9:49 am

Name of the user

Post by Natter »

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?
User avatar
driessen
Posts: 1422
Joined: Mon Oct 10, 2005 11:26 am
Location: Genk, Belgium

Re: Name of the user

Post by driessen »

Hi,

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

Good luck.
Regards,

Michel D.
Genk (Belgium)
_____________________________________________________________________________________________
I use : FiveWin for (x)Harbour v. 24.09 - Harbour 3.2.0 (February 2024) - xHarbour Builder (January 2020) - Bcc773
ADutheil
Posts: 368
Joined: Sun May 31, 2009 6:25 pm
Location: Salvador - Bahia - Brazil

Re: Name of the user

Post by ADutheil »

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


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
Natter
Posts: 1244
Joined: Mon May 14, 2007 9:49 am

Re: Name of the user

Post by Natter »

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.
Post Reply