ovidiucucu
November 4th, 2011, 10:26 AM
Q: When expand a variable of type HWND in Watch, QuickWatch or Variables window, the following is shown: unused - CXX0030: Error: expression cannot be evaluated.
http://www.codexpert.ro/forum/download/file.php?id=592
What 'unused' means? Is it an error in program?
A: No, it isn't a programming error.
We can find in WINDEF.H
DECLARE_HANDLE (HWND);
DECLARE_HANDLE is defined as
typedef void *PVOID;
//...
#ifdef STRICT
typedef void *HANDLE;
#define DECLARE_HANDLE(name) struct name##__ { int unused; }; typedef struct name##__ *name
#else
typedef PVOID HANDLE;
#define DECLARE_HANDLE(name) typedef HANDLE name
#endif
typedef HANDLE *PHANDLE;
There are two situations:
If STRICT is not defined, HWND is an alias of void*.
This case, if let's say a HWND type is required as a function parameter, we can easily make a mistake and pass some other handle type (e.g. HBRUSH which is also void*) or any other pointer type.
That leads in run-time errors and program malfunction.
If STRICT is defined, then STRICT type checking is performed.
After preprocessing phase, the above code becomes something like:
struct HWND__
{
int unused;
};
typedef struct HWND__ *HWND;
Now, HWND is of type pointer to a structure (HWND__ *) and, if we replace it by mistake with HBRUSH (of type HBRUSH__*) the compiler raises an error, avoiding further run-time troubles.
Example
HBRUSH hBrush = ::CreateSolidBrush(RGB(0, 0, 255));
// ...
// Stupid but possible mistake
::SetForegroundWindow(hBrush);
// Error: cannot convert parameter 1 from 'struct HBRUSH__ *' to 'struct HWND__ *'
Notes
There is a bunch of other handle types defined in the same manner (HBITMAP, HINSTANCE, etc). See WINDEF.H header file.
In Windows SDK, STRICT is defined by default. If, for some reason you don't want it, you have to add NO_STRICT to preprocessor definitions.
The C standard states the following: if the struct-declaration-list contains no named members, the behavior is undefined, then C compiler from Visual Studio gives an error if we have an empty structure: error C2016: C requires that a struct or union has at least one member.
For this reason, HWND__ and the other similar structures have a dummy member, int unused.
Resources
[MSDN] STRICT Type Checking (http://msdn.microsoft.com/en-us/library/aa383732(v=VS.85).aspx)
Credits
Thanks Viorel (http://www.codeguru.com/forum/member.php?u=213233) for suggesting note #3!
http://www.codexpert.ro/forum/download/file.php?id=592
What 'unused' means? Is it an error in program?
A: No, it isn't a programming error.
We can find in WINDEF.H
DECLARE_HANDLE (HWND);
DECLARE_HANDLE is defined as
typedef void *PVOID;
//...
#ifdef STRICT
typedef void *HANDLE;
#define DECLARE_HANDLE(name) struct name##__ { int unused; }; typedef struct name##__ *name
#else
typedef PVOID HANDLE;
#define DECLARE_HANDLE(name) typedef HANDLE name
#endif
typedef HANDLE *PHANDLE;
There are two situations:
If STRICT is not defined, HWND is an alias of void*.
This case, if let's say a HWND type is required as a function parameter, we can easily make a mistake and pass some other handle type (e.g. HBRUSH which is also void*) or any other pointer type.
That leads in run-time errors and program malfunction.
If STRICT is defined, then STRICT type checking is performed.
After preprocessing phase, the above code becomes something like:
struct HWND__
{
int unused;
};
typedef struct HWND__ *HWND;
Now, HWND is of type pointer to a structure (HWND__ *) and, if we replace it by mistake with HBRUSH (of type HBRUSH__*) the compiler raises an error, avoiding further run-time troubles.
Example
HBRUSH hBrush = ::CreateSolidBrush(RGB(0, 0, 255));
// ...
// Stupid but possible mistake
::SetForegroundWindow(hBrush);
// Error: cannot convert parameter 1 from 'struct HBRUSH__ *' to 'struct HWND__ *'
Notes
There is a bunch of other handle types defined in the same manner (HBITMAP, HINSTANCE, etc). See WINDEF.H header file.
In Windows SDK, STRICT is defined by default. If, for some reason you don't want it, you have to add NO_STRICT to preprocessor definitions.
The C standard states the following: if the struct-declaration-list contains no named members, the behavior is undefined, then C compiler from Visual Studio gives an error if we have an empty structure: error C2016: C requires that a struct or union has at least one member.
For this reason, HWND__ and the other similar structures have a dummy member, int unused.
Resources
[MSDN] STRICT Type Checking (http://msdn.microsoft.com/en-us/library/aa383732(v=VS.85).aspx)
Credits
Thanks Viorel (http://www.codeguru.com/forum/member.php?u=213233) for suggesting note #3!