From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,f02a96c85b47538d X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Received: by 10.68.30.34 with SMTP id p2mr3925558pbh.4.1320883371611; Wed, 09 Nov 2011 16:02:51 -0800 (PST) Path: h5ni18593pba.0!nntp.google.com!news2.google.com!postnews.google.com!u37g2000prh.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: Read Windows login username in Ada 95 Date: Wed, 9 Nov 2011 16:02:51 -0800 (PST) Organization: http://groups.google.com Message-ID: References: <9192369.445.1320801564794.JavaMail.geo-discussion-forums@yqbl36> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 X-Trace: posting.google.com 1320883371 6387 127.0.0.1 (10 Nov 2011 00:02:51 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 10 Nov 2011 00:02:51 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: u37g2000prh.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-Header-Order: ARLUEHNKC X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; WOW64; Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.21022; .NET CLR 3.5.30729; .NET CLR 3.0.30618; .NET4.0C),gzip(gfe) Xref: news2.google.com comp.lang.ada:14378 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Date: 2011-11-09T16:02:51-08:00 List-Id: On Nov 8, 9:05=A0pm, 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. > > =A0No. =A0Not 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 :=3D Buffer_Type'Length; Result :=3D 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