Results 1 to 9 of 9

Thread: get user home directory

  1. #1

    get user home directory

    Hi! is there a way to retrieve the user home directory?
    i've tried something like this:
    [pascal]
    assign(f, '%USERPROFILE%\test.txt');
    rewrite(f);
    write(f, 'ciao');
    closefile(f);
    [/pascal]

    but it doesn't work..
    I'd like something that work for linux too.

    Bye!
    If you save your data in a proprietary format, the owner of the format owns your data.
    <br /><A href="http://msx80.blogspot.com">http://msx80.blogspot.com</A>

  2. #2

    get user home directory

    You should be able to use something like this ( NOTE Untested and thrown together from stuff off the internet )...
    [pascal]
    uses
    ActiveX,
    ComObj,
    ShlObj;

    function GetPersonalPath : string;
    var
    {$IFDEF WIN32}
    shellMalloc: IMalloc;
    ppidl: PItemIdList;
    PerDir: string;
    {$ELSE}
    ppwd PasswordRecord;
    {$ENDIF}
    begin
    {$IFDEF WIN32}
    ppidl := nil;
    try
    if SHGetMalloc(shellMalloc) = NOERROR then
    begin
    SHGetSpecialFolderLocation(0, CSIDL_PERSONAL, ppidl);
    SetLength(Result, MAX_PATH);
    if not SHGetPathFromIDList(ppidl, PChar(Result)) then
    raise exception.create('SHGetPathFromIDList failed : invalid pidl');
    SetLength(Result, lStrLen(PChar(Result)));
    end;
    finally
    if ppidl <> nil then
    shellMalloc.free(ppidl);
    end;
    {$ELSE}
    ppwd := getpwuid( getuid() );
    Result := ppwd^.pw_dir;
    {$ENDIF}
    end;
    [/pascal]

    Keep us posted if you find a better solution?
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  3. #3

    get user home directory

    Btw, for linux you could also use the GetEnvironmentVariable('HOME') method, but that will only work if the environment variable has been set.
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  4. #4

    get user home directory

    I can't find the unit that contains that calls for the Windows part. Can you post them? I hope they exists on FPC..

    thanks
    If you save your data in a proprietary format, the owner of the format owns your data.
    <br /><A href="http://msx80.blogspot.com">http://msx80.blogspot.com</A>

  5. #5

    get user home directory

    shellapi

    ahh 6-bit knight
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  6. #6

    get user home directory

    Actually, the uses clause you need is...
    [pascal]
    uses
    ActiveX,
    ComObj,
    ShlObj;
    [/pascal]

    I would be surprised if it does not exist in FPC, and if it does not, it should be fairly easy to add.
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  7. #7

    get user home directory

    After further investigation, another alternative would be...
    [pascal]
    uses
    {$IFDEF WIN32}
    ShlObj; // Only one unit, yeah!
    {$ELSE}
    libc; // Not sure where getpwuid(), getuid() and PPasswordRecord live
    {$ENDIF}

    function GetPersonalPath : string;
    var
    {$IFDEF WIN32}
    Res: Bool;
    Path: array[0..Max_Path] of Char;
    {$ELSE}
    ppwd PasswordRecord;
    {$ENDIF}
    begin
    {$IFDEF WIN32}
    Res := ShGetSpecialFolderPath(0, Path, , False);
    if not Res then raise Exception.Create(
    'Could not determine My Documents path');
    Result := Path;
    {$ELSE}
    ppwd := getpwuid( getuid() );
    Result := ppwd^.pw_dir;
    {$ENDIF}
    end;
    [/pascal]

    NOTE :if you change CSIDL_PERSONAL to CSIDL_APPDATA this will give you the path to where Applications store their data, which for games would be a much better place to store things than in the My Documents folder.
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  8. #8

    get user home directory

    Unluckly the ShlObj of fpc is empty..
    So i hacked up this ugly code. I've tested on linux and windows:

    [pascal]
    {$IFDEF WIN32}
    uses windows, Classes, SysUtils;
    const NOERROR:integer=0;
    CSIDL_APPDATA = $001a;
    type IMalloc = interface(IUnknown)
    procedure Free(pv: Pointer); stdcall;
    end;
    PItemIdList=pointer;
    function SHGetMalloc(var ppMalloc: IMalloc): HResult; stdcall; external 'shell32.dll';
    function SHGetSpecialFolderLocation(hwndOwner: HWND; nFolder: Integer; var ppidl: PItemIDList): HResult; stdcall; external 'shell32.dll';
    function SHGetPathFromIDList(pidl: PItemIDList; pszPath: PChar): BOOL; stdcall; external 'shell32.dll';
    {$ELSE}
    uses libc;
    {$ENDIF}

    function GetPersonalPath : string;
    var
    {$IFDEF WIN32}
    shellMalloc: IMalloc;
    ppidl: PItemIdList;
    PerDir: string;
    {$ELSE}
    ppwd PasswordRecord;
    {$ENDIF}
    begin
    {$IFDEF WIN32}
    ppidl := nil;
    try
    if SHGetMalloc(shellMalloc) = NOERROR then
    begin
    SHGetSpecialFolderLocation(0, CSIDL_APPDATA,
    ppidl);
    SetLength(Result, MAX_PATH);
    if not SHGetPathFromIDList(ppidl, PChar(Result)) then raise exception.create('SHGetPathFromIDList failed : invalid pidl');
    SetLength(Result, lStrLen(PChar(Result)));
    end;
    finally
    if ppidl <> nil then
    shellMalloc.free(ppidl);
    end;
    {$ELSE}
    ppwd := getpwuid( getuid() );
    Result := ppwd^.pw_dir;
    {$ENDIF}
    end;
    [/pascal]
    If you save your data in a proprietary format, the owner of the format owns your data.
    <br /><A href="http://msx80.blogspot.com">http://msx80.blogspot.com</A>

  9. #9

    get user home directory

    Thanks for letting us know.
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •