Why can we change the platform in the settings of the .NET project when it is platform independent?

I work with visual studio 2010, .NET 4.0 and WPF. We are trying to build a project with a platform like "AnyCPU" that will make the program platform neutral. In the middle of this, I began to get confused.

I thought that all .NET programs are targeting the .NET platform instead of the operating system. If so, I should not worry about whether I am working on an "x64" or "x86" system. All this target program is the .NET framework, which is the proper "Run Time", which takes care of itself. The CLR can allocate memory according to the platform on which it sits, but .NET projects should not worry about that.

What am I missing here? What is the meaning of the β€œplatform” in the build settings in .NET projects?

+4
source share
3 answers

One of the common reasons to limit yourself to one architecture is to use your own libraries that support only one platform.

For example, in one of my projects, I turned on libvorbis , and I was too lazy to compile its 64-bit version. So I just set my project to only 32 bits. Some other proprietary libraries may not support 64 bits at all, especially if they are closed source components of the source code.

+4
source

As already mentioned, when using other libraries created for a specific platform, you may need to specify this platform. A good example is COM.

Let's say you have a 32-bit COM component (which is the most), and the client OS is 32 bits. If you compile for "AnyCPU", it will work fine, because it will be JIT for its own x86 code, since you are working on a 32-bit system.

Now let's say that you have the same COM component, and the client OS is 64 bits. On Windows, when a COM component is registered, it will be registered in the 32-bit part of the registry (WOW6432Node, I think). Therefore, if you have an application compiled for "AnyCPU", it will be a native 64-bit application and will not be able to call your COM component (as a result of which the class is not registered), since it will look in the 64-bit registry. You will get the same error if you compile for x64 (since after JIT you get the same native code.) But if you compile for x86, it will work fine.

+4
source

As CodeInChaos said, this is a library issue. On x64, you cannot use x86 libraries. On x86, you can run x64 libraries.
Any cpu means that it will use your current platform settings (cpu works).

If you want to read more about this, here is a good read: http: //visualstudiohacks.com/articles / ...

+1
source

Source: https://habr.com/ru/post/1393965/


All Articles