{"id":10053,"date":"2011-07-27T07:00:00","date_gmt":"2011-07-27T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2011\/07\/27\/you-dont-need-to-ask-me-a-question-the-compiler-can-answer-more-accurately\/"},"modified":"2011-07-27T07:00:00","modified_gmt":"2011-07-27T07:00:00","slug":"you-dont-need-to-ask-me-a-question-the-compiler-can-answer-more-accurately","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20110727-00\/?p=10053","title":{"rendered":"You don&#039;t need to ask me a question the compiler can answer more accurately"},"content":{"rendered":"<p>\nA customer reported having problems developing the correct\np\/invoke signature for the\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2009\/08\/13\/9867383.aspx\">\n<code>INPUT<\/code> structure<\/a>.\n&#8220;The code works on 32-bit machines, but on 64-bit machines,\nthe call to <code>Send&shy;Input<\/code> returns\n<code>ERROR_INVALID_PARAMETER<\/code>.&#8221;\nThe sample code included the lines\n<\/p>\n<pre>\nINPUT[] inputs = new INPUT[1];\ninputs[0].type = INPUT_KEYBOARD;\n...\nint size = Marshal.SizeOf(inputs[0]);\nif (!SendInput(1, ref inputs, size))\n{\n Console.WriteLine(\"Failed with error {0}\", GetLastError());\n}\n<\/pre>\n<\/p>\n<p>\nMy response was simply,\n&#8220;I bet you are passing the wrong structure size.\nNote that on 64-bit Windows, the alignment of the inner structures\nis 8-byte rather than 4-byte.\nWrite a C++ program that does the same thing and compare.&#8221;\n<\/p>\n<p>\nThe customer decided to read only the first sentence of my reply,\npossibly the second, and ignore the third.\n&#8220;So what size should the <code>INPUT<\/code> structure be on 64-bit\nmachines?&#8221;\n<\/p>\n<p>\nThere are a few\nways you can go about finding out the size of a structure.\n<\/p>\n<p>\nOne method is to ask Raymond.\nThis may work, but it makes Raymond rather grumpy\nsince it demonstrates that you are lazy and just want the answer\nhanded to you with no effort (or demonstration of understanding)\non your part.\n<\/p>\n<p>\nAnother method is to sit down with the structure definition\nand work out the size on a piece of paper,\nbearing in mind that various <code>#pragma pack<\/code> directives\ncan change how the structure packing is applied.\n<\/p>\n<p>\nBut the easiest way to get the size of a structure is to use\nthe <code>sizeof<\/code> operator, because that is after all\nthe method the compiler uses.\nTo get the same value as the C++ compiler,\njust ask the C++ compiler!\n<\/p>\n<pre>\n#include &lt;windows.h&gt;\n#include &lt;stdio.h&gt;\n#include &lt;stddef.h&gt;\nint __cdecl main(int argc, char **argv)\n{\n printf(\"the size is %d\\n\", sizeof(INPUT));\n printf(\"the offset of mi is %d\\n\", offsetof(INPUT, mi));\n return 0;\n}\n<\/pre>\n<p>\nYou can now use this amazing technique to answer\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2009\/04\/22\/9560726.aspx#9564367\">\nwolf550e&#8217;s question<\/a>:\n<\/p>\n<blockquote CLASS=\"q\"><p>\nSSE types require 16-byte alignment.\nI guess they aren&#8217;t used in the Windows SDK?\nHow about directX or something where they are used?\n<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>A customer reported having problems developing the correct p\/invoke signature for the INPUT structure. &#8220;The code works on 32-bit machines, but on 64-bit machines, the call to Send&shy;Input returns ERROR_INVALID_PARAMETER.&#8221; The sample code included the lines INPUT[] inputs = new INPUT[1]; inputs[0].type = INPUT_KEYBOARD; &#8230; int size = Marshal.SizeOf(inputs[0]); if (!SendInput(1, ref inputs, size)) { [&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-10053","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A customer reported having problems developing the correct p\/invoke signature for the INPUT structure. &#8220;The code works on 32-bit machines, but on 64-bit machines, the call to Send&shy;Input returns ERROR_INVALID_PARAMETER.&#8221; The sample code included the lines INPUT[] inputs = new INPUT[1]; inputs[0].type = INPUT_KEYBOARD; &#8230; int size = Marshal.SizeOf(inputs[0]); if (!SendInput(1, ref inputs, size)) { [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/10053","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=10053"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/10053\/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=10053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=10053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=10053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}