From: Allan M. Bruce on
I am trying to get rid of a few annoying warnings in my project - I have got
rid of most of them but a few remain which I cant seem to get rid of. The
first one is while using FD_SET/FD_CLR for adding and clearing sockets. I
get the warning:
warning C4127: conditional expression is constant

The other one is using SetWindowLongPtr. I did have two warnings for this
but managed to cast the result to get rid of one warning. I cant get rid of
the last warning though. My code is:
mOldWndProc = (WNDPROC)(long long)SetWindowLongPtr(mHwnd, GWLP_WNDPROC,
(LONG_PTR)SkinWndProc);

Where SkinWndProc is defined as:
LRESULT CALLBACK SkinWndProc(HWND hWnd, UINT uMessage, WPARAM wParam,
LPARAM lParam)

and the warning is:
warning C4244: 'argument' : conversion from 'LONG_PTR' to 'LONG', possible
loss of data

If anybody can help I would be very grateful.

Many Thanks
Allan


From: Grzegorz on
Allan M. Bruce wrote:
> I am trying to get rid of a few annoying warnings in my project - I have got
> rid of most of them but a few remain which I cant seem to get rid of. The
> first one is while using FD_SET/FD_CLR for adding and clearing sockets. I
> get the warning:
> warning C4127: conditional expression is constant

Show us that conditional expression

>
> The other one is using SetWindowLongPtr. I did have two warnings for this
> but managed to cast the result to get rid of one warning. I cant get rid of
> the last warning though. My code is:
> mOldWndProc = (WNDPROC)(long long)SetWindowLongPtr(mHwnd, GWLP_WNDPROC,
> (LONG_PTR)SkinWndProc);

mOldWndProc = (WNDPROC)SetWindowLongPtr(mHwnd, GWLP_WNDPROC,(LONG_PTR)SkinWndProc);
or just:
mOldWndProc = SetWindowLongPtr(mHwnd, GWLP_WNDPROC,(LONG_PTR)SkinWndProc);

>
> Where SkinWndProc is defined as:
> LRESULT CALLBACK SkinWndProc(HWND hWnd, UINT uMessage, WPARAM wParam,
> LPARAM lParam)
>
> and the warning is:
> warning C4244: 'argument' : conversion from 'LONG_PTR' to 'LONG', possible
> loss of data
>
> If anybody can help I would be very grateful.
>
> Many Thanks
> Allan
>
>

--
677265676F727940346E6575726F6E732E636F6D
From: Allan M. Bruce on
> Show us that conditional expression

The conditional expression is FD_SET(mSocket, &mMasterSet)

>> mOldWndProc = (WNDPROC)(long long)SetWindowLongPtr(mHwnd, GWLP_WNDPROC,
>> (LONG_PTR)SkinWndProc);
>
> mOldWndProc = (WNDPROC)SetWindowLongPtr(mHwnd,
> GWLP_WNDPROC,(LONG_PTR)SkinWndProc);
> or just:
> mOldWndProc = SetWindowLongPtr(mHwnd, GWLP_WNDPROC,(LONG_PTR)SkinWndProc);

The warning is produced for the last arguement, not the return of
SetWindowLongPtr. I must add I am using warning level 4 to show maximum
warnings.

Allan


From: Grzegorz on


Allan M. Bruce wrote:
>>Show us that conditional expression
>
>
> The conditional expression is FD_SET(mSocket, &mMasterSet)

This warning: "warning C4127: conditional expression is constant" is shown as a result of constant expression in condition check for example:
conts int A=2;
....
if(A)

or:
while(A!=3)

and so on

FD_SET is macro. I checked how it's defined in winsock.h and the definition is:
#define FD_SET(fd, set) do { \
if (((fd_set FAR *)(set))->fd_count < FD_SETSIZE) \
((fd_set FAR *)(set))->fd_array[((fd_set FAR *)(set))->fd_count++]=(fd);\
} while(0)

You can't get rid of this warning on level 4 when using this macro. But you may replace it with your own macro or function if you don't want to see that warning.

>>>mOldWndProc = (WNDPROC)(long long)SetWindowLongPtr(mHwnd, GWLP_WNDPROC,
>>>(LONG_PTR)SkinWndProc);
>>
>>mOldWndProc = (WNDPROC)SetWindowLongPtr(mHwnd,
>>GWLP_WNDPROC,(LONG_PTR)SkinWndProc);
>>or just:
>>mOldWndProc = SetWindowLongPtr(mHwnd, GWLP_WNDPROC,(LONG_PTR)SkinWndProc);
>
>
> The warning is produced for the last arguement, not the return of
> SetWindowLongPtr. I must add I am using warning level 4 to show maximum
> warnings.

I don't see here any cast from LONG_PTR to LONG.
Do you get this warning when you just call "SetWindowLongPtr(mHwnd, GWLP_WNDPROC,(LONG_PTR)SkinWndProc);"?
Why do you have such weird cast of return value (to long long and then to WNDPROC)?

--
677265676F727940346E6575726F6E732E636F6D
From: Allan M. Bruce on
> I don't see here any cast from LONG_PTR to LONG.
> Do you get this warning when you just call "SetWindowLongPtr(mHwnd,
> GWLP_WNDPROC,(LONG_PTR)SkinWndProc);"?
> Why do you have such weird cast of return value (to long long and then to
> WNDPROC)?

If I dont cast the result I get an error and a warning:
warning C4244: 'argument' : conversion from 'LONG_PTR' to 'LONG', possible
loss of data
error C2440: '=' : cannot convert from 'LONG' to 'WNDPROC'

if I cast the result with just (WNDPROC) then I get two warnings:
warning C4244: 'argument' : conversion from 'LONG_PTR' to 'LONG', possible
loss of data
warning C4312: 'type cast' : conversion from 'LONG' to 'WNDPROC' of greater
size

So, thats why I have two casts - I thought this was ugly but MS use this
kind of trick quite a lot. If only I could get rid of that warning for the
last arguement...

Allan