comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Read Windows login username in Ada 95
Date: Wed, 9 Nov 2011 16:02:51 -0800 (PST)
Date: 2011-11-09T16:02:51-08:00	[thread overview]
Message-ID: <cd7e37ef-e12e-445c-9806-9fa544e83f8f@u37g2000prh.googlegroups.com> (raw)
In-Reply-To: j9d1mn$2p2$1@speranza.aioe.org

On Nov 8, 9:05 pm, tmo...@acm.org wrote:
> > Is there a function in Ada 95 which returns me a String containing
> > the logged username in an Windows application? Thanks.
>
>  No.  Not all Ada applications run in a Windows environment.
>
> In Windows, you can call function GetEnvironmentVariable
> and get the value of the environment variable "USERNAME".

Doesn't always work (and neither does using
Ada.Environment_Variables).  I just tried it and found that
GetUserName returns my login name, while Ada.Environment_Variables
says that "USERNAME" doesn't exist.  It may be an unusual setup--I'm
logging into an Windows XP system remotely through the GoodTech telnet
server.  But you may as well use the function that works more
reliably.

This worked for me, but it could use more error checking:

with Text_IO;
procedure Print_User_Name is
    subtype Buffer_Type is String (1 .. 200);
    function GetUserName (lpBuffer : access Buffer_Type;
                          lpnSize  : access Integer) return Integer;
    pragma Import (StdCall, GetUserName, "GetUserNameA");

    Buf : aliased Buffer_Type;
    Size : aliased Integer;
    Result : Integer;
begin
    Size := Buffer_Type'Length;
    Result := GetUserName (Buf'Access, Size'Access);
    Text_IO.Put_Line (Buf (1 .. Size - 1));
end Print_User_Name;

StdCall is how we import Windows API functions with ICC Ada.  Don't
know how GNAT does it--probably the same.  Result should be checked
for errors, but I didn't bother.  GetUserName sets Size to the size of
the result including the null terminator, which is why the next line
uses Size - 1.

                                -- Adam





  reply	other threads:[~2011-11-10  0:02 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-09  1:19 Read Windows login username in Ada 95 Rego, P.
2011-11-09  5:05 ` tmoran
2011-11-10  0:02   ` Adam Beneschan [this message]
2011-11-10  1:17     ` Rego, P.
2011-11-09 10:44 ` Pascal Obry
2011-11-09 20:47 ` Rego, P.
2011-11-09 21:06   ` Pascal Obry
2011-11-10  1:16     ` Rego, P.
2011-11-09 21:52   ` Jeffrey Carter
2011-11-10  1:21     ` Rego, P.
2011-11-10  2:09       ` tmoran
2011-11-10 18:26       ` Jeffrey Carter
2011-11-10 23:13         ` Pascal Obry
2011-11-12 14:21           ` Rego, P.
2011-11-10 23:13         ` Pascal Obry
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox