I created a Library with C# over a year ago under windows 7 32-bit and it works correctly, this library use the "User32.dll" and "Gdi32.dll" libraries.
at first I compiled this library for AnyCPU it work on 32bit but it does not work on win 64bit, I Also compiled it for 64bit CPUs but the same happened again.
my library uses the "RawInput devices" from "User32.dll" and "GetDeviceCaps" from "gdi32.dll".
This article in channel9 says:
Third party DLL's, which are 32 bit in nature, cannot be accessed from 64 bit clients. I have yet to see any workarounds for this that actually work. Apparently .NET DLL's will auto-adjust if compiled with "Any CPU" and called from a 32 bit or 64 bit host client.
And also in this question in MSDN:
A 64bit executable cannot call a 32bit dll and viceversa. Unless you actually need your application to be 64bit, the simplest option is to set it to target x86. This will still allow it to run on both 32 and 64bit versions of Windows.
If for some reason this is inapplicable, a possible solution would be to create a separate 32bit process that would load the 32bit dll, and have your 64bit application communicate with the other process, possibly using IPC (in some trivial case, redirecting the standard input and output can also work, or even just inspecting the return value of the process). In any case, this results in some extra work; I would advise you to review accurately your requirements first.
I try to integrate Web Camera support into C# solution. 1st of all I have learnt the nice example from Touchless.Vision. It contains C# solution with 3 components :
- one windows form project (C#, Any CPU)
- one wrapper project (C#, Any CPU)
- WebCamLib project (C++, x64)
I use Win7 x64.
So the example works fine. At least from the moment when I changes Platform target from Win32 to x64 for C library/project.
But then I have added two projects (C# wrapper and C++ project) under another C# solution. Now it always fails when it calls a method from C++ project.
System.BadImageFormatException was unhandled by user code
Message="Could not load file or assembly 'WebCamLib, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format."
Source="Touchless.Vision"
FileName="WebCamLib, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
FusionLog=""
StackTrace:
at Touchless.Vision.Camera.CameraService.<BuildCameraList>d__0.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Touchless.Vision.Camera.CameraService.get_AvailableCameras() in C:\CustomWare.NET\CustomWare\WebCamWrapper\Camera\CameraService.cs:line 40
The solution uses AnyCPU platform as well. I think there should not be any conflicts based on platform.
What can be the different between these two cases? What should I check?
The reason is that you're calling a 32-bit library from a 64-bit application. When using the AnyCPU configuration on an executable it will load as the OS type. 32-bit on a 32-bit machine, 64-bit on a 64-bit machine.
When calling a 32-bit library from a 64-bit executable there might differences in things like the data types which can lead to serious trouble. For example, C# specifies that a long is always 64 bits in size (it's an alias of System.Int64), while in C it's defined as at least 32 bits in size.
Or in your case, I assume the opposite applies.
So make sure to check whether your newly added C# library is indeed a AnyCPU or x64 project, and your C project is an x64 project. My guess is that the last one is not the case.
To provide a bit more insight on the subject:
AnyCPU is perfectly fine for # class libraries. It's the calling assembly that determines how to load an assembly. Thus, if you have a 32-bit application on a 64-bit machine calling an AnyCPU class library, it will load as if it were a 32-bit library.
In VS2010 the behavior changed compared to VS2008. When you create a Console, WPF or Forms application the target platform will always be x86 (32-bit). My suggestion is to use x86 unless you absolutely need or want to create a 64-bit version of your application. 64-bit applications have some benefits, and I'm sure that somewhere in the future the use of 32-bit applications will completely disappear, but because maintaining two versions doubles the time necessary to test the software, create releases, test those releases, and so forth, I usually don't bother creating a 64-bit version.
Rick Byers wrote an interesting article on this a while ago: AnyCPU Exes are usually more trouble than they're worth.
I need to use a COM-DLL from an external company (so I have no source code) that only works with the compile-option CPU-Target x86.
But my program is a "Any CPU" program and I don't want to change this.
So I read and google a lot and found out that I need 2 processes that communicate with IPC and WCF.
The problem: WCF isn't available with the .Net Framework 2.0.
So what is the best and easiest way to do it without change CPU-Target from my main program?
If you have a x86 target dll, be it a .Net assembly or a native dll then you must host this dll in a 32 bit process - in the case of .Net this means selecting the x86 platform, otherwise your dll wil fail to load on a 64 bit machine.
If you absolutely must have a 64 bit process when possible then your only real means of using this dll will be to create an external 32 bit process that "hosts" the dll and communicated with your main 64 bit process via IPC (interprocess communication). WCF is only 1 method of communicating between processes - its not available in .Net 2.0 however you can still use other methods such as .Net remoting.
See Interprocess communication for Windows in C# (.NET 2.0)
However all of this will be a pain to implement and maintain - unless you have a very good reason just compile your application with the x86 platform instead, at least until the external company release a 64 bit version.
If you don't want to change your assembly to "x86" then you need to use some form of IPC, of which WCF is only one. Another option is to used Named Pipes to communicate between the two processes.
In the past I've always written a .NET wrapper for COM and C++ DLLs and placed it within it's own class library.
The wrapper can deal with all interop calls to the dll.
It also allows you to perform data conversion and error reporting that would be meaningful to both parties (your NET application and the COM DLL).
I would think that this should solve your problem.
EDIT
Actually I've thought further on this and the above process won't work. This is because the X86, x64 and Itanium target processors are so fundamentally different.
edited x64 is able to run x86 targetted code and so can Itanium (which used an emulator and now an extension EM64T)
There's more information here
What you may be able to do though is run your x86 dll within a separate process and implement some form of communication between them. I guess that this is why you mentioned WCF. I say this because x86 software should run on x64 system.
This would mean that your solution would have two executables one x86 and one AnyCPU, as an assembly can only be targetted at one CPU type.
If so are the changes I would need to make to an existing program written in c# vast?
This MSDN page has more information on 64 bit Applications:
All applications built with the 1.0 and 1.1 releases of the .NET Framework are treated as 32-bit applications and are always executed under WOW64 on the 32-bit common language runtime (CLR) on a 64-bit operating system. In addition, 32-bit specific applications built with version 2.0 of the .NET Framework would run under WOW64 on 64-bit platforms.
So you need .NET 2.0 to be able to target 64 bit specifically.
This link might provide some pointers assuming it is Visual studio 2003 you are referring to:
http://www.toymaker.info/Games/html/64_bit.html
Visual Studio 2003 only targets the 32 bit platform, so you would need a newer version to create an application that can run in 64 bit mode.
Normally you don't have to make any code changes at all to make an application compatible with the 64 bit platform. I have done a lot of development and testing on a 32 bit computer and published on a 64 bit web server, and also the opposite.
I have used C# in Visual Studio with .NET, and I have played around a little with Mono on openSUSE Linux, but I don't really understand how it works.
If I write an app in Windows on .NET, how does this relate to Mono? I can't just execute an a Windows .exe file on Linux without Wine, so it doesn't help me execute apps developed in Windows.
Is the purpose purely to have a .NET library equivalent on Linux (and others) to make cross platform development easier? For example, if I was a business and wanted to reach Linux customers, but really wanted to use .NET, then Mono should be my choice? Or is there something more that I'm missing?
This is an old question (with an already selected answer) but I do not believe the question has really been answered well.
First, a little background...
How does .NET work?
A traditional Windows .EXE file is a binary file that represents a series of machine language instructions that your computer understands and that makes calls into the Win32 API which are parts of Windows that deliver services that applications can take advantage of. The machine language used is very specific to your kind of computer and the Win32 calls make the executable very dependent on Windows. A .NET executable is not like that.
It is important to realize that a .NET executable (.EXE file) is not actually a native Windows application. Windows itself does not understand how to run the code in a .NET executable. Your computer does not understand it either.
Much like Java, a .NET application is made up of instructions in a language called CIL (Common Intermediate Language) that you can think of as the machine language for an idealized computer that does not really exist. In .NET, the software implementation of this idealized machine is called the Common Language Runtime (CLR). The equivalent in the Java world is called the Java Virtual Machine (JVM). In Java, the equivalent to CIL is called Java bytecode. CIL is sometimes called MSIL (Microsoft Intermediate Language).
CIL is designed to run on the CLR (an idealized machine) but is otherwise platform independent, which means that the CIL does not care what kind of computer you have or what operating system you are running.
Just as you need a native version of the Java JVM on each platform on which you want to run Java, you need a native version of the CLR to run .NET CIL executables. The CLR is a native Windows application just like the traditional Win32 EXE files described above. The CLR itself is specific to the Windows implementation and computer architecture on which it was designed to run.
It does not matter what .NET language you start with (C#, VisualBasic, F#, IronPython, IronRuby, Boo, etc.), they all get compiled down to CIL bytecode. You can easily "disassemble" a CIL program into a form of object-oriented assembly language that is easily readable by humans. You can write a program in CIL directly yourself but few people do.
On Windows, the CLR compiles this CIL code Just-in-Time (JIT) right when you run the executable--just before the code is actually run. This means that the CIL bytecode is converted (compiled) to actual machine code that runs natively on your computer. This part of the CLR is called the JIT compiler or often just the JIT.
To date, Microsoft has released four versions of the CLR: 1.0, 1.1, 2.0, and 4.0. You need to have the right version of the CLR installed on your machine if you want to run .NET executables targeting that runtime. The CLR 2.0 supports .NET 2.0, 3.0, and 3.5 applications. For other versions of .NET, the .NET version maps cleanly to the CLR version.
In addition to the JIT/CLR, .NET provides a host of libraries (assemblies) that make up the rest of the .NET framework and that provide a host of capabilities and services that .NET applications can call upon. The great majority of these assemblies are pure CIL code which runs on the CLR. On Windows, a some make calls into the Win32 API as well. When you install .NET, you are installing the CLR, the class libraries (framework), and a bunch of development tools. Each version of the CLR generally requires a complete set of these "framework" assemblies. Some versions of .NET (eg. 3.0 and 3.5) added additional framework assemblies without updating the CLR or the existing assemblies associated with that CLR.
The Portable Executable (PE) file format that a Windows .EXE file is delivered in contains a header that describes the executable and identifies the file as a .NET file or a native Win32 file. When Windows tries to run a .NET file, it sees this header and automatically invokes the CLR on your behalf. This is why .NET EXE files appear to run natively on Windows.
Ok, so how does Mono work?
Mono implements the CLR on Linux, Mac, and other platforms. The Mono runtime (the CLR) is a native application written mostly in the C language and compiled down to machine language code for the computer system on which is designed to run. Like on Windows, the Mono runtime is specific to the Operating System and kind of machine you are using.
Just like on Windows, the Mono runtime (the CLR) compiles the CIL bytecode in your .NET executable Just-in-time to native code that your computer can understand and execute. In this way, a .NET file is just as "native" to Linux as it is to Windows.
To port Mono to a new architecture you need to port the JIT/CLR. This is just like porting any native application to a new platform.
How well .NET code runs on Linux or Mac is really just a question of how well the CLR is implemented on these systems. In theory, the Mono CLR could execute .NET code on these systems much better than the MS version of .NET does on Windows. In practice, the MS implementation is generally superior (though not in all cases).
In addition to the CLR, Mono provides most of the rest of the libraries (assemblies) that make up the .NET framework. Just as with the Microsoft version of .NET (in fact more so) the Mono assemblies are provided as CIL bytecode. This makes it possible to take a *.dll or *.exe file from Mono and run it unmodified on Windows, Mac, or Linux as CIL is the "native" language of the CLR implementations on these systems.
Just like on Windows, Mono supports multiple versions of the CLR and the associated assemblies:
Very early versions of Mono (before 1.2?) only supported CLR 1.0 or 1.1.
Mono did not support big chunks of the 2.0 framework until it's own 2.0 version.
Mono versions up to version 2.4 supported both CLR 1.1 and CLR 2.0 applications.
Starting with Mono 2.6, CLR 4.0 was added but CLR 2.0 was still the default.
Starting with Mono 2.8 the CLR 4.0 became the default and the CLR 1.1 is no longer supported.
Mono 2.10 continues to use the CLR 4.0 as default and also to support the CLR 2.0.
Just like the real .NET (but in far fewer cases) there are some Mono assemblies that call into native libraries. In order to make the System.Drawing assembly work on Mono, the Mono team wrote a Linux program to simulate the GDI+ portion of the Win32 API on Linux. This library is called 'libgdiplus'. If you compile Mono from source, you will notice that you need to build this 'libgdiplus' file before you can build 'mono'. You do not need 'libgdiplus' on Windows because the GDI+ portion of the Win32 API is already part of Windows. A full port of Mono to new platforms requires this 'libgdiplus' library to be ported as well.
In areas where the design of the .NET library is overly influenced by the design of Windows, and a poor fit for systems like Mac or Linux, the Mono team has written extensions to the .NET framework. The Mono extensions are also just CIL bytecode and generally work just fine on .NET.
Unlike on Windows, Linux generally does not detect .NET executables and launch the CLR by default. The user must usually run the CLR directly by typing 'mono appname.exe' or something similar. Here 'mono' is the application that implements the CLR and 'appname.exe' is the EXE file that contains the .NET code to be executed.
To make things easier for users, Mono applications are often wrapped in a shell script that launches the CLR. This hides the fact that the CLR is being used just as in Windows. It is also possible to tell Linux to launch the CLR when a file using the PE file format is encountered. This is usually not done as the PE file format is also used for native Win32 Windows executables which of course the CLR (Mono) does not support.
There is no technical reason why a PE launcher could not be used by Linux which then launches either a system that understands native Windows code (like Wine) or the CLR (Mono) as appropriate. This has simply not been done to my knowledge.
Back and forth
Any .NET code that sticks to "fully managed" code, which means it does not call into non-.NET code, should work fine on Mono on all platforms. I routinely use compiled .NET assemblies from Windows (for which I do not have the code) on Linux and Mac.
I can also take any code that I compile on Mono and run that on .NET on Windows. I can provide a client some code I compiled with Mono and not worry if he is on 32-bit or 64-bit Windows for example. The client does need to have the right version of .NET (the right CLR) installed fo course. CLR 2.0 has been around for a very long time and you can bet almost all Windows users have it installed. The Mono compilers and other code are also just CIL executables and so they run fine on Windows if you like.
Mono compatibility is good enough that large chunks of actual Microsoft code, like ASP.NET MVC, can be taken (where legal to do so) from the actual MS version of .NET and run on Mac or Linux. In general, the Mono team has done a great job of implementing both the CLR and the rest of the framework (class libraries/assemblies).
ASP.NET
On Windows, the Internet Information Server (IIS) knows how to call into the CLR to execute .NET as part of a web application. On Linux/Mac there is an Apache module (mod_mono) that provides similar capabilities to the Apache webserver. This application is written in C and must also be ported to new architectures.
Porting Mono
This discussion has identified parts of Mono that are built as "native" executables and must exist on a system on which you want to run .NET applications.
The CLR (including JIT compiler) - generally known as Mono
libgdiplus (for systems which do not natively support the GDI+ API [only Windows does])
mod_mono (to allow Apache to invoke the CLR for .NET web applications)
These three components, with the addition of the class libraries, provide a .NET environment that looks "native" to the .NET executable files you need to run.
That is how Mono works.
A Windows EXE contains multiple "parts". Simplified, the .net Code (=MSIL) is only a Part of the EXE, and there is also a "real" native Windows Part inside the EXE that serves as some sort of launcher for the .net Framework which then executes the MSIL.
Mono will just take the MSIL and execute it, ignoring the native Windows Launcher stuff.
Again, this is a simplified overview.
Edit: I fear my understanding of the deep deep details is not good enough for really much detail (I know roughly what a PE Header is, but not really the details), but i found these links helpful:
NET Assembly Structure – Part II
.NET Foundations - .NET assembly structure
You can in fact run a .NET .exe file with Mono on Linux. This does not require Wine. In fact, Mono compiles programs to .exe files, which can run either with Mono on Linux or as an executable on Windows.
Mono is an open-source implementation of Microsofts .NET CLR (Common Language Runtime). This is what runs part of .NET programs which are not in native code but in CIL (Common Intermediate Language), a language and machine-neutral intermediate language. The Runtime takes that intermediate code and translates it into machine code.
At the current state of Mono, you can take .NET programs that use the main parts of .NET (mscorlib.dll) and run them everywhere Mono runs, not just Windows.
But as it is mentioned that Mono is open source and you can't just rely that it will be the full .NET implementation, it has some controls that are not working, you must be also careful with P/Invokes that your application will use, for e.g your application will communicate with MCI (Multimedia Controller Interface) under win32. But I was using mono writing GTK# Applications also, but I've also used my Windows applications that worked without any recompilation as mentioned our fellow programmers above, that is, mono is an open source alternative of Microsoft's .NET, and by default if you are building either WinForms or Gtk# applications mono will compile and will create an .exe assembly for each file, and of course if you want it will create an Dynamic Link Library (DLL), almost as it is done in .NET. Just for suggestion try writing some Gtk# (with MonoDevelop IDE which has its built-in gui designer called stetic). And of course mono can be a great replacement for Web Services that you can create them on .NET and you can host them on Apache (because Linux hosting nowadays are more cheap than Windows ones) web services and other asp.net apps will work under apache with a mod_mono module that must be included in apache.
A little bit out of topic but I just wanted to tell you a sneak-peek from my experience.
Also you can take a look to MoMA (if your goal is to port applications from Win to Lin).
The Mono Migration Analyzer (MoMA)
tool helps you identify issues you may
have when porting your .Net
application to Mono. It helps pinpoint
platform specific calls (P/Invoke) and
areas that are not yet supported by
the Mono project.
MoMA
Here is a webapp that compares the types of the BCL already implemented by Mono and the .NET Framework 3.5
http://mono.ximian.com/class-status/2.0-vs-3.5/index.html
To further Michael's response, I believe you will have to recompile on Mono for the app to run on the Mono runtime. Exceptions may exist. I've only played around with Mono just a bit, and I've always re-compiled the source. I've never tried to run a .NET app directly on Mono.
Also, Mono 1.9 is supposed to be fully .NET 2.0 compliant. Mono Olive and Moonlight are supposed to add .NET 3.0 (less WPF) and Silverlight functionality.
It might help you, How does Mono's C# compiler work? and as well as Understanding Mono C# Compiler book.