{"id":17023,"date":"2009-08-24T10:00:00","date_gmt":"2009-08-24T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2009\/08\/24\/the-operating-system-doesnt-know-which-language-programs-are-written-in-by-the-time-the-code-hits-the-cpu-they-all-look-the-same\/"},"modified":"2009-08-24T10:00:00","modified_gmt":"2009-08-24T10:00:00","slug":"the-operating-system-doesnt-know-which-language-programs-are-written-in-by-the-time-the-code-hits-the-cpu-they-all-look-the-same","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20090824-00\/?p=17023","title":{"rendered":"The operating system doesn&#039;t know which language programs are written in &#8211; by the time the code hits the CPU, they all look the same"},"content":{"rendered":"<p>\nReader\n<a HREF=\"http:\/\/www.ubercode.com\">Will Rayer<\/a>\nasks about\n&#8220;<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/pages\/407234.aspx#649621\">the degree to which &#8216;plain vanilla&#8217; C Windows API code\nworks under Vista with the native look and feel<\/a>.&#8221;\n<\/p>\n<p>\nIt works just as well as code written in any other language.\nThe operating system doesn&#8217;t know which language programs are\nwritten in.\nBy the time the code reaches the CPU,\nthey all look the same.\nIt&#8217;s just a bunch of instructions that occasionally call an API function.\nYou can write it in C, C++, assembly, Delphi, Perl, whatever.\n<\/p>\n<p>\nOf course, some languages are better-suited to calling Win32 than others.\nWin32 is a C-based API,\nin the sense that the way you call an exported function is\nexpressed in a C header file,\nand <code>__stdcall<\/code> calling convention matches up reasonably\nwell with the way C does things\n(once you convince your compiler to follow that convention).\nThe way types are passed on the stack or in registers,\nhow return values are represented,\nthe fact that pointers are just the address of some blob of data,\nthese all follow the C way of thinking.\nIt stands to reason that the C language\n(and languages which follow in C&#8217;s footsteps,\nlike C++)\nhave a pretty easy time of calling Win32 exported functions.\n<\/p>\n<p>\nBut that doesn&#8217;t mean that those are the only languages.\nAfter all, at the end of the day, it&#8217;s all machine code.\nIf you can write assembly language that pushes the parameters\nin the right format in the right order, then you can use Win32 from\nassembly language.\n(There appears to be a whole subculture devoted to this endeavor.)\n<\/p>\n<p>\nNow, it is indeed the case that COM programming is much more\nconvenient in C++ because\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2004\/02\/05\/68017.aspx\">\nthe COM object layout<\/a>\nmatches that of many C++ compilers.\nBut that doesn&#8217;t mean you can&#8217;t use some other language to do it.\nAs long as that language knows how to indirect through a vtable,\nyou can use COM objects.\nIndeed, the COM header files go out of their way to make sure\neven you old-school C programmers can call COM objects.\nIf you define the <code>COBJMACROS<\/code> symbol, then\nyou get access to macros like this:\n<\/p>\n<pre>\n#define IPersistFile_GetClassID(This,pClassID)      \\\n    (This)-&gt;lpVtbl -&gt; GetClassID(This,pClassID)\n<\/pre>\n<p>\nThis snippet from the <code>objidl.h<\/code> header file\nis some syntactical sugar to help C programmers use COM.\nUnder pure C, you would retrieve the <code>CLSID<\/code>\nfrom an <code>IPersistFile<\/code> interface pointer like this:\n<\/p>\n<pre>\nCLSID clsid;\nIPersistFile* ppf = ...;\nHRESULT hr = ppf-&gt;lpVtbl-&gt;GetClassID(ppf, &amp;clsid);\n<\/pre>\n<p>\nThe above macro at least removes the error potential of\npassing the wrong <code>this<\/code> pointer:\n<\/p>\n<pre>\nCLSID clsid;\nIPersistFile* ppf = ...;\nHRESULT hr = IPersistFile_GetClassID(ppf, &amp;clsid);\n<\/pre>\n<p>\nIf you want to write your programs in C,\nyou still have a lot of company.\nHuge chunks of Windows are still written in the C language.\nNot that you can tell, because once the compiler is\ndone doing its thing,\nthe identity of the source language is long gone.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Reader Will Rayer asks about &#8220;the degree to which &#8216;plain vanilla&#8217; C Windows API code works under Vista with the native look and feel.&#8221; It works just as well as code written in any other language. The operating system doesn&#8217;t know which language programs are written in. By the time the code reaches the CPU, [&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":[26],"class_list":["post-17023","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-other"],"acf":[],"blog_post_summary":"<p>Reader Will Rayer asks about &#8220;the degree to which &#8216;plain vanilla&#8217; C Windows API code works under Vista with the native look and feel.&#8221; It works just as well as code written in any other language. The operating system doesn&#8217;t know which language programs are written in. By the time the code reaches the CPU, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/17023","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=17023"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/17023\/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=17023"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=17023"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=17023"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}