{"id":11983,"date":"2010-12-17T07:00:00","date_gmt":"2010-12-17T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2010\/12\/17\/the-overlapped-associated-with-asynchronous-io-is-passed-by-address-and-you-can-take-advantage-of-that\/"},"modified":"2010-12-17T07:00:00","modified_gmt":"2010-12-17T07:00:00","slug":"the-overlapped-associated-with-asynchronous-io-is-passed-by-address-and-you-can-take-advantage-of-that","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20101217-00\/?p=11983","title":{"rendered":"The OVERLAPPED associated with asynchronous I\/O is passed by address, and you can take advantage of that"},"content":{"rendered":"<p>\nWhen you issue asynchronous I\/O,\nthe completion function or the I\/O completion port receives,\namong other things, a pointer to the <code>OVERLAPPED<\/code>\nstructure that the I\/O was originally issued against.\nAnd that is your key to golden riches.\n<\/p>\n<p>\nIf you need to associate information with the I\/O operation,\nthere&#8217;s no obvious place to put it,\nso some people end up doing things like maintaining a master\ntable which records all outstanding overlapped I\/O as well\nas the additional information associated with that I\/O.\nWhen each I\/O\ncompletes, they look up the I\/O in the master table to locate that\nadditional information.\n<\/p>\n<p>\nBut it&#8217;s easier than that.\n<\/p>\n<p>\nSince the <code>OVERLAPPED<\/code> structure is passed by address,\nyou can store your additional information <i>alongside<\/i> the\n<code>OVERLAPPED<\/code> structure:\n<\/p>\n<pre>\n\/\/ in C\nstruct OVERLAPPEDEX {\n OVERLAPPED o;\n CClient *AssociatedClient;\n CLIENTSTATE ClientState;\n};\n\/\/ or in C++\nstruct OVERLAPPEDEX : OVERLAPPED {\n CClient *AssociatedClient;\n CLIENTSTATE ClientState;\n};\n<\/pre>\n<p>\nWhen the I\/O completes,\nyou can use the <code>CONTAINING_RECORD<\/code> macro\nor just <code>static_cast<\/code> the <code>LPOVERLAPPED<\/code>\nto <code>OVERLAPPEDEX*<\/code> and bingo, there&#8217;s your\nextra information right there.\nOf course, you have to know that the I\/O that completed\nis one that was issued against an <code>OVERLAPPEDEX<\/code>\nstructure instead of a plain <code>OVERLAPPED<\/code> structure,\nbut there are ways of keeping track of that.\nIf you&#8217;re using a completion function, then only use an\n<code>OVERLAPPEDEX<\/code>-aware completion function when\nthe <code>OVERLAPPED<\/code> structure is part of an\n<code>OVERLAPPEDEX<\/code> structure.\nIf you&#8217;re using an I\/O completion port, then you can use the\ncompletion key or the <code>OVERLAPPED.hEvent<\/code> to\ndistinguish <code>OVERLAPPEDEX<\/code> asynchronous I\/O\nfrom boring <code>OVERLAPPED<\/code> I\/O.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you issue asynchronous I\/O, the completion function or the I\/O completion port receives, among other things, a pointer to the OVERLAPPED structure that the I\/O was originally issued against. And that is your key to golden riches. If you need to associate information with the I\/O operation, there&#8217;s no obvious place to put it, [&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-11983","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>When you issue asynchronous I\/O, the completion function or the I\/O completion port receives, among other things, a pointer to the OVERLAPPED structure that the I\/O was originally issued against. And that is your key to golden riches. If you need to associate information with the I\/O operation, there&#8217;s no obvious place to put it, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/11983","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=11983"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/11983\/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=11983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=11983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=11983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}