comp.lang.ada
 help / color / mirror / Atom feed
From: "Jerry van Dijk" <jvandyk@ibm.net>
Subject: Re: Quickie question (I hope)...
Date: 1998/11/13
Date: 1998-11-13T00:00:00+00:00	[thread overview]
Message-ID: <01be0f59$f07ecb60$0100007f@aptiva> (raw)
In-Reply-To: 72i7j0$sqa@sjx-ixn9.ix.netcom.com

> How do I do an IMPORT_VALUED_PROCEDURE pragma w/GNAT?
> Or is that not necessary?
> 
> I want to import and use the NT routine GetComputerNameA
> from Kernel32.  I know how to link against K32, but I can't
> figure out the import.

I suppose you are not using the Win32 binding.

In that case, remember that kernel32 is a dll, so binding to
it requires the StdCall convention. Also, the Win32 API
requires data of less than 128 byte to be passed by copy
instead of the usual pass by reference.

See my homepage for more info.

Here is one of many ways to use this function:

pragma C_Pass_By_Copy (128);

with System;
with Ada.Text_IO;
with Interfaces.C;

procedure Demo is

   package C renames Interfaces.C;
   use type C.size_t;

   function Get_Computer_Name return String is

      MAX_COMPUTERNAME_LENGTH : constant := 15;

      Size   : C.size_t := MAX_COMPUTERNAME_LENGTH + 1;
      Buffer : C.char_array (1 .. Size + 1) := (others => C.nul);

      function GetComputerName (Buffer : C.char_array;
                                Size   : System.Address) return Integer;
      pragma Import (StdCall, GetComputerName, "GetComputerNameA");

      dummy : integer;

   begin
      dummy := GetComputerName (Buffer, Size'Address);
      return C.To_Ada (Buffer);
   end Get_Computer_Name;

begin
   Ada.Text_IO.Put_Line ("Computer name: " & Get_Computer_Name);
end Demo;


-- 
-- Jerry van Dijk | Leiden, Holland
-- Team Ada       | email: jdijk@acm.org
-- Ada & Win32: http://stad.dsl.nl/~jvandyk





  reply	other threads:[~1998-11-13  0:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-11-13  0:00 Quickie question (I hope) Michael S. Jessop
1998-11-13  0:00 ` Jerry van Dijk [this message]
1998-11-16  0:00   ` Michael S. Jessop
replies disabled

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