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
a.k.a.

-------------------------------------------------------------------
Shonsu, Swordsman of the Seventh Rank
sho...@mindspring.com
http://shonsu.home.mindspring.com/              (main page)
http://shonsu.home.mindspring.com/dhs84_01.htm  (DHS Class of 1984)
http://shonsu.home.mindspring.com/as400.htm     (AS/400 Page)

 

Re:Getting Network User ID in Delphi 3.0


Quote
sho...@mindspring.com (Shonsu) wrote:
>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:

David, you need to preinitialize the PChar buffer, before calling
WNGetUserId:

Var
  liBufferSize: Integer;
  lsUserId: String;
Begin
  ..
  // Allocate string space
  liBufferSize:=255;  
  SetLength(lsUserId, liBufferSize);

  // Get User ID in lsUserID
  WNetGetUser(Nil, PChar(lsUserId), liBufferSize);

  // and Set length to actual used stringlength
  SetLength(lsUserId, StrLen(PChar(lsUserId)));

  ShowMessage(lsUserId);
End;

Hope this helps,

Martin Israelsen
Denmark

Other Threads