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. Is there any Win32 API to get users home directory

Is there any Win32 API to get users home directory

Scheduled Pinned Locked Moved C / C++ / MFC
jsonhelpquestion
8 Posts 5 Posters 1 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.
  • N Offline
    N Offline
    Nandu_77b
    wrote on last edited by
    #1

    HI Any can help me, to get the users home directory in windows. Is there any win32 API avilable for it? Home directory means ::C:\Documents and Settings\ Please help with possible ways to get this thro program. Thanks, Nandu

    N J K 3 Replies Last reply
    0
    • N Nandu_77b

      HI Any can help me, to get the users home directory in windows. Is there any win32 API avilable for it? Home directory means ::C:\Documents and Settings\ Please help with possible ways to get this thro program. Thanks, Nandu

      N Offline
      N Offline
      Nandu_77b
      wrote on last edited by
      #2

      Home dir:C:\Documents and Settings\userid

      1 Reply Last reply
      0
      • N Nandu_77b

        HI Any can help me, to get the users home directory in windows. Is there any win32 API avilable for it? Home directory means ::C:\Documents and Settings\ Please help with possible ways to get this thro program. Thanks, Nandu

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

        SHGetSpecialFolderPath[^]

        1 Reply Last reply
        0
        • N Nandu_77b

          HI Any can help me, to get the users home directory in windows. Is there any win32 API avilable for it? Home directory means ::C:\Documents and Settings\ Please help with possible ways to get this thro program. Thanks, Nandu

          K Offline
          K Offline
          krmed
          wrote on last edited by
          #4

          You could also use GetEnvironmentVariable:

          TCHAR tcDirName[_MAX_PATH];
          GetEnvironmentVariable(_T("userprofile"), tcDirName, _MAX_PATH);

          This environment variable is set when the user logs on. Note that it is not always on the same drive as the system (C:). Hope that helps.

          Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

          N 1 Reply Last reply
          0
          • K krmed

            You could also use GetEnvironmentVariable:

            TCHAR tcDirName[_MAX_PATH];
            GetEnvironmentVariable(_T("userprofile"), tcDirName, _MAX_PATH);

            This environment variable is set when the user logs on. Note that it is not always on the same drive as the system (C:). Hope that helps.

            Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

            N Offline
            N Offline
            Nandu_77b
            wrote on last edited by
            #5

            Thanks for your reply, is there any specific win32 API who will do this job. As i need to use this in win32 API programming, not in MFC or higher framwork. -Nandu

            K N 2 Replies Last reply
            0
            • N Nandu_77b

              Thanks for your reply, is there any specific win32 API who will do this job. As i need to use this in win32 API programming, not in MFC or higher framwork. -Nandu

              K Offline
              K Offline
              krmed
              wrote on last edited by
              #6

              That is Win32 API call.

              Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

              1 Reply Last reply
              0
              • N Nandu_77b

                Thanks for your reply, is there any specific win32 API who will do this job. As i need to use this in win32 API programming, not in MFC or higher framwork. -Nandu

                N Offline
                N Offline
                Nibu babu thomas
                wrote on last edited by
                #7

                Nandu_77b wrote:

                is there any specific win32 API who will do this job

                Yes, use GetUserProfileDirectory - retrieves the path to the root directory of the specified user's profile. For e.g. C:\Documents and Settings\Nibu A function which uses ths API to return current process user home dir...

                #include "Userenv.h"
                #pragma comment(lib, "userenv.lib")
                CString GetUserHomeDir()
                {
                TCHAR szHomeDirBuf[MAX_PATH] = { 0 };

                HANDLE hToken = 0;
                VERIFY( OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &hToken ));

                DWORD BufSize = MAX_PATH;
                VERIFY( GetUserProfileDirectory( hToken, szHomeDirBuf, &BufSize ));

                CloseHandle( hToken );
                return CString( szHomeDirBuf );
                }

                Then there are other functions too which might interest you, like... GetAllUsersProfileDirectory GetDefaultUserProfileDirectory GetProfilesDirectory Look up these in MSDN for more information.

                Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

                modified on Wednesday, June 4, 2008 11:55 PM

                M 1 Reply Last reply
                0
                • N Nibu babu thomas

                  Nandu_77b wrote:

                  is there any specific win32 API who will do this job

                  Yes, use GetUserProfileDirectory - retrieves the path to the root directory of the specified user's profile. For e.g. C:\Documents and Settings\Nibu A function which uses ths API to return current process user home dir...

                  #include "Userenv.h"
                  #pragma comment(lib, "userenv.lib")
                  CString GetUserHomeDir()
                  {
                  TCHAR szHomeDirBuf[MAX_PATH] = { 0 };

                  HANDLE hToken = 0;
                  VERIFY( OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &hToken ));

                  DWORD BufSize = MAX_PATH;
                  VERIFY( GetUserProfileDirectory( hToken, szHomeDirBuf, &BufSize ));

                  CloseHandle( hToken );
                  return CString( szHomeDirBuf );
                  }

                  Then there are other functions too which might interest you, like... GetAllUsersProfileDirectory GetDefaultUserProfileDirectory GetProfilesDirectory Look up these in MSDN for more information.

                  Nibu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com

                  modified on Wednesday, June 4, 2008 11:55 PM

                  M Offline
                  M Offline
                  Member 8534035
                  wrote on last edited by
                  #8

                  Is the given code correct? I am wondering about the return instruction, in which a CString object is created: return CString( szHomeDirBuf ); In my opinion this object will be already destroyed while the function returns. When accessing from caller, the object is already invalid. CString res = GetUserHomeDir(); Am I wrong?

                  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