comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: win32 interfacing check (SetClipboardData)
Date: Wed, 30 Aug 2017 20:41:48 -0500
Date: 2017-08-30T20:41:48-05:00	[thread overview]
Message-ID: <oo7pgt$764$1@franka.jacob-sparre.dk> (raw)
In-Reply-To: oo6nlo$dcf$1@gioia.aioe.org

> On 29/08/2017 22:28, Xavier Petit wrote:
>> Hi, I would like to know if this win32 code is "correct" from your PoV or 
>> could be written in a better way, especially this block :
>>
>> declare
>>  Tmp : Wide_String (1 .. Source'Length + 1) with Address => AMem;
>> begin
>>  Tmp := Source & Wide_Character'First;
>> end;
>
> It looks OK.

For me, using an address clause for anything other than interfacing to 
hardware is wrong. We certainly didn't do anything like this in Claw when 
implementing the clipboard operations. We used instances of 
Unchecked_Conversion to get a pointer of the right type, and then assigned 
into that. (Nowdays, I might use an instance of 
Address_to_Access_Conversions.)

The code that converts a String parameter into a value in the clipboard list 
looks like:

    procedure Append_Copy(Item : in     String;
                          List : in out Representation_List_Type;
                          Kind : in     Text_Kinds := Text) is
        use type Claw.Win32.HGlobal;
        Mem : Claw.Win32.HGlobal;
        subtype C_String_Type is Interfaces.C.Char_Array(0 .. Item'length);
        type Target_Pointer is access all C_String_Type;
        function Convert is new Ada.Unchecked_Conversion
          (Source => Claw.Win32.HGlobal,
           Target => Target_Pointer);
        N : Interfaces.C.Size_T;
    begin
        Mem := Claw.Low_Level.Miscellaneous.Global_Alloc (
          Claw.Low_Level.Miscellaneous.GMEM_FIXED,
          DWord(C_String_Type'Length*(Interfaces.C.Char'Size/8)));
        if Mem = Claw.Win32.NULL_HGLOBAL then
            Claw.Raise_Windows_Error;
        end if;
        Interfaces.C.To_C(Item, Convert(Mem).all, N);
        Append((Handle => Mem, Format => Text_Kind_Format(Kind),
                Delayed_Renderer => null), List);
    end Append_Copy;

"Append" here adds "Mem" to the Representation_List, giving ownership of the 
handle to the List.

A Wide_String version would work the same way, using the appropriate 
Interfaces.C types.

Note that we were trying for maximum portability (to any sane Ada 95 
compiler for Windows); there's no real need to use the Interfaces.C types on 
GNAT (if that's all you care about).

                              Randy.






  parent reply	other threads:[~2017-08-31  1:41 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-29 20:28 win32 interfacing check (SetClipboardData) Xavier Petit
2017-08-30 16:04 ` Dmitry A. Kazakov
2017-08-30 18:41   ` Xavier Petit
2017-08-30 21:17     ` Dmitry A. Kazakov
2017-09-01 12:51       ` Xavier Petit
2017-09-01 13:10         ` Dmitry A. Kazakov
2017-09-02  9:38           ` Xavier Petit
2017-09-02 12:29             ` Dmitry A. Kazakov
2017-08-31  1:41   ` Randy Brukardt [this message]
2017-09-01 12:53     ` Xavier Petit
replies disabled

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