MAKEINTATOM() Does Not Return a Valid LPSTR

Last reviewed: November 2, 1995
Article ID: Q61980
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) versions 3.0 and 3.1
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT versions 3.5 and 3.51
        - Microsoft Windows 95 version 4.0
    

SUMMARY

The LPSTR returned from MAKEINTATOM() cannot be treated as a general-purpose string pointer. Instead, either use it with the Atom family or convert it into a valid string before passing it off as one.

MORE INFORMATION

The MAKEINTATOM() macro is documented on Page 370 of the "Microsoft Windows Software Development Kit Programmer's Reference" versions 2.x as returning a value of type LPSTR. This is correct, but misleading. The LPSTR value returned is a "fabricated" LPSTR and cannot be considered a general-purpose string pointer. Consider the definition of the MAKEINTATOM macro:

   #define MAKEINTATOM(i)  (LPSTR) ((DWORD)((WORD)(i)))

This tells the compiler to "take the integer value 'i', think of it as a WORD, zero-extend this into a DWORD, then think of this as a LPSTR." Thus, MAKEINTATOM(1Ah) returns 001Ah. This obviously is not the same as "1A", which would be ASCII(1)+ASCII(A)+0.

The reason this psuedo-LPSTR works with AddAtom(), for example, is that AddAtom() looks to see if the HIWORD of the LPSTR parameter is 0 (zero). If so, AddAtom() knows that the LOWORD contains an actual integer value and it simply grabs that.

The following code samples show how problems can occur with these psuedo-LPSTRs returned from MAKEINTATOM.

Incorrect

ATOM AddIntAtom(int iAtom) {

    LPSTR   szAtom;

    MessageBox(hWnd,
               (szAtom=MAKEINTATOM(iAtom)),
               "Adding Atom",
               MB_OK);
    return (AddAtom(szAtom));
}

The above code fragment will create and return a valid atom, but the message box will display an erroneous value.

Correct

ATOM AddIntAtom(int iAtom) {

    LPSTR   szAtom;
    char    szBuf[10];

    szAtom=MAKEINTATOM(iAtom);
    sprintf(szBuf, "%d", LOWORD(szAtom));   /* Here's the trick */
    MessageBox(hWnd,
               szBuf,
               "Adding Atom",
               MB_OK);
    return (AddAtom(szAtom));
}

In the above example, we converted the integer value contained in the LOWORD of szAtom into a character string, then used this new character string in the MessageBox() call.

Although these code fragments illustrate the limitations of a MAKEINTATOM LPSTR, they are not very realistic because you really should use GetAtomName() to get the character string of an atom. If you have not yet created an atom out of an integer value, you could just format the integer into character string directly, as follows:

   sprintf (szBuf, "%d", iAtom);
   MessageBox (hWnd, szBuf,....);


Additional reference words: 3.00 3.10 3.50 3.51 4.00 95
KBCategory: kbui
KBSubcategory: UsrDde


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 2, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.