I am attempting to get the username of the current user on a windows computer using windows API. I am using the GetUserName function in the Windows Unit. I am having some problems with giving the correct data types for function arguments. Any help would be greatly appreciated.

program logname;

uses
    Windows;
var
    username : array[0..255] of char;

begin
  if GetUserName(username,255) then
  begin
    WriteLn(username);
  end;
  Readln;
end.

When I run the compiled program I get a runtime error of 216 at 0x77FE1DDE.
I also get an error from my ide that the compiler does not know which function to overload.
I am using FPC and the FPS ide.
Thanks.
--EAnder

Recommended Answers

All 3 Replies

Thanks. That website shows what I need to do. I cannot use pascal strings (eg. [5, 'h', 'e', 'l', 'l', 'o'] right?) instead I need to create a new datatype and functions to work that datatype for C Strings((Null terminated)?). This is what I gathered from the webpage (http://www.hu.freepascal.org/lists/f...ly/005694.html) you pointed me to. Help me out if I am wrong. Thanks a lot.
--EAnder

this worked for me in free pascal and in extended pascal (changing unit to include of course):

program logname ;
uses
windows;
var
buffer: array[0..255] of char;
size: dword;

begin
size := 256;
GetUserName(buffer, size);
write(buffer);

readln;

end.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.