VS create winforms apps only with x86 configuration, why? - winforms

I have VS.NET 10.0.40219.1 SP1 in 2 computers and when I create a new C# Winform app only do it with Properties/Build/Platform x86, and the target let me choose AnyCpu/x64, but can't set it in the configuration (however, libraries work fine).
If I try to change it in the project file manually, I get a lot of nasty errors. Is this normal behavior, a bug or a problem in my installation?
P.D: When I change manually the project, this happend:
Warning 1 The OutputPath property is not set for project '*.csproj'.
Please check to make sure that you have specified a valid combination
of Configuration and Platform for this project. Configuration='Debug'
Platform='x86'. This error may also appear if some other project is
trying to follow a project-to-project reference to this project, this
project has been unloaded or is not included in the solution, and the
referencing project does not build using the same or an equivalent
Configuration or
Platform. c:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets
When I reopen the project, ALL the references become invalid

It is normal for Visual Studio to create WinForms projects with just an x86 platform. I have no idea if that's because historically x86 was the only platform type or because x86 is the most commonly used value or for some other reason.
It's also normal to have problems when the project file is manually modified, try to avoid doing that where possible.
To create another platform configuration in a C# project
Select Build | Configuration manager
Select <New...> in the Active Solution Platform field
Select the new type you want
Click on OK

Visual Studio used to create AnyCPU apps by default. I think both VS2005 and VS2008 did this, though they may have changed it for VS2008. Here's what happened: a lot of AnyCPU apps ended up broken on 64-bit systems, because they were built with dependencies on 32-bit libraries. If you tried to run an AnyCPU app that had a 32bit dependancy on a 64bit system, you got a 64bit process that would crash as soon as it tried to load your 32bit dll.
To fix this, the default was changed to use the x86 option. If you want to use AnyCPU, you must now setup the environment for it.

Related

System.BadImageFormatException with Sqlite

I am trying to use SQLite with my WPF application. I can get the reads and writes to work ok. The only problem I have is that my xaml is not displaying and I get the following error:
System.BadImageFormatException
Could not load file or assembly 'System.Data.SQLite, Version=1.0.80.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139' or one of its dependencies. An attempt was made to load a program with an incorrect format.
My questions:
-How do I know what version of SQLite that I am using? 32bit or 64bit?
-I tried changing the Platform target to x86 and x64 but neither help. How can I resolve this error?
Had same problem when used 64-bit precompiled binaries. Solution is use 32-bit ones instead (manually download them from SQLite site if needed).
A System.BadImageFormatException error often occurs when a 64-bit assembly is opened by a 32-bit application or Conversely. In this case, Visual Studio is a 32-bit application and the Design view is attempting to load a 64-bit assembly. Visual Studio assemblies are located in the project references tree.
Right click on Your Project and select properties item. In Build Tab, change platform target to Any CPU.

How can I determine an application's software prerequisites?

I'm developing a small WinForms program for home use - I have no intentions to releasing it anywhere.
Running it on my development machine, where I have Visual Studio and .NET and whatever else installed, works but deploying it on my PC, where I might not have all those frameworks, doesn't. I'm using some library written in C++ so I'm assuming I'm going to need some version of Visual C++ Redistributable, but I'm not sure which? And I'm not sure if that would be all. So, how can I determine an application's software prerequisites?
I used Dependency Walker and I got a message IEHIMS.DLL - Error opening file. The system cannot find the file specified (2), but that's all I could make out; there is so much information I'm having trouble make sense of it all. Besides, according to a quick google, IESHIMS.DLL has something to do with Internet Explorer, so it shouldn't really matter.
Thanks in advance to anyone taking to the time to consider my question.
Kind regards
For framework you can set the prerequisites for your application by going to project properties -> Publish -> Prerequisites. So now, if the required framework is not installed on the deployment machine, the setup will prompt for an install.
Now, for other dlls you are using, copy those dlls to your bin folder of the project and add the files while creating setup. This will solve the problem.

Compiling 32-bit application using Visual Studio on a 64-bit machine

I'm trying to compile a simple 32-bit Hello World application written in C using Visual Studio 2010 on a 64-bit machine on a Windows 7 fresh install. I also installed the "Windows SDK for Windows 7 and .NET Framework 4" after installing Visual Studio.
I built the application selecting "Win32" as platform. It works on Windows 7 but if I run the application on my 32-bit machine with Windows XP Professional (fresh install also this, without softwares and Service Packs) it seems not working getting this error:
"This application has failed to start because msvcr100.dll was not found"
If it can be useful Dependency Walker detects 2 errors (see the linked picture for details):
"Error: At least one module has an unresolved import due to a missing export function in an implicitly dependent module."
"Error: Modules with different CPU types were found."
http://img820.imageshack.us/img820/4725/errordp.png (PIcture)
How can I solve it? Thanks!
The machine on which it is run needs the runtime libraries. See this MSDN information.
Don't trust Dependency Walker on this... It clearly shows your exe is 32 bit. Your problem is with the VC redistributables, which are the CRT dlls - look for vcredist_x86.exe in your VS installation. You should run it before you run your app.
Another option is to statically link the CRT. See the /MT option. Will make your exe larger, but save the vcredist stuff.
The problem here is the C runtime used by the compiled program is missing on the Windows XP machine. Visual Studio 2010 will by default us the 10.0 (msvcr100.dll) runtime which is not available on XP by default. You need to manually deploy the C runtime on your machine in order for your program to run.
You may want to read the following MSDN article which discusses the issues around deploying C applications built with Visual Studio 2010
http://msdn.microsoft.com/en-us/library/zebw5zk9.aspx
There may be additional 32 / 64 issues at work but this appears to be the primary problem
The answers about the runtime library are correct. Another possible solution is to link to the static runtime libraries rather than to the DLL versions. This way you can build an executable that you can drop onto any machine without extra deployment hassles.
It's a trade-off, but, without knowing more about your situation, it might be worth considering.

How do I compile C++/CLI code for Silverlight?

I have a C++/CLI library that I would like to use in a Silverlight application. It is supposed to be possible to write code for Silverlight in any .NET language, but so far I've only worked out how to compile C#. Silverlight does not seem to be able to use DLLs compiled for .NET.
I'm using Visual Studio 2010 and Silverlight 4. The only new projects available for Silverlight are C# projects. Porting the code to C# is not a practical option.
How do I compile C++/CLI code for Silverlight?
I think I may have gotten a VS2010 C++/CLI class library project to build with references to (only) Silverlight assemblies.
Update
Ok, it is possible. But it is not nice.
First, you must convince the C++ compiler to NOT load the .NET Framework, using an undocumented compiler switch. But that's not the worst part.
Set your C++/CLI project "Common Language Runtime Support" to /clr:safe
Next, under References, remove all references.
Next, in the C++/CLI project properties, under C++ > Command Line, enter /d1clr:nomscorlib /FU"C:\Program Files (x86)\Microsoft Silverlight\4.0.50917.0\mscorlib.dll"
Now, save the project and exit Visual Studio. Open the .vcxproj in a text editor, and change the framework version setting. You want it to be the same as a C# Silverlight project:
<TargetFrameworkIdentifier>Silverlight</TargetFrameworkIdentifier>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<SilverlightVersion>$(TargetFrameworkVersion)</SilverlightVersion>
Now, reopen Visual Studio, and build the project. You'll get an error because the compiler auto-generated a file with #using<mscorlib> and the search path finds the .NET Framework version first.
Silverlight,Version=v4.0.AssemblyAttributes.cpp(1): fatal error C1197: cannot reference 'c:\windows\microsoft.net\framework\v4.0.30319\mscorlib.dll' as the program has already referenced 'c:\program files (x86)\microsoft silverlight\4.0.50917.0\mscorlib.dll'
Double-click the error to open the auto-generated file. Replace the path-less reference with e.g. (here's where you put your references, not in the project properties)
#using <c:\program files (x86)\microsoft silverlight\4.0.50917.0\mscorlib.dll>
#using <c:\program files (x86)\microsoft silverlight\4.0.50917.0\System.dll>
#using <c:\program files (x86)\microsoft silverlight\4.0.50917.0\System.Core.dll>
Luckily, the compiler leaves your changes in-place. So you should be good as long as no one cleans your temp directory.
Building should now succeed.
Then, you need to go add the DLL created by the C++/CLI project to your Silverlight application. Note that you can't set up a project reference, because VS2010 still isn't convinced that the C++/CLI is a Silverlight project. So you'll have to browse and add the reference as an assembly file. (And it won't automatically switch between Debug and Release to match the Silverlight application).
Final Notes
I got it to run an empty Silverlight application in Debug mode and stop at a breakpoint in the middle of C++/CLI code. Also the C++/CLI code successfully returned a value to C# and the local variable in C# received the correct value. So I guess it's working.
I went through a bunch more steps trying to make this work, but I don't think they affected the outcome. If you run into errors, though, let me know and I'll try to figure out what I omitted from this answer.
Ben Voigt, thanks for this, it worked for me too.
Also, if your C++ code does anything that is specific to the C++ language (i.e. not entirely IL portable) like using stack semantics for an array, you'll get the following error:
could not find assembly 'Microsoft.VisualC.dll' etc.
If you recompile with the full .NET Framework and then dump the IL code, you'll find references to "''.$ArrayType$$$BY06$$CB_W modopt" or something similar. This tells you where to change the code.
I found that after I installed the Silverlight SDK and it got added to "\Program Files(x86)\Reference Assemblies" I did not have to go through all of Ben Voigt's steps, just changing the project file was enough.
Another note, you can also use:
<TargetFrameworkProfile>WindowsPhone71</TargetFrameworkProfile>
if you want to target Windows Phone (install the SDK first).
Silverlight does not support native C++ libraries, nor any P/Invoke scenarios due to security concerns. If your library is pure .Net you might be able to decompile it with ILDASM and recompile for Silverlight with ILASM.
Silverlight is not a powerful development platform like .NET which is tightly integrated with operating system. First of all silverlight is supposed to run on any operating system, so there is no choice of Native API anywhere in silverlight.
Silverlight also does not support MSIL completely, so there is lot of problem in compiling and recomiping it at IL level.
Can you say more about what kind of C++/CLI code you have? Most Rich internet applications (Silverlight's target) do not include any of high powerful computation, instead they are plain simple HTML+JS alternatives. For powerful graphics, you can use Silverlight's PixelShadder support.
Reflector
What you can do alternatively is,
Compile your C++/CLI to regular .NET DLL, use Reflector to disassemble and generate C# source code from your dll, it may not be perfect, but you will have most of your logic converted back in C#.
I was able Ben Voigt's solution to work with some minor changes in Visual Studio 2013
Here is what I did different.
Unload the silverlight project that you want to reference it in. Right click and choose edit project.csproj
Copy the Target Framework Settings. For me this was
<TargetFrameworkIdentifier>Silverlight</TargetFrameworkIdentifier>
<TargetFrameworkVersion>v5.0</TargetFrameworkVersion>
<SilverlightVersion>$(TargetFrameworkVersion)</SilverlightVersion>
For the compiler switch /d1clr:nomscorlib This was not working for me. Also a reference to mscorlib for silverlight was automatically added to the complied output without specifying it on the command line options. This is how I got around it.
Open the project in Just Deompile
Load the Assembly Editor Plugin
Navigate the tree to the references
Delete the reference to the non silverlight mscorlib from the reflexil menu.
Right click on the top level of the tree for the assembly, and save as under the reflexil menu.
I haven't tested all of the functions, but the ones I have tested so far worked as expected.
Thank you Ben, your post has saved me a lot of time. I was thinking I was going to have to port my library:)

Cannot run 32bit compiled WPF applications on Windows 7 64bit

I created a WPF project in VS2008 and compiled it with Any CPU, x64 and x86. Any CPU and x64 works, but compiling to x86 the application is hanging when running through VS2008 and crashing when running without debugging. Debugging it with WinDbg I can see a StackOverflowException and sometimes a MissingMethodException relating to WPF methods.
Common sense is telling something here that the CLR is not loading the correct assemblies or something when running 32bit WPF apps. I tried reinstalling .NET Framework 3.5 SP1, but it does not fix the problem. I don't know how to go about checking if the correct assemblies are loaded or used.
Any ideas?
UPDATE: Not a real solution but the best I could do quickly was to reinstall Windows 7
Try to force the right compiler in all projects of your solution :
Properties
Build
Platform target
Try forcing Windows to always use the 32bit CLR and see if it still crashes:
C:\WINDOWS\Microsoft.NET\Framework64\v2.0.50727\Ldr64.exe setwow
-Oisin
I think I ran into this issue also. If I remember correctly, I had a problem where a dependency was compiled for x86 and the main app was compiled for x64, or the other way around. i.e. say I had a library and an app with a main function. I think they both need to compiled the same way. i.e. I don't think you can mix and match. This might not have been the exact same issue. I would go through all the projects in the solution make sure they all have the same settings.

Resources