user763539
user763539

Reputation: 3699

Compiler error when calling Windows API with correct parameters

I am trying to run this elementary function but it will not work :

function GetWindowsUserName: String;
  const
    MAX_LENGTH = 256;
  var
    UserNameLength: DWord;
  begin
    UserNameLength := MAX_LENGTH - 1;
    SetLength(Result, MAX_LENGTH);
    if GetUserName(PChar(Result), UserNameLength) then
      SetLength(Result, UserNameLength - 1)
    else
      Result := '';
  end;

On formCreate I have :

AdvOfficeStatusBar1.Panels[0].Text := GetWindowsUserName;

I get :

[dcc32 Error] Unit1.pas(115): E2034 Too many actual parameters
[dcc32 Error] Unit1.pas(115): E2012 Type of expression must be BOOLEAN

What am I missing here ?

Edit : I am still getting the error : enter image description here

Upvotes: 0

Views: 717

Answers (2)

Whome
Whome

Reputation: 10400

Try this.

uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
- - - - - - 

{ Get current username. }
function TForm1.GetCurrentUser(): String;
var
   lpUserName: array[0..256] of Char;
   nSize: DWord;
begin
   // Get current loginname
   nSize := SizeOf(lpUserName);
   GetUserName(@lpUserName, nSize);
   Result := lpUserName;
end;

Its this function you must use, maybe Uses imports give you something else but similar name. But this is oldskool method. http://msdn.microsoft.com/en-us/library/windows/desktop/ms724432%28v=vs.85%29.aspx

New method is SSPI interface. (scrap this sspi is actually authenticating user+pwd values against WinAD. GetuserName is fine just reading active windows identity)
http://www.michael-puff.de/Programmierung/Delphi/Units/SSPIValidatePassword.pas

Upvotes: 0

David Heffernan
David Heffernan

Reputation: 612954

When you report a compiler error, you must indicate which line of the code the error applies to. In this case, the only line of code that can report that error is the call to GetUserName.

The only way in which the call to GetUserName could fail in that way would be for there to be an unexpected GetUserName in scope. Your code expects to find the GetUserName defined in the Windows. Clearly a different version is in scope.

You can fully specify the symbol to make the compiler use the right one:

Windows.GetUserName

or if you are using namespaces:

Winapi.Windows.GetUserName

Upvotes: 1

Related Questions