<p>What's the best way to determine which version of the .NET Compact Frameworks (including Service Packs) is installed on a device through a .NET application.  </p>

[{'answer_id': 70808, 'author': 'Roel', 'author_id': 11449, 'author_profile': 'https://Stackoverflow.com/users/11449', 'pm_score': 6, 'selected': True, 'text': '<p>I have battled this problem myself last week and consider myself somewhat of an expert now ;)</p>\n\n<p>I\'m 99% sure that not all dlls and static libraries were recompiled with the SP1 version. You need to put</p>\n\n<pre><code>#define _BIND_TO_CURRENT_MFC_VERSION 1\n#define _BIND_TO_CURRENT_CRT_VERSION 1\n</code></pre>\n\n<p>into <em>every</em> project you\'re using. For every project of a real-world size, it\'s very easy to forget some small lib that wasn\'t recompiled.</p>\n\n<p>There are more flags that define what versions to bind to; it\'s documented on <a href="http://msdn.microsoft.com/en-us/library/cc664727%28v=vs.90%29.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/cc664727%28v=vs.90%29.aspx</a> . As an alternative to the lines above, you can also put</p>\n\n<pre><code>#define _BIND_TO_CURRENT_VCLIBS_VERSION 1\n</code></pre>\n\n<p>which will bind to the latest version of all VC libs (CRT, MFC, ATL, OpenMP).</p>\n\n<p>Then, check what the embedded manifest says. Download XM Resource Editor: <a href="http://www.wilsonc.demon.co.uk/d10resourceeditor.htm" rel="nofollow noreferrer">http://www.wilsonc.demon.co.uk/d10resourceeditor.htm</a>. Open every dll and exe in your solution. Look under \'XP Theme Manifest\'. Check that the \'version\' attribute on the right-hand side is \'9.0.30729.1\'. If it\'s \'9.0.21022\', some static library is pulling in the manifest for the old version.</p>\n\n<p>What I found is that in many cases, <em>both</em> versions were included in the manifest. This means that some libraries use the sp1 version and others don\'t.</p>\n\n<p>A great way to debug which libraries don\'t have the preprocessor directives set: temporarily modify your platform headers so that compilation stops when it tries to embed the old manifest. Open C:\\Program Files\\Microsoft Visual Studio 9.0\\VC\\crt\\include\\crtassem.h. Search for the \'21022\' string. In that define, put something invalid (change \'define\' to \'blehbleh\' or so). This way, when you\'re compiling a project where the <code>_BIND_TO_CURRENT_CRT_VERSION</code> preprocessor flag is not set, your compilation will stop and you\'ll know that you need to add them or made sure that it\'s applied everywhere.</p>\n\n<p>Also make sure to use Dependency Walker so that you know what dlls are being pulled in. It\'s easiest to install a fresh Windows XP copy with no updates (only SP2) on a virtual machine. This way you know for sure that there is nothing in the SxS folder that is being used instead of the side-by-side dlls that you supplied.</p>\n'}, {'answer_id': 72619, 'author': 'Roel', 'author_id': 11449, 'author_profile': 'https://Stackoverflow.com/users/11449', 'pm_score': 2, 'selected': False, 'text': "<p>I just remembered another trick that I used to find out which static libraries were ill-behaving: 'grep' through the static libraries for the string '21022'. HOWEVER, don't use the 'normal' grep tools like wingrep because they won't show you these strings (they think it's a binary file and look for the raw, non-unicode string). Use the 'strings' utility from the resource kit (now in the Russinovich site I think). That one will grep through binaries ok. So you let this 'strings' go through your whole source tree and you'll see the binary files (dlls and static libraries) that contain references to the wrong manifest (or to the manifest with the wrong version in it).</p>\n"}, {'answer_id': 1567715, 'author': 'remcycles', 'author_id': 182734, 'author_profile': 'https://Stackoverflow.com/users/182734', 'pm_score': 2, 'selected': False, 'text': '<p>For your third option, you can probably find the DLLs and manifests for the 9.0.21022 version in the C:\\WINDOWS\\WinSxS directory on your dev machine.  If you can, then you can setup your own redist directory and install those files with your app.</p>\n\n<p>Alternatively, you can use the 9.0.30729.1 ones supplied with Visual Studio and forge the manifest you install with your app to report that it supplies the 9.0.21022 DLLs, and not 9.0.30729.1.  The runtime linker doesn\'t seem to mind.  See this <a href="http://blog.kalmbach-software.de/2009/05/27/deployment-of-vc2008-apps-without-installing-anything/" rel="nofollow noreferrer">blog</a>, which has been immensely helpful for solving these problems, for more information.</p>\n\n<p>Both workarounds fixed the problems I had with deploying the DLLs as private assemblies with VS2008 Express.</p>\n\n<p>Roel\'s answer is the way to go for your first option ("fix it right"), but if you depend on a library that depends on 9.0.21022 (and your manifest therefore lists both versions), then the third option may be the only way to go if you don\'t want to run vcredist_x86.exe.</p>\n'}, {'answer_id': 1567728, 'author': 'remcycles', 'author_id': 182734, 'author_profile': 'https://Stackoverflow.com/users/182734', 'pm_score': 2, 'selected': False, 'text': '<p>Another nice tool for viewing exe and dll manifests is <a href="http://weblogs.asp.net/kennykerr/archive/2007/07/10/manifest-view-1-0.aspx" rel="nofollow noreferrer">Manifest View</a>, which fittingly enough will not run on a clean install of XP, because <em>it</em> depends on 9.0.21022.</p>\n'}, {'answer_id': 2153085, 'author': 'Dimitri C.', 'author_id': 74612, 'author_profile': 'https://Stackoverflow.com/users/74612', 'pm_score': 4, 'selected': False, 'text': '<p>To understand the problem, I think it is important to realize that there are <b>four version numbers involved</b>: </p>\n\n<ul>\n<li>(A) The version of the VC header files to which the .exe is compiled.</li>\n<li>(B) The version of the manifest file that is embedded in the resources section of that .exe. By default, this manifest file is automatically generated by Visual Studio.</li>\n<li>(C) The version of the VC .DLLs (part of the side-by-side assembly) you copy in the same directory as the .exe.</li>\n<li>(D) The version of the VC manifest files (part of the side-by-side assembly) you copy in the same directory as the .exe.</li>\n</ul>\n\n<p>There are two versions of the VC 2008 DLL\'s in the running:</p>\n\n<ul>\n<li>v1: 9.0.21022.8</li>\n<li>v2: 9.0.30729.4148</li>\n</ul>\n\n<p>For clarity, I\'ll use the v1/v2 notation. The following table shows a number of possible situations:</p>\n\n<pre><code>Situation | .exe (A) | embedded manifest (B) | VC DLLs (C) | VC manifests (D)\n-----------------------------------------------------------------------------\n1         | v2       | v1                    | v1          | v1         \n2         | v2       | v1                    | v2          | v2          \n3         | v2       | v1                    | v2          | v1\n4         | v2       | v2                    | v2          | v2\n</code></pre>\n\n<p>The results of these situations when running the .exe on a clean Vista SP1 installation are:</p>\n\n<ul>\n<li><p>Situation 1: a popup is shown, saying: "The procedure entry point XYZXYZ could not be located in the dynamic link library".</p></li>\n<li><p>Situation 2: nothing seems to happen when running the .exe, but the following event is logged in Windows\' "Event Viewer / Application log":</p>\n\n<p>Activation context generation failed for "C:\\Path\\file.exe".Error in manifest or policy file "C:\\Path\\Microsoft.VC90.CRT.MANIFEST" on line 4. Component identity found in manifest does not match the identity of the component requested. Reference is Microsoft.VC90.CRT,processorArchitecture="x86",publicKeyToken="1fc8b3b9a1e18e3b",type="win32",version="9.0.21022.8". Definition is Microsoft</p></li>\n<li><p>Situation 3: everything seems to work fine. This is <a href="https://stackoverflow.com/questions/59635/app-does-not-run-with-vs-2008-sp1-dlls-previous-version-works-with-rtm-versions/1567715#1567715">remicles2\'s solution</a>.</p></li>\n<li><p>Situation 4: this is <a href="https://stackoverflow.com/questions/59635/app-does-not-run-with-vs-2008-sp1-dlls-previous-version-works-with-rtm-versions/70808#70808">how it should be done</a>. Regrettably, as Roel indicates, it can be rather hard to implement.</p></li>\n</ul>\n\n<p>Now, my situation (and I think it is the same as <a href="https://stackoverflow.com/users/1441/crashmstr">crashmstr\'s</a>) is nr 1. The problem is that Visual Studio for one reason or another generates client code (A) for v2, but for one reason or another, generates a v1 manifest file (B). I have no idea where version (A) can be configured.</p>\n\n<p><b>Note</b> that this whole explanation is still in the context of <a href="http://msdn.microsoft.com/en-us/library/aa375674%28VS.85%29.aspx" rel="nofollow noreferrer">private assemblies</a>.</p>\n\n<p><b>Update</b>: finally I start to understand what is going on. Apparently, <a href="https://stackoverflow.com/questions/2289593/how-to-select-the-version-of-the-vc-2008-dlls-the-application-should-be-linked-to/2289740#2289740">Visual Studio generates client code (A) for v2 by default</a>, contrary to what I\'ve read on some Microsoft blogs. The _BIND_TO_CURRENT_VCLIBS_VERSION flag only selects the version in the generated manifest file (B), but this version will be ignored when running the application.</p>\n\n<h2>Conclusion</h2>\n\n<p>An .exe that is compiled by Visual Studio 2008 links to the newest versions of the VC90 DLLs by default. You can <a href="https://stackoverflow.com/questions/59635/app-does-not-run-with-vs-2008-sp1-dlls-previous-version-works-with-rtm-versions/70808#70808">use the _BIND_TO_CURRENT_VCLIBS_VERSION flag</a> to control which version of the VC90 libraries will be generated in the manifest file. This indeed avoids situation 2 where you get the error message "manifest does not match the identity of the component requested". It also explains why situation 3 works fine, as even without the _BIND_TO_CURRENT_VCLIBS_VERSION flag the application is linked to the newest versions of the VC DLLs.</p>\n\n<p>The situation is even weirder with public side-by-side assemblies, where vcredist was run, putting the VC 9.0 DLLs in the Windows SxS directory. Even if the .exe\'s manifest file states that the old versions of the DLLs should be used (this is the case when the _BIND_TO_CURRENT_VCLIBS_VERSION flag is not set), Windows <b>ignores</b> this version number by default! Instead, Windows will use a newer version if present on the system, except <a href="https://stackoverflow.com/questions/2289593/how-to-select-the-version-of-the-vc-2008-dlls-the-application-should-be-linked-to/2289740#2289740">when an "application configuration file"</a> is used.</p>\n\n<p>Am I the only one who thinks this is confusing?</p>\n\n<p>So <b>in summary</b>: </p>\n\n<ul>\n<li>For private assemblies, use the _BIND_TO_CURRENT_VCLIBS_VERSION flag in the .exe\'s project and <em>all</em> dependent .lib projects. </li>\n<li>For public assemblies, this is not required, as Windows will automatically select the correct version of the .DLLs from the SxS directory.</li>\n</ul>\n'}]
['https://Stackoverflow.com/questions/59642', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2723/']