comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: win32 interfacing check (SetClipboardData)
Date: Wed, 30 Aug 2017 23:17:05 +0200
Date: 2017-08-30T23:17:05+02:00	[thread overview]
Message-ID: <oo7a0f$1d9t$1@gioia.aioe.org> (raw)
In-Reply-To: 59a706d2$0$3723$426a74cc@news.free.fr

On 2017-08-30 20:41, Xavier Petit wrote:
> Le 30/08/2017 à 18:04, Dmitry A. Kazakov a écrit :
>> It looks OK. Except that formally Wide_String is UCS-2 and Windows is 
>> UTF-16.
> Thank you for pointing that out.
> 
>> I would use UTF-8 encoded string as the input and recode it into 
>> UTF-16 to have CF_UNICODETEXT, e.g. by using MultiByteToWideChar.
> Thank you but I always get the error ERROR_INVALID_PARAMETER from 
> GetLastError, using it like this :
> 
> UTF16_Code_Page : constant := 1200;
> 
> Length := MultiByteToWideChar (CodePage       => UTF16_Code_Page,
>                                 DwFlags        => MB_PRECOMPOSED,
>                                 LpMultiByteStr => Addr (Source),
>                                 CchMultiByte   => -1,
>                                 LpWideCharStr  => Encoded,
>                                 CchWideChar    => 0);

The output must be null when its length is. And it looks like 
MB_PRECOMPOSED does not work. So (without error handling):

--
-- UTF-8 to UTF-16 conversion using MultiByteToWideChar
--
    function Convert (Text : String) return Wide_String is
       use type Win32.INT;
       Length : Win32.INT;
    begin
       Length := MultiByteToWideChar -- Determine length
                 (  CodePage       => CP_UTF8,
                    DwFlags        => 0,
                    LpMultiByteStr => Addr (Text),
                    CchMultiByte   => -1,
                    LpWideCharStr  => null,
                    CchWideChar    => 0
                 );
       declare
          Result : Wide_String (1..Integer (Length));
       begin
          Length := MultiByteToWideChar -- Do conversion
                    (  CodePage       => CP_UTF8,
                       DwFlags        => 0,
                       LpMultiByteStr => Addr (Text),
                       CchMultiByte   => -1,
                       LpWideCharStr  => Addr (Result),
                       CchWideChar    => Length
                 );
          Put_Line (Win32.INT'Image (Length));
          return Result;
       end;
    end Convert;

And Text must end with Character'Val (0). Of course you can do 
GlobalAlloc for the second call instead of returning Ada string.

>> Why should it need gnatW8 or gnata? You get characters by encoding 
>> them, I suppose.
> I use gnata to trigger Ada.Assertions errors, I could use the pragma 
> Assertion_Policy (Check) too.
> I have tested the following Wide_String : "123〠" with or without -gnatW8.
> It only worked with, but thanks to you I know that my procedure was 
> wrong anyway.

I see. IMO, it is a bad idea to use non-ASCII characters in the source. 
When I need a special character I take its UNICODE code position and 
convert that to String.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


  reply	other threads:[~2017-08-30 21:17 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 [this message]
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
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