{"id":91661,"date":"2015-09-04T07:00:00","date_gmt":"2015-09-04T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20150904-00\/?p=91661\/"},"modified":"2019-03-13T12:19:12","modified_gmt":"2019-03-13T19:19:12","slug":"20150904-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20150904-00\/?p=91661","title":{"rendered":"Using an intermediate library to make the main library retargetable"},"content":{"rendered":"<p>A customer was developing a static library targetting both Windows&nbsp;XP Win32 applications and universal Windows apps. (This was before Windows&nbsp;XP reached end-of-life.) <\/p>\n<blockquote CLASS=\"q\">\n<p>Our library uses critical sections, but unfortunately there is no version <code>Initialize&shy;Critical&shy;Section<\/code> that is available to both Windows&nbsp;XP Win32 applications and universal Windows apps. Universal Windows apps must use <code>Initialize&shy;Critical&shy;Section&shy;Ex<\/code>, but that function is not available to Windows&nbsp;XP Win32 applications. Is there a way to dynamically target both Windows&nbsp;XP Win32 applications and universal Windows apps, pass WACK validation, and still have one library? <\/p>\n<p>We thought we could use <code>Get&shy;Module&shy;Handle<\/code> and <code>Get&shy;Proc&shy;Address<\/code> to detect which platform we are one, but <code>Get&shy;Module&shy;Handle<\/code> is not allowed in universal Windows apps, so we&#8217;re back where we started. <\/p>\n<p>Are we stuck having two versions of our library, one for Windows&nbsp;XP Win32 applications and one for universal Windows apps? <\/p>\n<\/blockquote>\n<p>Runtime dynamic linking (<code>Load&shy;Library<\/code>, <code>Get&shy;Proc&shy;Address<\/code>) is not permitted in universal Windows apps, which means that for universal Windows apps, you must have an entry for <code>Initialize&shy;Critical&shy;Section&shy;Ex<\/code> in your import table. But if that function is in your input table, then it won&#8217;t load on Windows&nbsp;XP. <\/p>\n<p>(You might think that you could have a second library to be used by Windows&nbsp;XP clients that implements the <code>Initialize&shy;Critical&shy;Section&shy;Ex<\/code> function. Unfortunately, you will run afoul of <a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2006\/07\/26\/679044.aspx\">dllimport<\/a>.) <\/p>\n<p>You are going to have to have separate libraries at some point, but you don&#8217;t have to have two versions of your library. You could build your library  to call, say, <code>Contoso&shy;Initialize&shy;Critical&shy;Section<\/code>, and have two helper libraries, one for Windows&nbsp;XP Win32 applications and one for universal Windows apps, each of which implement the <code>Contoso&shy;Initialize&shy;Critical&shy;Section<\/code> function in a manner appropriate to the target. <\/p>\n<p>In other words, people targeting Windows&nbsp;XP would link to <code>ContosoCore.dll<\/code> and <code>ContosoXPSupport.dll<\/code>. People writing universal Windows apps would link to <code>ContosoCore.dll<\/code> and <code>ContosoStoreSupport.dll<\/code>. <\/p>\n<p>This approach has a few advantages: <\/p>\n<ul>\n<li>It&#8217;s simple, works (because it&#8217;s so simple), and     everybody understands it. \n<li>All the files in your core library need to be compiled only once. <\/ul>\n<p>The second clause pays off if your library is large, or if you need to add new operating system targets. <\/p>\n<p><b>Update<\/b>: I guess I didn&#8217;t make it clear. My suggestion is that <code>Contoso&shy;Core.dll<\/code> link to the nonexistent <code>Contoso&shy;Support.dll<\/code>. If your program targets Windows XP, then rename <code>Contoso&shy;XP&shy;Support.dll<\/code> to <code>Contoso&shy;Support.dll<\/code>. If your program is a universal Windows app, then rename <code>Contoso&shy;Store&shy;Support.dll<\/code> to <code>Contoso&shy;Support.dll<\/code>. <\/p>\n<p>This technique also works with static libraries. You have a single <code>Contoso&shy;Core.lib<\/code> which calls a <code>Contoso&shy;Initialize&shy;Critical&shy;Section<\/code> function. There are two implementations of <code>Contoso&shy;Initialize&shy;Critical&shy;Section<\/code>, one in <code>Contoso&shy;XP&shy;Support.lib<\/code> and another in <code>Contoso&shy;Store&shy;Support.lib<\/code>. Each application chooses which support library to link in. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>A little traffic cop.<\/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-91661","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A little traffic cop.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/91661","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=91661"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/91661\/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=91661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=91661"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=91661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}