开发者

FindAtom with MAKEINTATOM useless?

as I understand it, FindAtom returns 0 if the requested Atom is not found. But even in this small code, without any AddAtom at all, it seems to always return something. Why is that? Doesn’t this make FindAtom with Ints useless? :confus:

int main( ) {
  cout << FindAtom(MAKEINTATOM(12345));  // Output: 12345
  cout << FindAtom(MAKEINTATOM(2011));  // Output:开发者_开发技巧 2011
  return 0;
}

Bye


Signature of the FindAtom function:

ATOM WINAPI FindAtom(
    __in  LPCTSTR lpString
);

I found the following code in Wine svn-repository. It represents the basic logic:

295 ATOM WINAPI FindAtom16( LPCSTR str )
296 {
297     ATOMTABLE * table;
298     WORD hash,iatom;
299     HANDLE16 entry;
300     int len;
301 
302     TRACE("%s\n",debugstr_a(str));
303 
304     if (ATOM_IsIntAtomA( str, &iatom )) return iatom;
305     if ((len = strlen( str )) > 255) len = 255;
306     if (!(table = ATOM_GetTable( FALSE ))) return 0;
307     hash = ATOM_Hash( table->size, str, len );
308     entry = table->entries[hash];
309     while (entry)
310     {
311         ATOMENTRY * entryPtr = ATOM_MakePtr( entry );
312         if ((entryPtr->length == len) &&
313             (!strncasecmp( entryPtr->str, str, len )))
314         {
315             TRACE("-- found %x\n", entry);
316             return HANDLETOATOM( entry );
317         }
318         entry = entryPtr->next;
319     }
320     TRACE("-- not found\n");
321     return 0;
322 }

So, let's try to trace the program, when it's calling the FindAtom function:

FindAtom(MAKEINTATOM(12345));

MAKEINTATOM is a macro:

#define MAKEINTATOM(i) (LPTSTR)((DWORD)((WORD)(i)))
  1. MAKEINTATOM(12345) returns an integer atom cast to a string pointer.
  2. Calling FindAtom(MAKEINTATOM(12345))
  3. Checking the condition if (ATOM_IsIntAtomA( str, &iatom ))
119 static BOOL ATOM_IsIntAtomA(LPCSTR atomstr,WORD *atomid)
120 {
121     UINT atom = 0;
122     if (!HIWORD(atomstr)) atom = LOWORD(atomstr);
123     else
124     {
125         if (*atomstr++ != '#') return FALSE;
126         while (*atomstr >= '' && *atomstr <= '9')
127         {
128             atom = atom * 10 + *atomstr - '';
129             atomstr++;
130         }
131         if (*atomstr) return FALSE;
132     }
133     if (atom >= MAXINTATOM)
134     {
135         SetLastError( ERROR_INVALID_PARAMETER );
136         atom = 0;
137     }
138     *atomid = atom;
139     return TRUE;
140 }

As you can see this function will return a pointer to UINT when called with an argument which represents an integer atom.

Then it executes the body

return iatom;

So when you call cout << FindAtom (MAKEINTATOM (12345)); you get 12345 as output.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜