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);
            lpUserName : pchar;
            lpnLength  : integer;
                Label1.Caption := 'User Name ' + lpUserName;
                Label2.Caption := IntToStr(lpnLength);

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

In article <6calda$>, 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:

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

Other Threads