{"id":22503,"date":"2008-05-01T10:00:00","date_gmt":"2008-05-01T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2008\/05\/01\/strange-uses-for-window-class-atoms\/"},"modified":"2008-05-01T10:00:00","modified_gmt":"2008-05-01T10:00:00","slug":"strange-uses-for-window-class-atoms","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20080501-00\/?p=22503","title":{"rendered":"Strange uses for window class atoms"},"content":{"rendered":"<p>\nWhen you register a window class (with the\n<code>RegisterClass<\/code> or\n<code>RegisterClassEx<\/code> function),\nyou get an <code>ATOM<\/code> back.\nWhat use is this atom?\n<\/p>\n<p>\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2004\/10\/11\/240744.aspx\">\nNot much<\/a>.\n<\/p>\n<p>\nYou can use this atom in many places where a window class name\ncan be used; just convert it to a string with the\n<code>MAKEINTATOM<\/code> macro.\nLet&#8217;s change our\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2003\/07\/23\/54576.aspx\">\nscratch program<\/a> to illustrate:\n<\/p>\n<pre>\n<font COLOR=\"blue\">ATOM g_atmClass;<\/font>\nBOOL\nInitApp(void)\n{\n    ...\n    <font COLOR=\"blue\">g_atmClass = RegisterClass(&amp;wc);\n    if (!g_atmClass) return FALSE;<\/font>\n    ...\n}\nint WINAPI WinMain(HINSTANCE hinst, HINSTANCE hinstPrev,\n                   LPSTR lpCmdLine, int nShowCmd)\n{\n    ...\n        hwnd = CreateWindow(\n            <font COLOR=\"blue\">MAKEINTATOM(g_atmClass)<\/font>,        \/* Class Name *\/\n            \"Scratch\",                      \/* Title *\/\n            WS_OVERLAPPEDWINDOW,            \/* Style *\/\n            CW_USEDEFAULT, CW_USEDEFAULT,   \/* Position *\/\n            CW_USEDEFAULT, CW_USEDEFAULT,   \/* Size *\/\n            NULL,                           \/* Parent *\/\n            NULL,                           \/* No menu *\/\n            hinst,                          \/* Instance *\/\n            0);                             \/* No special parameters *\/\n    ...\n}\n<\/pre>\n<p>\nWe save the atom returned by the <code>RegisterClass<\/code>\nfunction and use it (in the form of a <code>MAKEINTATOM<\/code>)\nin place of the class name.\nif you run this program, you&#8217;ll see that it works exactly the same\nas the old version that used the class name.\nThe class atom is valid as long as the class remains registered.\n<\/p>\n<p>\nFunctions that accept a <code>MAKEINTATOM<\/code> as the class name\ninclude\n<code>CreateWindow<\/code>,\n<code>FindWindow<\/code>,\n<code>GetClassInfo<\/code>,\nand <code>UnregisterClass<\/code>\n(and the <code>Ex<\/code> versions of them).\n<\/p>\n<p>\nWhy would you do this?\n<\/p>\n<p>\nWell, there really isn&#8217;t much reason.\nThe string name works just as well as the atom,\nso the atom is just one more thing to keep track of.\nHowever, even though you don&#8217;t use it,\nyou have to be aware that other people might.\nFor example, the <code>lpszClass<\/code> member of the\n<code>CREATESTRUCT<\/code> structure is usually a pointer\nto a string, but it could be a <code>MAKEINTATOM<\/code> if\nsomebody decided to pass an atom instead of a string to\n<code>CreateWindow<\/code>.\nThose of you who&#8217;ve read\n<a HREF=\"http:\/\/www.awprofessional.com\/title\/0321440307\">\nthe first Bonus Chapter of my book<\/a>\nare already familiar with the program that crashed when\nsomebody created a window via an atom.\n<\/p>\n<p>\nThere is one interesting thing you can do with the atom:\nIf you have a valid class atom, you can quickly tell whether\na window belongs to that class by checking the window word\nfor the atom:\n<\/p>\n<pre>\nif (GetWindowWord(hwnd, GWW_ATOM) == atom) ...\n<\/pre>\n<p>\nThis technique saves you the trouble of calling\n<code>GetClassName<\/code> and then doing a string comparison,\nreducing it instead to an integer comparison.\nThis technique makes it very simple to\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2006\/02\/01\/521962.aspx#522672\">\nwrite a <code>TestIfWndIsDialog<\/code> function<\/a>:\n<\/p>\n<pre>\nBOOL TestIfWndIsDialog(HWND hwnd)\n{\n  return GetWindowWord(hwnd, GWW_ATOM) == (ULONG_PTR)<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2006\/08\/31\/733521.aspx\">WC_DIALOG<\/a>;\n}\n<\/pre>\n<p>\n<b>Exercise<\/b>: Discuss the limitations of the above\n<code>TestIfWndIsDialog<\/code> function.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you register a window class (with the RegisterClass or RegisterClassEx function), you get an ATOM back. What use is this atom? Not much. You can use this atom in many places where a window class name can be used; just convert it to a string with the MAKEINTATOM macro. Let&#8217;s change our scratch program [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-22503","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>When you register a window class (with the RegisterClass or RegisterClassEx function), you get an ATOM back. What use is this atom? Not much. You can use this atom in many places where a window class name can be used; just convert it to a string with the MAKEINTATOM macro. Let&#8217;s change our scratch program [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/22503","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=22503"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/22503\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=22503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=22503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=22503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}