Lazarus
Programming => Operating Systems => Windows => Topic started 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 ;)
-
Sysutils can give you the GetUserDir function.
Windows unit gives you GetUserName function..
Something like
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
-
It gives me the following error:
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:
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:
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...:)
-
http://www.freepascal.org/docs-html/rtl/sysutils/getuserdir.html
-
: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...