 |
|
 |
only works if explorer.exe is the shell ..
|
|
|
|
 |
|
 |
Really bad method. Especially when no explorer.exe is running (due to alternate shell, crashes, etc.)
|
|
|
|
 |
|
 |
I'm using some functions in the WTS (Windows Terminal Services) API, and it seems to work fine. From a Windows service, running as "Local System" I can determine the username of the currently logged-on Windows user. I've looked at a lot of other solutions, but this seems to be the simplest, most dependable, and most direct method. Please let me know if you know of another way to determine this. It might be possible to get the username by calling LsaEnumerateLogonSessions, and looping thru the returned session LUID's, getting the session info struct, and looking for one in which the "LogonType" is "Interactive".
...
char szTempBuf[MAX_PATH] = {0};
HANDLE hToken = NULL;
HANDLE hDupToken = NULL;
DWORD dwSessionId = WTSGetActiveConsoleSessionId();
if ( 0xFFFFFFFF == dwSessionId )
{
return;
}
WTSQueryUserToken(dwSessionId, &hToken);
if ( NULL == hToken )
{
}
DuplicateToken(hToken, SecurityImpersonation, &hDupToken);
if ( NULL == hDupToken )
{
CloseHandle(hToken);
return;
}
BOOL bRes = ImpersonateLoggedOnUser(hDupToken);
if ( bRes )
{
DWORD dwBufSize = sizeof(szTempBuf);
bRes = GetUserNameA(szTempBuf, &dwBufSize);
RevertToSelf(); if ( bRes )
{
}
}
CloseHandle(hDupToken);
CloseHandle(hToken);
...
|
|
|
|
 |
|
 |
System.Diagnostics.Process[] objArrProcess = System.DiagnosticsProcess.GetProcessesByName("explorer");
string strCurrentUserName = objArrProcess[0].StartInfo.EnvironmentVariables["username"];
regards
~pramod
|
|
|
|
 |
|
 |
Hi
is it possible in c/c++ .
Thanks,
Vinothkumar.R
|
|
|
|
 |
|
|
 |
|
 |
It's definitely a smart idea to determine the currently logged on user(s) by looking at the explorer.exe process, but please keep in mind that this is not 100% accurate.
Any local admin can change the "shell" and substitute it with a different process. There are many other processes available out there.
It would probably be better to enumerate all processes and temporarily record all usernames that are tied to processes. Then, filter out the ones that are built-in (e.g. LocalSystem, etc.) and display the remaining ones.
It's more work, but would be 100% accurate.
|
|
|
|
 |
|
 |
Hello, My application works something like this, it is a windows service. I want to access the current logged-in user-name(if only one or more users are logged-in). My development environment is VC++.Net2005 and windows XP. The exe should be compatible on win95,98,2K, XP and vista. what i did is, I have just added the refrence(System,System.Management) to project using the project properties from toolbar. And my code is as follows: CString Get_Username() { CString Username=_T(""); System::Management::ManagementObjectSearcher q = gcnew System::Management::ManagementObjectSearcher("Select * from Win32_Process"); System::Management::ManagementObjectCollection mc = q.Get(); System::Management::ManagementObject mo = mc.GetEnumerator(); while(!mo) { Username = mo("username"); mo.Get(mc); } return Username; } I am getting the following errors. error C2664: 'System::Management::ManagementObjectSearcher::ManagementObjectSearcher(System::String ^)' : cannot convert parameter 1 from 'System::Management::ManagementObjectSearcher ^' to 'System::String ^' No user-defined-conversion operator available, or Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast I have even tested by adding the using namespace System at the beginnin of my myservice.cpp file. But still the errors persists. Any idea.. please Thanks in advance Anee
|
|
|
|
 |
|
 |
hi well thanks for your article. I have a problem because i can retrieve the user log on name by Environment.UserName but i need to retrieve also his password ! Is there anyway to do so using dot net. thanks in advance Regards Soobrassen
|
|
|
|
 |
|
 |
Hey,
Have you found a solution for retrieving password. Even I want to get credentials for a running windows service & don't have a solution.
Please let me know if you have found something on this.
Thanks in advance,
SD.
|
|
|
|
 |
|
 |
Dear Baber Saeed
Thanks a lot. its exactly what i was looking for.
it will of great help if u can give the tip on how to shutdown / logoff from a windows service. i tried with
WindowsController.ExitWindows(RestartOptions.LogOff, True)
but it throws some error
|
|
|
|
 |
|
 |
Recently I came across this posting here, which was very heplful. but I keep getting an error where Return ProcessUser
The error is "'Return' statement in a Sub or a Set cannot return a value."
I copied and pasted the code directly...any ideas anyone?? page article and code below...thanks.
Create a Windows Service project using VB.NET.
Open Server Explorer and expand the tree node which says Management Classes.
Right click on Processes node and select "Generate Managed Class". This will add a reference to the System.Management namespace.
Now import System.Management namespace in your project (Service1.vb).
In the OnStart procedure, add the following code:
Dim mc As New ManagementClass("Win32_Process")
Dim moc As ManagementObjectCollection = mc.GetInstances
Dim mo As ManagementObject
Dim processDomain, processUser As String
For Each mo In moc
Dim p As New ROOT.CIMV2.Process(mo)
p.GetOwner(processDomain, processUser)
If (p.Name.Trim = "explorer.exe") Then
Return processUser
Exit For
End If
Next
|
|
|
|
 |
|
 |
Return is intended for functions and not for Sub procedures. So, instead of writing this part of coding in OnStart, we can put it in a separate function & call that function in OnStart.
Protected Overrides Sub OnStart(ByVal args() As String)
Dim curuser As String
curuser = getuname()
' u can write to a file or a msgbox this curuser value
End Sub
In the function getuname(), you shall write
Return processUser.ToString
|
|
|
|
 |
|
 |
I have followed your code and implemented user name retriever in C#.
ManagementClass objManClass = new ManagementClass("Win32_Process");
ManagementObjectCollection arrManObjects = objManClass.GetInstances();
foreach (ManagementObject objMan in arrManObjects)
{
if (objMan["Name"].ToString().Trim().ToLower() == "explorer.exe")
{
string[] arrArgs = { "", "" };
try
{
objMan.InvokeMethod("GetOwner", arrArgs);
sUserName = arrArgs[0];
}
catch (Exception ex)
{
}
}
It works fine under Win2k - in both cases - when the service is started at PC-boot or when the user manually starts it.
But under XP - it works only in case of manual start.
If the service was started automatically at boot up, the following exceptions are thrown by method GetInstances():
#1.Call was canceled by the message filter. (Exception from HRESULT: 0x80010002 (RPC_E_CALL_CANCELED))
#2.Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
#1 is thrown at the very first call and after that any try throws #2.
Any idea?
|
|
|
|
 |
|
 |
I am experiencing the same issue. Out of 1000 computers, 4 or 5 have this problem, and only sometimes, on a reboot, or a restart of the service, it works fine. I cannot figure it out. Any ideas?
I can picture in my mind a world without war, a world without hate. And I can picture us attacking that world, because they'd never expect it.
|
|
|
|
 |
|
 |
I'm having the same problem using ManagementClass. Everything works fine when the user starts my service. Not so much with an automatic start. It worked until I tested my service without using the automatic log on at boot (start->run "control userpasswords2"). I have since reinstated automatic login but it fixed nothing. Does anybody else have this experience?
|
|
|
|
 |
|
 |
How to add WMI as a dependency to your service.
"The component ServiceInstaller, in the ProjectInstaller which is added to the Solution, has the property ServicesDependedOn.
I added the string 'Windows Management Instrumentation', which is the service used by the management class (WMI)"
|
|
|
|
 |
|
 |
Thanks this was exactly what I was looking for!
|
|
|
|
 |
|
 |
What's the best way to retrieve the username of a process that is running? I can get the list of processes but can't get the username that the process is running under.
Dim pList() As System.Diagnostics.Process = System.Diagnostics.Process.GetProcesses
For Each proc As Process In pList
Debug.Writeline(proc.ProcessName)
Debug.WriteLine(">>" & proc.StartInfo.UserName) <-- This doesn't return anything
Next
Thanks
|
|
|
|
 |
|
 |
The method discribed seems to be a Visual Basic method. For us non VB folks who need a C, C++ method can you suggest how we do it in C or C++
|
|
|
|
 |
|
 |
Hi is posible using WMI for log or notify when any external program use the API WriteProcessMemory ?
Or Monitor Process the changes in hex vars in memory ?
|
|
|
|
 |
|
 |
Why not simply use the shared property Environment.UserName?
MUCH MUCH MUCH simpler
Thanks for firing me Darrell Eaker. I TRIPLED my salary and don't have to deal with morons like you.
|
|
|
|
 |
|
 |
Thank you for your valuable comment.
Well, dear I tried all API functions and class library functions to retrieve user name using a windows service but it always return "SYSTEM" as the currently logged in user which is a wrong result.
I didnt try this property Environment.UserName ... As soon as I get some time, I will check this. If you do and get correct results, please let me know.
Thanks once again
|
|
|
|
 |
|
 |
It does work. I use it in most of my programs.
|
|
|
|
 |
|
 |
Does it work in case of Windows Services?
Winners Never Quit And Quitters Never Win
|
|
|
|
 |