From: Jade on
Hi
I am using GNAT's ada bindings and with functions like gethostname, etc
they choose to have a return value as a WIN32.PSTR.
However, all my ada functions require either ada standard strings or
characters. I was wondering if there is way of converting PSTR to an
ada string.

Thanks,

Jade.

From: Gerd on
Here an example how it can be done:

function AddrToPSTR is new Unchecked_Conversion(System.Address,
Win32.PSTR);

procedure GetHostName (Name : out STRING; Len : out Integer; RC : out
INTEGER) is
Erg : Win32.INT;
HostName : STRING (1..255);
begin
-- The result (excluding the \0) must fit into "Name"
Erg := gethostname (AddrToPSTR (HostName'ADDRESS), Name'LENGTH +
1);

if Erg = SOCKET_ERROR
then
RC := INTEGER(WSAGetLastError);
else
RC := NO_ERROR;

Name := (others => ' ');

-- Should work correct, as "gethostname" should never return more
characters
-- than "Name" can get.
for i in HostName'FIRST .. HostName'LAST
loop
if HostName (i) = ASCII.NUL
then
Len := i - 1;
return;
else
Name (Name'FIRST + i - 1) := HostName (i);
end if;
end loop;
end if;
end GetHostName;


Jade schrieb:

> Hi
> I am using GNAT's ada bindings and with functions like gethostname, etc
> they choose to have a return value as a WIN32.PSTR.
> However, all my ada functions require either ada standard strings or
> characters. I was wondering if there is way of converting PSTR to an
> ada string.
>
> Thanks,
>
> Jade.

From: Philippe Bertin on
Jade,

You may want to have a look at GtkAda, which is an Ada binding to the
(C-written) gtk library. There's good examples in there on how to
convert from and to C string (and a whole lot of other C- types in
general)

Kind regards,

PhB

Jade wrote:
....
> I was wondering if there is way of converting PSTR to an
> ada string.

From: jca.miranda on

Jade a écrit :

> Hi
> I am using GNAT's ada bindings and with functions like gethostname, etc
> they choose to have a return value as a WIN32.PSTR.
> However, all my ada functions require either ada standard strings or
> characters. I was wondering if there is way of converting PSTR to an
> ada string.
>
> Thanks,
>
> Jade.

PSTR is simply a renaming for char * if I remember well.
So you should be able to make simple conversions thanks to
Ada.Interfaces.C To_C and To_Ada functions.

Rgds

Juan

P.S. : Hello Philippe! ^^ (Sorry for the noise added)

From: tmoran on
>You may want to have a look at GtkAda, which is an Ada binding to the
>(C-written) gtk library. There's good examples in there on how to ...
Or at Claw, whose Claw.Sockets does:
function gethostname(Name : Claw.Win32.Lpcstr;
Length : Claw.Int) return Claw.Int;
pragma Import(Stdcall, gethostname, "gethostname");
...
Local_Name : Interfaces.C.Char_Array(0 .. 200);
...
Err := gethostname(Local_Name(0)'unchecked_access,
Claw.Int(Local_Name'length));
if Err = Socket_Error then

> function AddrToPSTR is new Unchecked_Conversion(System.Address,
> Win32.PSTR);
Locks you into certain compilers. (A design requirement of Claw
was portability across 4 Ada->Windows compilers.)