Recent

Author Topic: Get username and hostname from Windows ?  (Read 12562 times)

seba22

  • Full Member
  • ***
  • Posts: 136
Get username and hostname from Windows ?
« on: March 07, 2010, 05:56:44 pm »
Welcome,

How can i run following functions under Lazarus (Win32)
Code: [Select]
function GetComputerNetName: string;
var
  buffer: array[0..255] of char;
  size: dword;
begin
  size := 256;
  if GetComputerName(buffer, size) then
    Result := buffer
  else
    Result := ''
end;

Function GetUserFromWindows: string;
Var
   UserName : string;
   UserNameLen : Dword;
Begin
   UserNameLen := 255;
   SetLength(userName, UserNameLen) ;
   If GetUserName(PChar(UserName), UserNameLen) Then
     Result := Copy(UserName,1,UserNameLen - 1)
   Else
     Result := 'Unknown';
End;


Right now i get
Code: [Select]
unit1.pas(256,15) Error: Identifier not found "getUserName"

Is there names are different or i forget add something to "uses" ?

Regards

eny

  • Hero Member
  • *****
  • Posts: 1665
Re: Get username and hostname from Windows ?
« Reply #1 on: March 07, 2010, 07:41:18 pm »
Try adding 'windows' to the uses section.
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

seba22

  • Full Member
  • ***
  • Posts: 136
Re: Get username and hostname from Windows ?
« Reply #2 on: March 08, 2010, 12:26:46 pm »
Thank You for answer, think You have right.
Right now I can't see that error about get host and username...

But after adding Windows to uses i get new errors...  and i'm unable to compile project.


I want print files in folder so i have following part of code. //it's just example
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
      var
 SR: TSearchRec;
 Fik : Integer;
 const
 patch:string = 'c:\\';


 begin
 patch:=edit1.text;
 Fik := FindFirst( patch+ '*.*', faAnyFile, SR);
 while (Fik = 0) do
 begin
 listbox1.items.add(AnsiToUtf8(patch+sr.Name));
 Fik := FindNext(SR);
 end;
 FindClose(SR);
memo1.lines:=listbox1.items;
end;       

And error is:
Code: [Select]
unit1.pas(234,14) Error: Incompatible type for arg no. 1: Got "TSearchRec", expected "LongWord"

And it's pointing here
Code: [Select]
FindClose(SR);
SR i think have to be TSearchRec...

If i remove "Windows" from uses it's working perfect...

Any idea ?

Regards

seba22

  • Full Member
  • ***
  • Posts: 136
Re: Get username and hostname from Windows ?
« Reply #3 on: March 08, 2010, 12:41:41 pm »
I found the solution.

Add SysUtils before command FindClose.
Code: [Select]
SysUtils.FindClose(SR);

 

TinyPortal © 2005-2018