comp.lang.ada
 help / color / mirror / Atom feed
* Read Windows login username in Ada 95
@ 2011-11-09  1:19 Rego, P.
  2011-11-09  5:05 ` tmoran
                   ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Rego, P. @ 2011-11-09  1:19 UTC (permalink / raw)


Is there a function in Ada 95 which returns me a String containing the logged username in an Windows application? Thanks.

(cross posted on stackoverflow <http://stackoverflow.com/questions/8056410/read-windows-login-username-with-ada-95>)



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Read Windows login username in Ada 95
  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
  2011-11-09 10:44 ` Pascal Obry
  2011-11-09 20:47 ` Rego, P.
  2 siblings, 1 reply; 15+ messages in thread
From: tmoran @ 2011-11-09  5:05 UTC (permalink / raw)


> 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".



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Read Windows login username in Ada 95
  2011-11-09  1:19 Read Windows login username in Ada 95 Rego, P.
  2011-11-09  5:05 ` tmoran
@ 2011-11-09 10:44 ` Pascal Obry
  2011-11-09 20:47 ` Rego, P.
  2 siblings, 0 replies; 15+ messages in thread
From: Pascal Obry @ 2011-11-09 10:44 UTC (permalink / raw)
  To: comp.lang.ada; +Cc: Rego, P.

Le 09/11/2011 02:19, Rego, P. a �crit :
> Is there a function in Ada 95 which returns me a String containing
> the logged username in an Windows application? Thanks.

No, for a portable solution you can use:

    POSIX.Process_Identification.Get_Login_Name

Available on Florist (GNU/Linux) and wPOSIX (Windows).

Pascal.

-- 

--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|    http://www.obry.net  -  http://v2p.fr.eu.org
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver keys.gnupg.net --recv-key F949BD3B




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Read Windows login username in Ada 95
  2011-11-09  1:19 Read Windows login username in Ada 95 Rego, P.
  2011-11-09  5:05 ` tmoran
  2011-11-09 10:44 ` Pascal Obry
@ 2011-11-09 20:47 ` Rego, P.
  2011-11-09 21:06   ` Pascal Obry
  2011-11-09 21:52   ` Jeffrey Carter
  2 siblings, 2 replies; 15+ messages in thread
From: Rego, P. @ 2011-11-09 20:47 UTC (permalink / raw)


Well, answering myself, we could use

function GetUsername return String is
   function GetEnv (Variable : String) return Interfaces.C.Strings.chars_ptr;
   pragma Import (C, GetEnv, "getenv");

   Command : constant String := "USERNAME";
   Answer_Ptr : constant Interfaces.C.Strings.chars_ptr := GetEnv (Command);
   Answer : constant String := Interfaces.C.Strings.Value (Answer_Ptr);
begin
   return Answer;
end GetUsername;

Not pure Ada, but fits very well.
Thank to all suggestions.



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Read Windows login username in Ada 95
  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
  1 sibling, 1 reply; 15+ messages in thread
From: Pascal Obry @ 2011-11-09 21:06 UTC (permalink / raw)
  To: comp.lang.ada; +Cc: Rego, P.

Le 09/11/2011 21:47, Rego, P. a �crit :
> Not pure Ada, but fits very well.

For a pure Ada version why not use Ada.Environment_Variables?

Pascal.

-- 

--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|    http://www.obry.net  -  http://v2p.fr.eu.org
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver keys.gnupg.net --recv-key F949BD3B




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Read Windows login username in Ada 95
  2011-11-09 20:47 ` Rego, P.
  2011-11-09 21:06   ` Pascal Obry
@ 2011-11-09 21:52   ` Jeffrey Carter
  2011-11-10  1:21     ` Rego, P.
  1 sibling, 1 reply; 15+ messages in thread
From: Jeffrey Carter @ 2011-11-09 21:52 UTC (permalink / raw)


On 11/09/2011 01:47 PM, Rego, P. wrote:
>
> function GetUsername return String is
>     function GetEnv (Variable : String) return Interfaces.C.Strings.chars_ptr;

This no doubt expects a NUL-terminated string. I would use Char_Array for the 
parameter and To_C to convert an Ada string passed to it.

>     pragma Import (C, GetEnv, "getenv");
>
>     Command : constant String := "USERNAME";
>     Answer_Ptr : constant Interfaces.C.Strings.chars_ptr := GetEnv (Command);

You're not giving it a NUL-terminated string.

>     Answer : constant String := Interfaces.C.Strings.Value (Answer_Ptr);
> begin
>     return Answer;

Who is responsible for freeing Answer_Ptr?

> end GetUsername;

As Obry suggested, Ada.Environment_Variables is an all-Ada way to do this.

-- 
Jeff Carter
"Alms for an ex-leper!"
Monty Python's Life of Brian
75



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Read Windows login username in Ada 95
  2011-11-09  5:05 ` tmoran
@ 2011-11-10  0:02   ` Adam Beneschan
  2011-11-10  1:17     ` Rego, P.
  0 siblings, 1 reply; 15+ messages in thread
From: Adam Beneschan @ 2011-11-10  0:02 UTC (permalink / raw)


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





^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Read Windows login username in Ada 95
  2011-11-09 21:06   ` Pascal Obry
@ 2011-11-10  1:16     ` Rego, P.
  0 siblings, 0 replies; 15+ messages in thread
From: Rego, P. @ 2011-11-10  1:16 UTC (permalink / raw)
  Cc: Rego, P.

> For a pure Ada version why not use Ada.Environment_Variables?
The problem is that Ada.Environment_Variables is an Ada 2005 package, cannot use it.



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Read Windows login username in Ada 95
  2011-11-10  0:02   ` Adam Beneschan
@ 2011-11-10  1:17     ` Rego, P.
  0 siblings, 0 replies; 15+ messages in thread
From: Rego, P. @ 2011-11-10  1:17 UTC (permalink / raw)


Thank you Adam.



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Read Windows login username in Ada 95
  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
  0 siblings, 2 replies; 15+ messages in thread
From: Rego, P. @ 2011-11-10  1:21 UTC (permalink / raw)


> This no doubt expects a NUL-terminated string. I would use Char_Array for the 
> parameter and To_C to convert an Ada string passed to it.
> 
> >     pragma Import (C, GetEnv, "getenv");
> >
> >     Command : constant String := "USERNAME";
> >     Answer_Ptr : constant Interfaces.C.Strings.chars_ptr := GetEnv (Command);
> 
> You're not giving it a NUL-terminated string.

You're right, I'm gonna check the result.

> Who is responsible for freeing Answer_Ptr?

If I included an Free(Answer_Ptr), would it solve this?

> As Obry suggested, Ada.Environment_Variables is an all-Ada way to do this.

That's the Ada 2005 problem I explained above.

Thank you.



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Read Windows login username in Ada 95
  2011-11-10  1:21     ` Rego, P.
@ 2011-11-10  2:09       ` tmoran
  2011-11-10 18:26       ` Jeffrey Carter
  1 sibling, 0 replies; 15+ messages in thread
From: tmoran @ 2011-11-10  2:09 UTC (permalink / raw)


> > Who is responsible for freeing Answer_Ptr?
>
> If I included an Free(Answer_Ptr), would it solve this?

I believe the C function getenv returns a pointer to a system area that
has the relevant string, it does not allocate a new buffer and copy the
string to the buffer.  The corresponding Windows functions make a copy
in the user-supplied buffer (presumably as an atomic operation).



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Read Windows login username in Ada 95
  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-10 23:13         ` Pascal Obry
  1 sibling, 2 replies; 15+ messages in thread
From: Jeffrey Carter @ 2011-11-10 18:26 UTC (permalink / raw)


On 11/09/2011 06:21 PM, Rego, P. wrote:
>
>> Who is responsible for freeing Answer_Ptr?
>
> If I included an Free(Answer_Ptr), would it solve this?

I don't know. My question was whether the pointer returned needs to be freed by 
the caller. Moran's response seems to indicate that it does not.

>> As Obry suggested, Ada.Environment_Variables is an all-Ada way to do this.
>
> That's the Ada 2005 problem I explained above.

Then you'll have to do something like this.

-- 
Jeff Carter
"Well, a gala day is enough for me. I don't think
I can handle any more."
Duck Soup
93



^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Read Windows login username in Ada 95
  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
  1 sibling, 1 reply; 15+ messages in thread
From: Pascal Obry @ 2011-11-10 23:13 UTC (permalink / raw)
  To: Jeffrey Carter

Le 10/11/2011 19:26, Jeffrey Carter a �crit :
> On 11/09/2011 06:21 PM, Rego, P. wrote:
>>
>>> Who is responsible for freeing Answer_Ptr?
>>
>> If I included an Free(Answer_Ptr), would it solve this?
>
> I don't know. My question was whether the pointer returned needs to be
> freed by the caller. Moran's response seems to indicate that it does not.

In this case it does not. I agree with Tom.

Pascal.

-- 

--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|    http://www.obry.net  -  http://v2p.fr.eu.org
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver keys.gnupg.net --recv-key F949BD3B




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Read Windows login username in Ada 95
  2011-11-10 18:26       ` Jeffrey Carter
  2011-11-10 23:13         ` Pascal Obry
@ 2011-11-10 23:13         ` Pascal Obry
  1 sibling, 0 replies; 15+ messages in thread
From: Pascal Obry @ 2011-11-10 23:13 UTC (permalink / raw)


Le 10/11/2011 19:26, Jeffrey Carter a �crit :
> On 11/09/2011 06:21 PM, Rego, P. wrote:
>>
>>> Who is responsible for freeing Answer_Ptr?
>>
>> If I included an Free(Answer_Ptr), would it solve this?
>
> I don't know. My question was whether the pointer returned needs to be
> freed by the caller. Moran's response seems to indicate that it does not.

In this case it does not. I agree with Tom.

Pascal.

-- 

--|------------------------------------------------------
--| Pascal Obry                           Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--|    http://www.obry.net  -  http://v2p.fr.eu.org
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver keys.gnupg.net --recv-key F949BD3B




^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: Read Windows login username in Ada 95
  2011-11-10 23:13         ` Pascal Obry
@ 2011-11-12 14:21           ` Rego, P.
  0 siblings, 0 replies; 15+ messages in thread
From: Rego, P. @ 2011-11-12 14:21 UTC (permalink / raw)
  Cc: Jeffrey Carter

Ok. Thank you people for the ideas.



^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2011-11-12 14:21 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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