[comp.windows.ms] A puzzling query

foz@cbnewsc.ATT.COM (The fozman) (03/08/89)

Hi!

I am currently developing an application using SDK 2.1.  Last night I ran
across an interesting bug(?).  I had written a new function for it and compiled
it (MSC 5.1).  While I was running my application, it hung somewhere in
the new function I had just inserted.  Upon inspection of my code, I noticed
that I had written:
			ReleaseDC(hDC, hWnd);
instead of:
			ReleaseDC(hWnd, hDC);

Now I think both HWND and HDC are 16 bit addresses but shouldn't the compiler
spit this out as a parameter order error?  An enquiring mind would like
to know......

Bill Thompson
att!ihlpf!foz

bturner@hpcvlx.HP.COM (Bill Turner) (03/11/89)

> Now I think both HWND and HDC are 16 bit addresses but shouldn't the compiler
> spit this out as a parameter order error?  An enquiring mind would like
> to know......

Unfortunately, a handle is a handle is a handle is a handle is....

[From windows.h:

    typedef WORD HANDLE;
    typedef HANDLE HWND;
    typedef HANDLE HDC;

So, as far as the compiler is concerned, *any* handle is equivalent....]

--Bill Turner

dick@venera.isi.edu (Richard Gillmann) (03/11/89)

In article <227@cbnewsc.ATT.COM> foz@cbnewsc.ATT.COM (The fozman) writes:
>I am currently developing an application using SDK 2.1.  Last night I ran
>across an interesting bug(?). I had written a new function for it and compiled
>it (MSC 5.1).  While I was running my application, it hung somewhere in
>the new function I had just inserted.  Upon inspection of my code, I noticed
>that I had written:
>			ReleaseDC(hDC, hWnd);
>instead of:
>			ReleaseDC(hWnd, hDC);
>
>Now I think both HWND and HDC are 16 bit addresses but shouldn't the compiler
>spit this out as a parameter order error?  An enquiring mind would like
>to know......

Welcome to C!  The C language uses a form of weak typing, in which types
are considered compatible if they have the same underlying base type (which
is the case here).  In strongly typed languages, such as Pascal or Ada, the
type names must match, which in your example would lead to a compile time
error.

gwydion@tavi.rice.edu (Basalat Ali Raja) (03/14/89)

In article <227@cbnewsc.ATT.COM> foz@cbnewsc.ATT.COM (The fozman) writes:
>			ReleaseDC(hDC, hWnd);
>instead of:
>			ReleaseDC(hWnd, hDC);
>
>Now I think both HWND and HDC are 16 bit addresses but shouldn't the compiler
>spit this out as a parameter order error?  An enquiring mind would like
>to know......

I'm afraid this is a quirk of the language rather than that of the 
compiler.  The types HDC and HWND are defined, quite simply, as:

typedef int HWND;
typedef int HDC;	/* Actually, it might be unsigned int, Anyway... */

This means that HWND and HDC become synonyms for type int.  That's
all.  So, whenever the compiler sees a HWND, it replaces it with an
int.  Ditto for int.  Thus, it is not possible to catch the error,
since both HWND and HDC are synonyms for the same thing.

I, for one, think that if MicroSoft decided to extend the language
definition to allow for better type-checking, it would be a good idea.
Any comments?  (Other than from the language purists :-] ).