954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Windows API Function: GetUserName

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

EAnder
Light Poster
49 posts since Feb 2008
Reputation Points: 26
Solved Threads: 5
 
FlamingClaw
Posting Pro
559 posts since Feb 2009
Reputation Points: 132
Solved Threads: 138
 

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(['h', 'e', 'l', 'l', 'o', 0](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

EAnder
Light Poster
49 posts since Feb 2008
Reputation Points: 26
Solved Threads: 5
 

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.

rapistebe
Newbie Poster
1 post since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: