Re: #define....



Hi Robert,

I am seeing this in many example programs of the "Programming Microsoft
Windows CE .NET" book where the following define statement appears at the top
of their header file examples.

#define dim(x) (sizeof(x) / sizeof(x[0]))

I am trying to understand what this line has to do with the rest of the
code. If anyone has this book (Isbn# 0-7356-1884-4) the ex. starts on P.18.

The way I see this is that we are dividing the size of x by the size of x[0]
and defining this by Dim(x). IM LOST!

because you must use Unicode under Windows CE, a lot of code looks like this:

TCHAR szText[128];
GetWindowText(hwnd, szText, sizeof(szText)/sizeof(TCHAR));
LoadString(hInst, IDS_STRING, szText, sizeof(szText)/sizeof(TCHAR));

So defining the very handy macro above will reduce the code to this:

TCHAR szText[128];
GetWindowText(hwnd, szText, dim(szText));
LoadString(hInst, IDS_STRING, dim(szText));

#define DIMOF(X) (sizeof(X) / sizeof(X[0]))
is a universal form of
#define CHAROF(X) ((X) / sizeof(TCHAR))

Another handy macro is:
#define CBTEXT(X) ((X) * sizeof(TCHAR))
for allocating memory:

TCHAR *psz
cch = _tcslen(pszSrc);
psz = malloc(CBTEXT(cch+1));
_tcscpy(pszDest, pszSrc);

Therefore it is a good idea, defining these macros in every source file.

Regards Wolfgang
.



Relevant Pages

  • HIDE 1.0.7 now available for HLA
    ... which uses the wscan.exe program to extract windows constants and external declarations from the w.hhf header file to create a more compact version to include in programs. ... This is an early experimental version which requires some manual work to get all the symbols referenced indireclty through the windows header file. ... -option to auto-complete common HLA structure keywords -1 click/key jump to declarations -debug mode and debug window to display output. ...
    (alt.lang.asm)
  • Re: LINK error
    ... Just to do a workaround with the header file ... DLLEXPORT retvalue DLLCALL func; ... Building closed-source Windows code with non-Microsoft tools is a very bad idea from the LIB vendor. ... if there is the source - build it using WDK too, avoid GNU tools at all ...
    (microsoft.public.development.device.drivers)
  • GetLocalPathName could not be located in Kernel32.dll
    ... Apparently this function is not implemented in Windows NT 4. ... Include an additional header file ... But now I am getting the following error during compilation: ... a C-style cast or ...
    (microsoft.public.dotnet.languages.vc)
  • RE: SSL and CrytoAPI
    ... Common header file containing handy macros and definitions ... this version has been modified to use GetWindowLong instead of ... Minimum operating systems Windows 95, ... Unicode Implemented as Unicode and ANSI versions on Windows NT, Windows 2000, Windows XP ...
    (microsoft.public.platformsdk.security)
  • Re: LINK error
    ... Just to do a workaround with the header file ... DLLEXPORT retvalue DLLCALL func; ... Building closed-source Windows code with non-Microsoft tools is a very bad idea from the LIB vendor. ... if there is the source - build it using WDK too, avoid GNU tools at all ...
    (microsoft.public.development.device.drivers)