Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to get Logged in user name using my own windows service application

How to get Logged in user name using my own windows service application

Scheduled Pinned Locked Moved C / C++ / MFC
csharpc++jsontutorial
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    anumadhu
    wrote on last edited by
    #1

    Hello, I am developing a windows service in VC++.Net2005 on windows XP. I want the end product should be portable to all the windows OS(atleast to win98,Me,2K,XP and Vista). In my windows service (which runs on Local System means as System's service), there is a requirement to access the current login user name. To get that i am using the GetUserName() of win32 API, but with this, i am only getting the Login-name as "SYSTEM" ( probably because of the reason that, my windows service is running in System's area(local system)). Is it feasible to get the current login name instead of getting the user in which the my windows service is running. If possible.. any clues.. Thank you

    Anee

    J J T 3 Replies Last reply
    0
    • A anumadhu

      Hello, I am developing a windows service in VC++.Net2005 on windows XP. I want the end product should be portable to all the windows OS(atleast to win98,Me,2K,XP and Vista). In my windows service (which runs on Local System means as System's service), there is a requirement to access the current login user name. To get that i am using the GetUserName() of win32 API, but with this, i am only getting the Login-name as "SYSTEM" ( probably because of the reason that, my windows service is running in System's area(local system)). Is it feasible to get the current login name instead of getting the user in which the my windows service is running. If possible.. any clues.. Thank you

      Anee

      J Offline
      J Offline
      Johpoke
      wrote on last edited by
      #2

      Now, im not sure, but maybe this might work.. getenv("USERNAME"); (It gets the USERNAME value, which also can be seen by doing set in cmd (where loads of other values are))

      /Johannes

      1 Reply Last reply
      0
      • A anumadhu

        Hello, I am developing a windows service in VC++.Net2005 on windows XP. I want the end product should be portable to all the windows OS(atleast to win98,Me,2K,XP and Vista). In my windows service (which runs on Local System means as System's service), there is a requirement to access the current login user name. To get that i am using the GetUserName() of win32 API, but with this, i am only getting the Login-name as "SYSTEM" ( probably because of the reason that, my windows service is running in System's area(local system)). Is it feasible to get the current login name instead of getting the user in which the my windows service is running. If possible.. any clues.. Thank you

        Anee

        J Offline
        J Offline
        JudyL_MD
        wrote on last edited by
        #3

        Be very careful doing this. There are a number of issues that you must address. What if the user hasn't signed on yet? What if you have two users logged on at the same time and they are going back and forth using Fast User Switching? What if the user logs off? Judy

        A 1 Reply Last reply
        0
        • A anumadhu

          Hello, I am developing a windows service in VC++.Net2005 on windows XP. I want the end product should be portable to all the windows OS(atleast to win98,Me,2K,XP and Vista). In my windows service (which runs on Local System means as System's service), there is a requirement to access the current login user name. To get that i am using the GetUserName() of win32 API, but with this, i am only getting the Login-name as "SYSTEM" ( probably because of the reason that, my windows service is running in System's area(local system)). Is it feasible to get the current login name instead of getting the user in which the my windows service is running. If possible.. any clues.. Thank you

          Anee

          T Offline
          T Offline
          ThatsAlok
          wrote on last edited by
          #4

          the hkey_local_machine is available from registry.. you canĀ  HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon for current logged on user + plus you need to write Winlogon Notification Package for same!

          "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

          cheers, Alok Gupta Global Interface Table: An Easy Way to Marshal an Interface Pointer[new] VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

          1 Reply Last reply
          0
          • J JudyL_MD

            Be very careful doing this. There are a number of issues that you must address. What if the user hasn't signed on yet? What if you have two users logged on at the same time and they are going back and forth using Fast User Switching? What if the user logs off? Judy

            A Offline
            A Offline
            anumadhu
            wrote on last edited by
            #5

            Hello friends... Thanks for your comments. I am taking help from the following article provided with Codeproject: http://www.codeproject.com/vb/net/Windows\_Service.asp What i understand from this is, we can have access to the WMI. This WMI shows all the information regarding the various processes running in machine. As i want to get the username in which i am currently logged-in, So, i am using the Win32_Process (which gives the name of process explorer... and the substring of, name of process explorer contains the currently logged-in username(eg.. user_nameprocess.exe) . Using string operations... i am trying to extract that particular username part of the process explorer name. Now,Taking help of this, i have added System.Management reference to my project (VC++.NET/windows XP) and then i got two files namely ROOT.CIMV2.Win32_Process.h/cpp with my project name. And in the ROOT.CIMV2.Win32_Process.cpp contains following lines of code: #include Stdafx.h #include ROOT.CIMV2.Win32_Process.h After this i added System refrence through the project properties. But while building this project, i am getting some errors in these new files ROOT.CIMV2.Win32_Process.h/cpp. and command line options are: C/C++ Command line options: /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /FD /EHa /MDd /Yu"stdafx.h" /Fp"Debug\myservice.pch" /Fo"Debug\\" /Fd"Debug\vc80.pdb" /W0 /nologo /c /Zi /clr:pure /TP /errorReport:prompt /FU "c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Management.dll" /FU "c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.dll" Linker Command Line Options: /OUT:"D:\Apr\2\myservice\Debug\myservice.exe" /INCREMENTAL /NOLOGO /MANIFEST /MANIFESTFILE:"Debug\myservice.exe.intermediate.manifest" /DEBUG /ASSEMBLYDEBUG /PDB:"d:\Apr\2\myservice\debug\myservice.pdb" /SUBSYSTEM:CONSOLE /MACHINE:X86 /FIXED:No /ERRORREPORT:PROMPT setupapi.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib And stdafx.h file looks like this: #pragma once #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers // TODO: reference additional headers your program requires here #define _AFXDLL /*I included this, bcoz if i don't, then i m getting an build error: D:\Microsoft Visual Studio 8\VC\atlmfc\include\afx.h(24) : fatal error C1189: #error : Building MFC application with /MD[d] (CRT dll version) requires MFC shared dll version.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups