Lazarus

Programming => Operating Systems => Windows => Topic started by: Newmann on February 16, 2011, 01:34:05 am

Title: Windows User Name
Post by: Newmann on February 16, 2011, 01:34:05 am
Hey guys,
I'm quite new to Lazarus, but I like it quite a bit. Just a quick question- what is the easiest way to get the current user(name) of the person and/or his folders (eg My Documents, Application Settings, etc).

Most of my Delphi code I could simply copy & paste over from my older projects, but to retrieve this info seems to be a bit harder than I though...or am I missing something somewhere?

Please help, thanks ;)
Title: Re: Windows User Name
Post by: captian jaster on February 16, 2011, 02:06:31 am
Sysutils can give you the GetUserDir function.
Windows unit gives you GetUserName function..
Something like
Code: [Select]
Uses
 Windows;
 ...

Function GetUsrName:String;
Var
 Buf : PChar;

Const
 MaxDBuf:Dword = 256;

begin
  if GetUserDir(Buf,MaxDBuf)then
   Result := StrPas(Buf) Else Result := '';
end;
Should work :D
Title: Re: Windows User Name
Post by: Newmann on February 16, 2011, 11:13:01 am
It gives me the following error:

Quote
main.pas(59,28) Error: Wrong number of parameters specified for call to "GetUserDir"

I tried this one as well last night and it didn't work- only I was trying to call from "ShellAPI", not "Windows". I tried Ctrl+Click the function to see in the source what it want's to pass to it- no luck there either.

The other one I tried was:
Code: [Select]
function GetCurrentUserName : string;
const
  cnMaxUserNameLen = 254;
var
  sUserName     : string;
  dwUserNameLen : DWord;
begin
  dwUserNameLen := cnMaxUserNameLen-1;
  SetLength( sUserName, cnMaxUserNameLen );
  GetUserName(
    PChar( sUserName ),
    dwUserNameLen );
  SetLength( sUserName, dwUserNameLen );
  Result := sUserName;
end;

However, this one worked 100% this time- so I think it's safe to say that the main problem was the fact that I used ShellAPI instead of Windows.
Unfortunately this has now caused another annoying problem...everywhere I used the "Beep" procedure it gives me the following error:
Quote
main.pas(213,3) Error: Wrong number of parameters specified for call to "Beep"

I really don't know what the compiler's problem is...:)
Title: Re: Windows User Name
Post by: theo on February 16, 2011, 11:18:37 am
http://www.freepascal.org/docs-html/rtl/sysutils/getuserdir.html
Title: Re: Windows User Name
Post by: Newmann on February 17, 2011, 01:39:34 am
 :D Beautiful!!!! Was actually exactly what I needed... thanks Theo!

When I posted the message I forgot that the path to this folder not the same in WinXP and Win7...
TinyPortal © 2005-2018