Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

GetProfilesDirectory (userenv)
 
.
Summary
Determines the profiles directory on the current machine. ie c:\Users on Vista and later

C# Signature:

[DllImport("userenv.dll", SetLastError=true, CharSet=CharSet.Auto)]
static extern bool GetProfilesDirectory(StringBuilder path, ref int size);

Sample Code:

   StringBuilder path = new StringBuilder(4*1024);
   int size = path.Capacity;
   if (GetProfilesDirectory(path, ref size) )
   {
       ... use path value, size contains length...
   }

Sample code using gcc in Cygwin as a GUI w/ ICON executable

Needs cygwin system set up and installed.

Save code below in file usrprof.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <windows.h>
#include <w32api/windows.h>
#include <w32api/userenv.h>

int main() {
    DWORD size = 4*1024;
    char *path = malloc(size);
    if (GetProfilesDirectory(path, &size) ) {
        printf("%d %s\n", (int)size, path);
        MessageBox(NULL, path, "GetProfilesDirectory()", MB_OK);
    }
    return 0;
}

To build: run the following commands at bash command prompt, after copying an icon file, say bfe.ico, to the cwd:

echo 'this ICON bfe.ico' > this.rc

windres this.rc -O coff -o this.res

gcc -Wall -Wl,--enable-stdcall-fixup -mnop-fun-dllimport -mwindows this.res usrprof.c /cygdrive/c/windows/system32/userenv.dll

./a.exe

If you dont need an icon in the executable, just do:

gcc -Wall -Wl,--enable-stdcall-fixup -mnop-fun-dllimport -mwindows usrprof.c /cygdrive/c/windows/system32/userenv.dll

./a.exe

Contributed by John Refling

Sample code using mingw in Cygwin

Needs cygwin system set up and installed, plus mingw package

Save code below in file usrprof.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <windows.h>

//#include "c:/cygwin/usr/include/w32api/userenv.h" USE THE FOLLOWING FROM HERE
WINBOOL WINAPI GetProfilesDirectoryA(LPSTR lpProfileDir, LPDWORD lpcchSize);

int main() {
    DWORD size = 4*1024;
    char *path = malloc(size);
    if (GetProfilesDirectory(path, &size) ) {
        printf("%d %s\n", (int)size, path);
        MessageBox(NULL, path, "GetProfilesDirectory()", MB_OK);
    }
    return 0;
}

To build:

i686-pc-mingw32-gcc -Wl,--enable-stdcall-fixup -Wall userprof.c /cygdrive/c/windows/system32/userenv.dll

To run:

./a.exe

Contributed by John Refling

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions