Board index » delphi » Getting Network User ID in Delphi 3.0

Getting Network User ID in Delphi 3.0

I'm trying to write a simple Delphi program to get the currently signed on
user name.  Does anyone know of a way to make this work???  I'm using Delphi
3.0 Professional with the following code:

        procedure TForm1.FormCreate(Sender: TObject);
        var
            lpUserName : pchar;
            lpnLength  : integer;
        begin
                WNetGetUser(nil,lpUserName,lpnLength);
                Label1.Caption := 'User Name ' + lpUserName;
                Label2.Caption := IntToStr(lpnLength);
        end;

The WNetGetUser API does not work.  Nor does the BDE API to do the same thing
(DBIGetNetUserName).  I get garbage back.  Does anyone have any ideas???  
Please, Delphi only answers.  No VB or C++ stuff.  I'm a newbie Delphi
programmer (I have 10+ years as an AS/400 RPG programmer though).

Thanks in advance......

David Scott

 

Re:Getting Network User ID in Delphi 3.0


Quote
In article <6calda$...@camel18.mindspring.com>, Shonsu wrote:
> The WNetGetUser API does not work.

The API works fine, the problem is your code.

Anytime you call a windows api that wants a PChar for returning a
result, you need to allocate memory for the result and initialize that
PChar to point to that memory.  You haven't done either.  

This one way:

Var
  lpUserName : Array [0..50] of char;
  LpnLength  : integer;
begin
  lpnLength := Sizeof (lpUserName);
  WNetGetUser(nil,lpUserName,lpnLength);
  Label1.Caption := 'User Name ' + StrPas (lpUserName);
  Label2.Caption := IntToStr(lpnLength);
  end;

Other Threads