How check if an executable is compatible to the installed .NET version - c#

I'm a software tester. I was given an executable to test on a given machine. In the test the executable behaved strangly which could not be explained by anybody.
After a lot of research and debugging I found the cause: The executable that was built for .NET target framework 4.6, but the machine was equipped with .NET 4.5.
This produced some "MissingMethodExeception" for even trivial methods like "string.Format()". Some try-catch caught these exceptions, but treated them in wrong way because nobody had expected them to occur.
A likewise issue has been described here:
Method not found: 'System.String System.String.Format(System.IFormatProvider, System.String, System.Object)
My questions:
Isn't Windows meant to warn me when I'm trying to run an executable that cannot be run properly since the necessary .NET version is not available?
What is the best practise to deal with this problem in general?
(I would have expected something like a checkbox "Dont execute if target network is not available" in VisualStudio?!)

Isn't Windows meant to warn me ...
You'll certainly should get a warning, not from Windows but from the CLR. The dialog looks like this, clicking Yes automatically gets the required framework version deployed and installed on the machine. The CLR performs this check by looking for the [TargetFramework] attribute embedded in the assembly. As noted, run ildasm.exe to verify this attribute. With the expectation that it is either missing or has a low enough value so the dialog does not trigger.
What is the best practice to deal with this problem in general?
It is procedural mistake, the assembly was built wrong. You know with high confidence that the compiler used reference assemblies that are only appropriate for .NET version 4.6. That has to be traced back to the machine that built it, most likely to be a build server. It was not setup correctly, the kind of mishap that is so common when the build engineer cut corners by avoiding using a licensed copy of Visual Studio. Or by favoring freeware tooling, Jenkins is a common scourge.
Beyond getting the [TargetFramework] attribute wrong or missing, this particular mishap is particularly easy to induce. All it takes is using the assemblies in c:\windows\microsoft.net\framework as reference assemblies instead of the proper ones that require a targeting pack and are installed in the c:\program files (x86)\reference assemblies directory. This Q+A has more leads.
Fixing a build server tends to have lots of resistance points, best thing to do as a tester is to file a bug report. You also want to write one for the broken catch-em-all exception handling that made the problem so difficult to diagnose.

To answer the questions:
No Windows is not meant to warn you that the desired .Net Framework is not installed to run this executable. However, Windows will log the error or application crash information in Windows Event Logs.
Re second question...
In general, executables are delivered, deployed or published using Installers. Installer can be configured/written such that they will warn user if .Net framework(required by the exe/dll) is not installed.

Related

How to make machine independent and .Net framework(dependency) independent 'EXE' file from .Net

I am creating a project on C#-.Net. The 'exe' file generated from the project is not executable on machines which do not have the .Net already installed. This error is popping up:
To run this application you first must install one of the following versions of the .NET.Framework: V4.0.30319
I don't want to make an installer file which installs the dependency files (.Net FW and other...) on PC.
As project requirement, I want to make an 'exe' that runs on every Windows PC without installing software or dependency sofware -> .Net FW. Just when clicked and the s/w exe should execute.
Is it possible to make such machine independent 'exe' for Windows from .Net ??
That's not exactly possible (there are some tools out there that will allow you to bundle dependencies, but I wouldn't recommend using them).
Your best possible approach to this is using an as-old-as-possible framework, for example .NET Framework 3.0. This way you'll use a framework version that is already preinstalled on pretty much all systems in use. Or in other words, you'll ensure your program runs on as many systems as possible out of the box. Just provide a link to the runtime in case someone is still missing it.
Also just to note that this is far better compared to what happened to the first few iterations of the .NET Framework: Those executables would just crash with a complex error message not telling the user that it's just the runtime missing. It improved a lot over time.
Also, just as an alternative: Have you thought about using ClickOnce deployment? This will allow you to provide users a simple and minimal installer they won't really see either. It will only download and install dependencies that are still missing. Also this is built into any edition of VisualStudio, even the Express ones.
This error is popping up
It is not an error. Just a friendly reminder to the user that your program need .NET 4 to be available before your program can run. He'll click "Yes, please!" and everything solves itself automagically.
You could create an installer to avoid the message. But, given that you don't want to do that, and it already takes care of it for you, there is very little point.
More about what this all looks like and why it works this way in this answer
"As project requirement, I want to make an 'exe' that runs on every Windows PC without installing software or dependency sofware -> .Net FW. Just when clicked and the s/w exe should execute.".
1) What if the version of Windows doesn't have any .Net Framework?
2) What if the version of Windows doesn't support any version of .Net Framework? (.Net didn't come around until Win2K/ME ish times, and Windows 95 won't take most .Net frameworks, 3.1 / 3.5 wont even take Mono)
"Is it possible to make such machine independent 'exe' for Windows from .Net ??"
Sounds like you're trying to make something like a Setup.exe that can be a single download that will work out the specifics after the fact... Actually the "machine independent" makes even C++ unsuitable, because while C++ will work on Windows, Mac, Linux, Unix, and a whole slew of more exotic systems with x86, x64, i64, PPC, ARM24/32/64 etc. (all of which exist with Windows installed on them, out there, in the wild, but are pretty rare) once the executable is compiled and linked, it will be targeted towards a single CPU architecture and OS. (OS/2, GEM and DOS all use .exe files, and there are some similarities between them, but most other OS don't require any specific file extension for executable binaries)
So, .Net isn't a terrible idea for this reason, any more than a .jar, .pl or .py would be. (which is relatively common for *nix software that you hope will run on Linux, Mac and BSD Unix... maybe even Solaris or HP/UX etc.) If you target MSIL, rather then x86 or x64, then your .exe will run on PPC Windows, DEC Alpha Windows, Itanium Windows, and ARM Windows, as well as the other two. (although this isn't what you are asking about) If you build it without a dependence on the WPF, or other Windows specific GUI engine, it will also work on Mac, Linux and BSD, so long as they have Mono installed. (it just may be worth considering, while you're at it... Versions of Windows Microsoft don't support with the appropriate .Net version will also need Mono to work this way)
To that end, I would recommend building a command-line executable in Mono, rather than .Net development environment. (Mono executable will run on .Net easier than .Net executables run on Mono... Though either is possible if you are careful about the dependencies you include in headers you import into your source)
I've had some success with this, writing a background service that would install on either Windows or Linux with the same binary executable. I used MonoDevelop. (https://www.monodevelop.com/) However, it's really just a flashy IDE around the core Mono development tools. (https://www.mono-project.com/)

C# com reference publish crash

I've never used C# before writing this app, and I loathe Visual Studio, so please bear with me if I am just being daft.
I've created a nice little utility that automates a number of painful processes at my job. I've presented it to a large number of people, including a number of people overseas who are keen to use it.
The only issue is... I cannot get the application to run on any machine besides my own. On any other machine, the application crashes immediately upon start.
I've positively ruled out any issues regarding .NET installation in the client machine by packing the application in an installer which installs the proper version of .NET if need be.
As of now, my thoughts are that I am handling packing my two COM references incorrectly or that the application is dying from a security exception.
The application utilizes the COM Excel reference as well as one other COM reference that any client running my app will have installed on their machine. I have attempted to both distribute the application relying on Visual Studio to work magic and pull needed references, and also to force the references to "CopyLocal". Isolating the references failed, but I'm not sure if that is necessary or not.
Does anyone have any clues as to what the issue could be? Don't hesitate to ask me for clarification on anything.
You are facing some deploy problem. Even if it is true that copying the assembli and its used dll on the target machine typically work pretty well with .NET you are in a situation not exactly that simple.
First of all:
nsure target machine have the .NET framework installed in a version compatible with the one you use for your program ( ie greather than or equal )
ensure the proper COM objects are present and registered on the target machine. And this is the tricky part.
Since you are using excel, try to install on the target machine the Primary interop assemblies ( PIA ) for office. This will probably solve your issue.

How do I deploy System.Management.Automation?

I am helping out with a project that a contractor worked on previously (so I don't have a lot of history for it).
The project builds fine, but when we try to perform some operations, we get a runtime error indicating that System.Management.Automation.dll could not be found.
As a troublshooting measure, we manually installed the dll into the installation directory. We then get an error indicating failure to load Microsoft.Management.Infrastructure.
As nearly as I can tell, these dlls are present in the Microsoft Management Framework download, and possibly in Powershell 3.0.
My question: What is the smallest package that these dlls are a part of, and what is the best way to deploy them for a production software release?
Edit
Just to be clear -- I am not looking to hack/frankenbuild by deploying just those dlls "naked", I am trying to identify the correct redistributable package for those dlls. I just can't seem to work out which one it is.
Edit
If it helps, the nature of the code that we are running is to programmatically create an exchange mailbox.
I think you can't legally redistribute any of those two DLLs alone (discussed for example here for the Automation, you can also check the "Redistributable" section on MSDN for those namespaces). You will have to make sure the target machines have PowerShell and the Management Framework.
Just in case anyone else runs into this problem: We ended up resolving the issue by deploying the Windows Management Framework 3.0, which includes the necessary assemblies. http://www.microsoft.com/en-us/download/details.aspx?id=34595

Common Language Runtime Detected an Invalid Program with ClickOnce

I have an InvalidProgram exception with the message
Common Language Runtime Detected an Invalid Program
This happen in an application that we didn't change in the last 3 month.
The only change is that we have change our build server (reinstall it).
The server is running Windows 8 and has Windows SDK 7.1 on it.
We package the application with ClickOnce.
This exception happen in a very specific method call, after methods of the same class as assembly are already called, so I think it rules out assembly loading issues.
I can't find a lead to where to start debug this issue. I think it related to the version of the tools I use on the build server such as MSBuild, CSC, mage.exe and such.
I found people say this error might happen when I have very long method names, but this does not seem to apply here because I don't have long methods names and I don't generate code myself.
The application use .NET 4.0
Update 1
It is for sure a problem with the compile tools (the version I think) or the ClickOnce packaging tools because when I compile and run the application on my machine it work, when I install the packaged application on my machine it show the exception above.
add this argument to your compiler: /nowin32manifest

Why might I get a DLL not found exception on Vista but not XP?

I have an app that relies on several managed libraries. These managed libraries in turn rely on some unmanaged libraries.
When I deploy the app to a machine running XP, it runs fine. When I do the same on a machine running Vista, I get a DLL not found exception.
I've tried both a VS2010 setup project and an NSIS installer to do the deployment and it's the same in both cases.
Why might this happen? What can I do to get around it?
Update - Further details
Both installers check for the installation of .NET 4.0 and install it if need be
The Vista computer is 64 bit, but the installation gets directed to the x86 program files folder as expected
In both cases I have an admin account
The DLLs are kept in the same directory as the executable
As far as I can tell, the files are getting copied to the right directory
Update 2
The full error is at http://pastebin.ca/2046487
The DLL is Audiere.Net.dll, which is one of mine and is a managed library.
I'm not sure if that error means that it can't find Audiere.Net.dll, or whether it can't load it because one of it's dependencies can't be found.
Update 3 - Stuff from Process Monitor
After running process monitor (thanks Mehrdad!) there are several entries which don't have a status of "SUCCESS". Some of these are "NAME NOT FOUND" and some are "PATH NOT FOUND". (It's even querying the PDB files, which I had thought were only used by a debugger.) It's really hard to see which entries might be the one leading to the actual failure. Anyway, I've uploaded the log (filtered to have a relevant path) in case it means something to anyone.
Update 4 - Added .pdb files
So I kinda got desperate and included the .pdb files to the output of the installer. I thought it would be useless, but it actually resulted in a more useful error. Rather than simply saying DLL not found, I now get a BadImageFormatException. Googling this tells me that this is a common problem for binaries compiled on x86 but being run on x64 (as the Vista machine is).
The suggested remedy is to force it to target x86, but Audiere.Net.dll already was. Could the fault lie with the library that it wraps?
Maybe there's some sort of redirection that's not actually letting your app install in the intended folder?
We'd need a bit more detail, but are you installing for the user or the machine? Are you an admin? Where is the DLL normally located?
Edit: Try using Process Monitor to monitor what file is actually being accessed.
If you are running a .Net application, do both computers have the proper Framework installed?
You mention that Audiere.Net.dll is targeted at x86, but what about your executable?
You can obviously recompile your program or use Corflags (part of the framework) to view the current setting on your exe.
Corflags ssd2.exe
Or to set or unset the flag
Corflags ssd2.exe /32BIT+
Corflags ssd2.exe /32BIT-
(Note, if your app is signed with a strong name it won't work unless you use /Force to remove the signature)
The solution turned out to be quite straightforward: one of the unmanaged DLLs needed to be recompiled for x64.
Key steps:
Check process monitor for likely sources of error. Look carefully at the error report that Windows offers to send when the app crashes.
Include the .pdb files for managed libraries. This seemed to result in more informative error messages.
These error messages not only specified which managed library was causing the error, but also indicated that it was a x86/x64 issue. (BadImageFormatException)
Following some sound advice, check that all of the unmanaged libraries are targeting x86. (Mine were, but it's good to be sure.)
Recompile the unmanaged dependencies of the troublesome managed library on an x64 machine.
Write an install script that copies the appropriate (x86 or x64) version of the DLL.
Profit!
Specifics:
The problem I had seemed to be with Audiere.Net.dll, but was actually caused by a problem with libaudieresharpglue.dll.
I use NSIS for installers. To accomplish the architecture specific DLL, I used a header called x64.nsh.
Usual reason is that the dll in question depends on other dlls which are not on the Vista machine (or possibly there but not registered.)
We ran into something simlar and found we needed to download the c++ Redistibuatable Package to get the program to run on windows 7 using 3rd party dlls.
I recall running into a similar issue with SQLite wrapper.
The source of the problem is the 32/64 bit issue of course, and it's the same scenario as the SQLite wrapper is a managed wrapper which makes it processor dependent.
I am guessing that while you're managed lib (Audiere.Net.dll) is compiled for 32 bits, you main application (ssd2.exe) is not.
The installation folder is determined by the configuration of the setup, but if the application project is not strictly configured to compile as a x86 project (usually targeting the default Any Cpu environment) then the application will launch as a 64 bit process, regardless of the installation path. This can be easily verified by looking at the process in task manager on a 64 bit machine, all 32 bit processes have an additional *32 on a 64 bit windows machine (they won't have it on a 32 bit machine).
EDIT: or more easily by looking at the project properties->Build-> Platform Target :)
Anyhow - you should change the project settings for the project that builds ssd2.exe to target x86 and you should be ok.

Categories