Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 02-07-2014, 04:05 PM
delphimpd delphimpd is offline
Senior Member
 
Join Date: Oct 2006
Posts: 701
Default capture the Current logged in computer USER Name PChar Error

Hi and Thanks in advanced for the help

Using Delphi 2010

I got this from About.com Delphi.

I am trying to capture the Current logged in computer USER Name but in this function I get an error right before PChar in this line
If GetUserName(PChar(UserName),

What could be possible happen?

Here is the function

Code:
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;
Reply With Quote
  #2  
Old 02-07-2014, 04:12 PM
delphimpd delphimpd is offline
Senior Member
 
Join Date: Oct 2006
Posts: 701
Default

Never Mind

I got it

Code:
function ReturnUser:String;
var
  PC: PChar;
  Car: Cardinal;
begin
  Car := 0;
  PC := Nil;
  GetUserName(PC,Car);
  If Car <> 0 then
  begin
    GetMem(PC,Car);
    GetUserName(PC,Car);
    Result := String(PC);
    FreeMem(PC);
  end else
    Result := 'UNKNOWN';
end;
Then from a button click event

Code:
showmessage(ReturnUser);

This Link
http://www.experts-exchange.com/Prog..._27814217.html
Reply With Quote
  #3  
Old 02-07-2014, 04:39 PM
delphimpd delphimpd is offline
Senior Member
 
Join Date: Oct 2006
Posts: 701
Default

I Spoke to soon.

This is weird,

The first and the second function I posted nether of them works in my project, however, if I create a new project it works fine what could it? the error stops there in front of the pChar and it says too many actual parameters


Any advice

Thanks
Reply With Quote
  #4  
Old 02-07-2014, 04:55 PM
kolbasz kolbasz is offline
Senior Member
 
Join Date: Nov 2013
Posts: 841
Default

This will work in all version of delphi:
Code:
function GetWindowsUser: String;
var
  Size: DWord;
begin
  Size := 1024;
  SetLength(Result, Size);
  if GetUserName(PChar(Result), Size) then
    SetLength(Result, Size - 1)
  else
    ShowMessage(SysErrorMessage(GetLastError));
end;
Reply With Quote
  #5  
Old 02-07-2014, 05:51 PM
delphimpd delphimpd is offline
Senior Member
 
Join Date: Oct 2006
Posts: 701
Default

Thanks kolbasz for the quick response.

I had a conflict with one of my DevExpress Units declared in my uses clause, which it was making reference of the same GetUserName inside the unit. I removed it and now it works just fine.

The unit in question, just in case some else comes across the same conflict as mine was DevExpress Unit dxPSUt1

Thank you so much for the help

Best regards,
Reply With Quote
  #6  
Old 02-07-2014, 07:12 PM
kolbasz kolbasz is offline
Senior Member
 
Join Date: Nov 2013
Posts: 841
Default

You're welcome.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 02:31 PM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.