user3927897
user3927897

Reputation: 605

strange issue with dxComponentPrinter1

This should probably get posted under 'believe it or not' questions but ...As soon as I put dxComponentPrinter1 (devexpress) on the form my function ceases to work :

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

When I remove it (dxComponentPrinter1) then it works ok. The error I get, when the dxComponentPrinter1 is on the form is this :

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

Delphi XE6 underlines in red the line :

GetUserName(PChar(UserName), UserNameLen)

If I remove the function then everything compiles ok. No error. Has anybody encountered this before ?

Upvotes: 1

Views: 233

Answers (1)

David Heffernan
David Heffernan

Reputation: 612794

When you add the component to the form, a devExpress unit is added to your uses clause. This unit defines a function named GetUserName that your code now sees rather than the one you want to use declared in the Windows unit.

You could deal with this by fully qualifying the call to GetUserName. Either

Windows.GetUserName

or

Winapi.Windows.GetUserName

depending on how you named the Windows unit when you used it.

Personally, I'd be looking to see this GetUserFromWindows function declared in a low-level unit rather than a GUI unit.

If you want to make a function that reads the user name and can handle any length of name, then you could go for this possibly over-engineered version:

function UserName: string;
var
  Len: DWORD;
begin
  Len := 0;
  Win32Check(not GetUserName(nil, Len) and (GetLastError=ERROR_INSUFFICIENT_BUFFER));
  SetLength(Result, Len-1);
  Win32Check(GetUserName(PChar(Result), Len));
end;

Note that I've not fully qualified here because I assume that you'll move this function into a different unit that uses no dexExpress UI components.

Upvotes: 4

Related Questions