Recent

Author Topic: Windows User Name  (Read 12452 times)

Newmann

  • Jr. Member
  • **
  • Posts: 62
Windows User Name
« 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 ;)
Lazarus 1.0.14
FPC 2.6.0
Win7 Pro 64bit/Win XP Pro 32bit

captian jaster

  • Guest
Re: Windows User Name
« Reply #1 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

Newmann

  • Jr. Member
  • **
  • Posts: 62
Re: Windows User Name
« Reply #2 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...:)
Lazarus 1.0.14
FPC 2.6.0
Win7 Pro 64bit/Win XP Pro 32bit

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1927

Newmann

  • Jr. Member
  • **
  • Posts: 62
Re: Windows User Name
« Reply #4 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...
Lazarus 1.0.14
FPC 2.6.0
Win7 Pro 64bit/Win XP Pro 32bit

 

TinyPortal © 2005-2018