Re: Weird problem calling DLL
- From: Rob Kennedy <me3@xxxxxxxxxxx>
- Date: Mon, 02 Mar 2009 23:02:20 -0600
Paul E. Schoen wrote:
"Rob Kennedy" <me3@xxxxxxxxxxx> wrote in message news:712d9bFj2e0kU2@xxxxxxxxxxxxxxxxxxxxxMarco van de Voort wrote:On 2009-02-28, Benjamin <benja@xxxxxxxx> wrote:ByteBool would be a good choice. BOOL is a four-byte type; ByteBool is one byte like C++'s bool.function SaveAImage(fname, temp: pchar; tipo: integer; qual: byte; grayscale: boolean): integer ; stdcall; external IMAGEDLL;Besides the said callingconvention items, afaik a Delphi boolean is not
extern "C" DDLL_API int SaveAImage(char* fname, char* temp, int tipo, BYTE qual, bool grayscale);
always equal to a bool also. Use Windows.BOOL instead of boolean
But Boolean is also a good choice. Neither Boolean nor bool can have any numeric values but 0 and 1 unless code has been doing funny things with type casts, which are not and never have been necessary. (So if the program stops working properly because of it, then, well, serves 'em right.)
I always thought a boolean True was -1, or $FF (binary all bits set). Zero is False in any language AFAIK. Usually any non-zero value is True.
In Delphi, Ord(True) is 1. In C++, bool's "usual arithmetic conversion" is the "integral promotion" that yields 1 for true.
Technically, *neither* language defines the binary representation of the types. Delphi merely defines what the Ord function does. C++ defines what happens when a bool expression occurs in a context where a number is expected. In either language, the door is open for arbitrary binary representations that the compiler inserts code to handle, to preserve the illusion that they're stored as 0 as 1.
Practically speaking, though, it would be foolish for language implementers to use anything other than 0 and 1, given how the languages need to work, not just internally, but with external libraries that have certain expectations for various data types.
You might be thinking of the OLE values. VB used all-bits-one for true. I suppose it was to make the "not" operator easy to implement. Besides that, though, I don't think there's much advantage to that representation.
For the most part, it doesn't really matter. The compiler generates code that checks for any non-zero value in a Boolean variable and treats them all as True.
Almost.
I have seen compiler-generated code that *masks out* the top bit as part of the comparison. That means that if you somehow go the value $80 into your variable it might be interpreted as False sometimes, but not all the time. Therefore, my advice continues to be that you should never type-cast to Boolean. If you have a numeric value, simply compare it with zero and store the result.
// No:
b := Boolean(x);
// Yes:
b := x <> 0;
--
Rob
.
- Follow-Ups:
- Re: Weird problem calling DLL
- From: Paul E. Schoen
- Re: Weird problem calling DLL
- References:
- Re: Weird problem calling DLL
- From: Marco van de Voort
- Re: Weird problem calling DLL
- From: Rob Kennedy
- Re: Weird problem calling DLL
- From: Paul E. Schoen
- Re: Weird problem calling DLL
- Prev by Date: Re: Weird problem calling DLL
- Next by Date: Re: Weird problem calling DLL
- Previous by thread: Re: Weird problem calling DLL
- Next by thread: Re: Weird problem calling DLL
- Index(es):
Relevant Pages
|