Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2014-03

THandle in code that must compile also with Delphi

8 messages · 5 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Juha Manninen

FileCreate and FileOpen return THandle with FPC but integer with Delphi2007. Delphi defines THandle as LongWord but for some reason Integer is used instead. With FPC on a 64-bit system THandle is 64- bit unsigned.

Question: what is the best way to maintain code that must compile with both FPC and Delphi2007?
The obvious solution is to use IFDEF and define a new type, say TMyHandle. FPC's libraries however take care of many platform differences. Is there any better way to solve this?

Regards,
Juha

Reply by email

Michael Van Canneyt
to Juha Manninen
Juha Manninen wrote 9 lines
FileCreate and FileOpen return THandle with FPC but integer with Delphi2007. Delphi defines THandle as LongWord but for some reason Integer is used instead. With FPC on a 64-bit system THandle is 64- bit unsigned. Question: what is the best way to maintain code that must compile with both FPC and Delphi2007? The obvious solution is to use IFDEF and define a new type, say TMyHandle. FPC's libraries however take care of many platform differences. Is there any better way to solve this?

Shortly said: No. The Delphi API is wrong; It should also return THandle (as that is what Windows CreateFile returns), but probably for historical reasons they still use Integer.
With the advent of 64-bit delphi, they could have cleaned up their API, but Alas...

Michael.

Reply by email

Marco van de Voort
to Michael Van Canneyt

In our previous episode, Michael Van Canneyt said:

Show quoted text 10 lines
> Question: what is the best way to maintain code that must compile with > both FPC and Delphi2007? > The obvious solution is to use IFDEF and define a new type, say TMyHandle. > FPC's libraries however take care of many platform differences. Is > there any better way to solve this? Shortly said: No. The Delphi API is wrong; It should also return THandle (as that is what Windows CreateFile returns), but probably for historical reasons they still use Integer. With the advent of 64-bit delphi, they could have cleaned up their API, but Alas...

IMHO the universal handle (for e.g. IPC related handles and file handles) is not a portable concept to begin with.

Reply by email

Michael Van Canneyt
to Marco van de Voort
Marco van de Voort wrote 14 lines
In our previous episode, Michael Van Canneyt said: >> Question: what is the best way to maintain code that must compile with >> both FPC and Delphi2007? >> The obvious solution is to use IFDEF and define a new type, say TMyHandle. >> FPC's libraries however take care of many platform differences. Is >> there any better way to solve this? > > Shortly said: No. The Delphi API is wrong; It should also return THandle > (as that is what Windows CreateFile returns), but probably for historical > reasons they still use Integer. > With the advent of 64-bit delphi, they could have cleaned up their API, but Alas... IMHO the universal handle (for e.g. IPC related handles and file handles) is not a portable concept to begin with.

Normally, I'd expect a TXYZHandle for each 'separate' type. With XYZ = File, Window, Lib, Module, Directory, Shmem, whatnot. This should cover most cases.

That these separate types are equal in the background is something the user need not know.

Michael.

Reply by email

Virgo Pärna
to Michael Van Canneyt: Shortly said: No. The Delphi API is wrong; It should also return THandle (as that is what…
Michael Van Canneyt wrote 6 lines
Shortly said: No. The Delphi API is wrong; It should also return THandle (as that is what Windows CreateFile returns), but probably for historical reasons they still use Integer. With the advent of 64-bit delphi, they could have cleaned up their API, but Alas...

I did try googling and it appears, that in 64 bit Delphi XE2 uses THandle as a result type. Anyway, in the case of error, it returns INVALID_HANDLE_VALUE, which is DWORD(-1).
So, under Windows it would be possible to use same code, if you compare result against INVALID_HANDLE_VALUE. And you should be able safely cast return value to THandle for that (and use variable of that type).
I really don't know, how it would work on linux, because sys_open seems to return int there.

Virgo Pärna

Reply by email

Mark Morgan Lloyd
to Marco van de Voort: In our previous episode, Michael Van Canneyt said: IMHO the universal handle (for e.g.…
Marco van de Voort wrote
IMHO the universal handle (for e.g. IPC related handles and file handles) is not a portable concept to begin with.

I think the concept of a handle as an opaque type is universal. What isn't safe is any assumption of consistent behaviour, e.g. that select() may be applied to any type of handle or that a program's stdin/stdout might in practice be implemented by a socket.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Marco van de Voort
to Mark Morgan Lloyd

In our previous episode, Mark Morgan Lloyd said:

Show quoted text 7 lines
> IMHO the universal handle (for e.g. IPC related handles and file handles) is > not a portable concept to begin with. I think the concept of a handle as an opaque type is universal. What isn't safe is any assumption of consistent behaviour, e.g. that select() may be applied to any type of handle or that a program's stdin/stdout might in practice be implemented by a socket.

Before that, I don't even think sizeof(any handle)=constant for a given target is a valid assumption.

Reply by email

Juha Manninen
to Virgo Pärna: I did try googling and it appears, that in 64 bit Delphi XE2 uses THandle as a result…
Virgo Pärna wrote 8 lines
I did try googling and it appears, that in 64 bit Delphi XE2 uses THandle as a result type. Anyway, in the case of error, it returns INVALID_HANDLE_VALUE, which is DWORD(-1). So, under Windows it would be possible to use same code, if you compare result against INVALID_HANDLE_VALUE. And you should be able safely cast return value to THandle for that (and use variable of that type). I really don't know, how it would work on linux, because sys_open seems to return int there.

Thanks Virgo for this note. I was just thinking of how to check for errors. In many places our Delphi code has :

  if Handle-1 then begin
    ...
  end;

I will not define a new type after all, instead I will use THandle everywhere and then compare against INVALID_HANDLE_VALUE which is defined in Windows unit also for Delphi2007.
The only downside is that Delphi compiler will complain about incompatible types (signed / unsigned) when assigning FileCreate() etc. return values. I must use typecast then but it is OK.

Regards,
Juha

Reply by email