null PublicKeyToken for x86 assemblies - c#

I have a problem when compiling a managed DLL project. The solution consists of two projects, the first is a .NET DLL written in C# and the other is Managed C++ DLL that directly references the C# project.
Both projects/DLLs are strongly named with an snk file on disk. The C# dll has a target framework of "AnyCPU" while the Manage C++ project is compiled twice, one for an x86 target and the other for an x64.
My problem is that when I compile the Managed C++ project to target the x86 platform, the result DLL has a PublicKeyToken = null as reported by ILSpy. When compiling to target an x64 platform, the DLL has the correct PublicKeyToken. I have checked my project properties, the snk file is referenced correctly for both platform targets under Configuration Properties -> Linker->Advanced->Key File with no delay signing; the Target Machine option is also set correctly based on the desired compilation target.
Here is the information shown by ILSpy when I load my DLL.
For the x64 dll:
// MyDll.x64, Version=1.1.1000.1, Culture=neutral, PublicKeyToken=XXXXXXXXX
// Architecture: x64
// This assembly contains unmanaged code.
// Runtime: .NET 2.0
For the x86 dll:
// MyDll.x86, Version=1.1.1000.1, Culture=neutral, PublicKeyToken=null
// Architecture: AnyCPU (64-bit preferred)
// This assembly contains unmanaged code.
// Runtime: .NET 2.0
What concerns me is the Architecture description for the x86 assembly: AnyCPU (64-bit preferred)
I am not sure why its using the AnyCPU configuration and what the 64-bit preferred annotation means exactly?
I also like to mention that my project is built against .NET Framwork 2.0 for the C# project while the Managed c++ project is built against the v90 Platform Toolset. I am using Visual Studio 2010 running on a Windows 7 64-bit machine.
Can someone tell me why this is happening and how can I solve this issue?

It is simply a consequence of how the COR header in the assembly can indicate what processor architecture is desired. You can see the declarations in the CorHdr.h SDK header file, you'll find it in your Windows SDK directory on your machine. You can use the CorFlags.exe utility to display the values.
The only flag available is COMIMAGE_FLAGS_32BITREQUIRED. When set, it indicates to the CLR that you want to run the program in 32-bit mode, even on a 64-bit operating system. An additional flag got added in .NET 4.5, COMIMAGE_FLAGS_32BITPREFERRED, it resolves an ambiguity on ARM cores. Too many assemblies around where 32BITREQUIRED actually means "x86 required" instead of "32-bit required".
So there is nothing similar to a "64 bit required" flag, an assembly can only indicate "32-bit" or "doesn't matter". With the jitter providing the "doesn't matter" glue, it generates the architecture dependent machine code at runtime. Since the 32BITREQUIRED option isn't turned on in your assembly, the disassembler cannot display anything else but AnyCPU.
The next detail is the IMAGE_FILE_HEADER.Machine field in the PE header of the executable file, it indicates what kind of machine the executable can run on. That's a weak signal for .NET assemblies since they don't normally contain any executable code, just MSIL. And it is readily ignored by the Windows loader, .NET assemblies normally have this field set to IMAGE_FILE_MACHINE_I386 to indicate x86. You still get a 64-bit process out of such an EXE assembly, some pretty heroic loader structure patching occurs when such an EXE is loaded. The job of mscoree.dll, the "loader shim". More about that in this post.
Since you targeted x64 in your C++/CLI project, the IMAGE_FILE_HEADER.Machine was set to IMAGE_FILE_MACHINE_AMD64 by the linker. The disassembler saw that, thus producing the "64-bit preferred" annotation.
Don't be fooled by the word "preferred" here. The disassembler didn't look deep enough to see that your assembly actually contains machine code, generated by the C++/CLI compiler. They don't like to, there isn't any disassembler that will decompile the machine code back to C++/CLI source code. The assembly isn't ever going to run on a 32-bit operating system. Kaboom on a 32-bit OS, the program fails with error 11, ERROR_BAD_FORMAT, "An attempt was made to load a program with an incorrect format".
This answers your question, it doesn't otherwise have anything to do with a strong name.

Related

System.BadImageFormatException when use C# call C++

code is here https://github.com/Layty/cppnetdll
I have check the cpp dll and c# all is on x86, my pc is x64 ,I have also use both cpp and C# with x64
but it still tell me like
Unhandled exception. System.BadImageFormatException: Could not load file or assembly 'DLLMYX86, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. 试图加载格式不正确的程序。
File name: 'DLLMYX86, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' ---> System.BadImageFormatException: 试图加载格式不正确的程序。 (0x8007000B)
at MYNETX86.Program.Main(String[] args)
In .NET when you compile a managed assembly the compiler generate a intermediate code which is then compile at the fly by the JIT (just in time compiler) for the machine architecture where you are running your application.
A native (C++) dll is compiled for a specific architecture (could be x86 or x64) and the binary image file is specific for that architecture.
If you want to use a native dll from a .NET app you have to be sure that the JIT compiles the code for the same architecture the native dll was made for. The only way to be sure that everything is coherent is to compile the .NET assembly for that specific architecture. In Visual Studio this is possible switching the target architecture from Any CPU to the same of the native dll.
Your native dll is referenced as a regular managed dll. This will not work. Your options are
Rewrite the functionality in managed code
Use C++/CLI to produce a managed dll
Use PInvoke to call a native dll
I would prefer option 1 if possible. Interacting with unmanaged code is kind of a pain in the ass for anything except the most trivial signatures.
Your C# project targets .Net Core 3.1, so you need update your C++ project to target the same version.
In Project Properties > Advanced > C++/CLI Properties.
Set Common Language Runtime Support to .Net Core Runtime Support (/clr:netcore), then click Apply.
Set .Net Target Framework Version to netcoreapp3.1 or .NET Core 3.1.
I have solve this problem. I remove the pch.h and the pch.cpp and dllmain , then every thing run well .
But I dont know why ? it is just pre combile , and I have clean obj dir and clean project befor .
I have update my git code . just fix https://github.com/Layty/cppnetdll/blob/master/DLLMYX86/DLLMYX86/dllmain.cpp
or del this file.
I have make other both include cpp and C# .
othrs if have problem can download it. it works well
now I can new cpp object in c#

How to check if my C# app is using the right platform specific dll from nuget package?

I'm busy adding support for multiple .net frameworks and platforms. The different frameworks seem to work. But if the right platform specific dll is used I don't really know how to check this. Now it seems that the MSIL/Any CPU version is always used...
First the nuget package
I followed the descriptions at:
https://learn.microsoft.com/en-us/nuget/create-packages/supporting-multiple-target-frameworks
And I'm also using .targets and .props files.
For the code look at Pullrequest Opensource project: https://github.com/lucascebertin/Serilog.Sinks.Burst/pull/1
Using the nuget package
When using the Nuget package everything seems to work. But currently I do 2 checks to see if the right platform dll is used.
Note: The platform choice is MSIL/Any CPU, x86, and x64.
I do a diff between the dll in my package and the extracted/copyied one in the bin folder of the using project: Looks like the MSIL/Any CPU dll version is copyied.
The second check is from code to see which dll is used runtime:
var oraSinkAsm = Assembly.GetAssembly(typeof(Serilog.Sinks.Burst.Burst<>));
Console.WriteLine("Using {0} from {1}", oraSinkAsm.FullName, oraSinkAsm.CodeBase);
Console.WriteLine("Location: {0}", oraSinkAsm.Location);
Result is:
Using Serilog.Sinks.Burst, Version=1.0.1.0, Culture=neutral,
PublicKeyToken=null from
file:///C:/Develop/Serilog.Sinks.Oracle/src/Serilog.Sinks.Oracle/Serilog.Sinks.Oracle.ConsoleTester/bin/x64/Release/net452/Serilog.Sinks.Burst.DLL
Location:
C:\Develop\Serilog.Sinks.Oracle\src\Serilog.Sinks.Oracle\Serilog.Sinks.Oracle.ConsoleTester\bin\x64\Release\net452\Serilog.Sinks.Burst.dll
So above bin\x64\Release\net452\Serilog.Sinks.Burst.dll looks to be the MSIL version:
The original builded version in my dist\build folder is:
So they should be the same but diff states: I386 vs. AMD64???
My questions:
Does or Can the runtime C# use another dll than compiletime?
Are above checks sufficient or do I need others?
Is it really needed to build all platform flavors or is the MSIL/Any CPU dll sufficient?
There is usually no need to include any architecture-specific folders in your package unless it contains any assemblies that specifically target ARM, x86, or x64. This typically applies only to native (non-.NET) assemblies that your C# code may wrap.
If your package contains only managed assemblies, the most common thing to do is to build against Any CPU in release mode.

Any CPU not available in C++/C# solution

I have a solution that contains C# and managed C++ projects.
It compiles in the solution platform x64 and x86. Since it is managed C++ I wanted to create a 'Any CPU' solution and get rid of the old ones.
I changed the C++ project linker settings to Force Safe IL Image for both x64 and x86.
Next, using the Configuration Manager, I created a new solution platform called 'Any CPU'. Next I added a project platform also called 'Any CPU'.
I proceeded to set all the C# projects to 'Any CPU', but for the C++ I can't do that. The project platform 'Any CPU' is not in the drop down, and there is also no option 'New...'.
VS is adement about it, so I kept it like it was and started a build. To my surprise the result DLL (from the C++ project) was MSIL even though the platform for C++ was x64. Same happens when compiling x32, the resulting DLL is in MSIL.
What gives?
Why can't I set the C++ project to 'Any CPU'?
As far as I know, you cannot create an "AnyCPU" project type in Visual Studio for a C++/CLI project. However, you can configure your C++/CLI project (under the "Win32" project type) so that it compiles as pure, safe MSIL, without a target platform. Doing so will allow your C++/CLI DLL assembly to be used with an "AnyCPU" C# project. I.e. it's effectively "AnyCPU", even though that's not its actual name in the Configuration Manager.
In the "C/C++" project settings:
Common Language RunTime Support: Safe MSIL Common Language RunTime Support (/clr:safe)
In the "Linker" project settings:
CLR Image Type: just make sure this isn't set explicitly to IJW or PURE
Notes:
By using the "safe" project type, a few of the compiler and linker options which appear to affect platform type will be ignored. I.e. you don't have to go through and set everything to a non-specific platform type. Just the above. But you may set the other options to something appropriate, if it makes you feel better. :)
"Safe" will prevent the use of pointers. If this is an important issue, it is apparently possible to do albeit with a more complicated process. See Creating a pure MSIL assembly from a C++/CLI project? for details.
Don't forget that by default, Visual Studio will create C# projects that even though they are "AnyCPU" and even though they are executed on a 64-bit OS, will start up as a 32-bit process. This can hide platform-mismatch issues, if a dependency is x86 instead of pure/safe MSIL as intended. Just something be aware of (you can control this by unchecking the "Prefer 32-bit" option in the C# project's "Build" project properties page).
In order for the C++ functionality to be consumed by a C# dll, the C++ project must produce both x86 and x64 versions of the dll. It is not possible to reference just a x86 or a x64 dll from a C# dll compiled with the AnyCPU setting.
The trick to getting the AnyCPU dll to play with the C++ dll, is at runtime make sure the assembly cannot load the C++ dll and then subscribe to the AppDomain AssemblyResolve event. When the assembly tries to load the dll and fails, then your code has the opportunity to determine which dll needs to be loaded.
Subscribing to the event looks something like this:
System.AppDomain.CurrentDomain.AssemblyResolve += Resolver;
Event handler looks something like this:
System.Reflection.Assembly Resolver(object sender, System.ResolveEventArgs args)
{
string assembly_dll = new AssemblyName(args.Name).Name + ".dll";
string assembly_directory = "Parent directory of the C++ dlls";
Assembly assembly = null;
if(Environment.Is64BitProcess)
{
assembly = Assembly.LoadFile(assembly_directory + #"\x64\" + assembly_dll);
}
else
{
assembly = Assembly.LoadFile(assembly_directory + #"\x86\" + assembly_dll);
}
return assembly;
}
I have created a simple project demonstrating how to access C++ functionality from an AnyCPU dll.
https://github.com/kevin-marshall/Managed.AnyCPU

Identifying problematic dependencies in C++/CLI project

My application compiles fine, but I get the following runtime error:
System.IO.FileNotFoundException was unhandled
HResult=-2147024770
Message=Could not load file or assembly {Wrapper} or one of its dependencies. The specified module could not be found.
The reference to Wrapper in the calling Application looks correct. The Wrapper dll exists in the correct location.
This project used to build and run on someone else's system, I saw it demonstrated several times. That person/computer is no longer available. Some paths of some dependencies have changed since the last time it was successfully built and run, I have fixed all compile errors related to this.
Just to clarify my project structure:
Digraph G
{
App [ label = "My C# Application"]
Wrapper [ label = "C++/CLI Wrapper"]
Lib [ label = "C++ Library"]
Dll [ label = "My helper C# DLL"]
CDep [ label = "Series of deep C++ dependencies managed \n by CMake for Lib, hard coded relative paths for Wrapper."]
App->Wrapper->Lib->CDep;
App->Dll->Wrapper->CDep;
}
Wrapper is a C++/CLI wrapper around a C++ library. The error is triggered when we try to load a class in the Application that has a using statement for the Wrapper.
Wrapper does have a lot of dependencies, but the error message does not indicate which dependency is problematic. This is a large, complicated system, most of which is built by other teams. The C++ components use CMake to get all the dependencies correct, but CMake does not natively support C#.
I tried using fuslogvw to find the binding error, but it showed me absolutely nothing until I changed settings to include all binds, and then it only showed successful binds.
http://msdn.microsoft.com/en-us/library/e74a18c4(v=vs.100).aspx
The paths are long, but not over 256 characters.
I had a warning in the compile for Dll (see graph above):
Warning 1 There was a mismatch between the processor architecture of the project being built "MSIL" and the processor architecture of the reference "{cli_wrapper.dll}", "AMD64". This mismatch may cause runtime failures. Please consider changing the targeted processor architecture of your project through the Configuration Manager so as to align the processor architectures between your project and references, or take a dependency on references with a processor architecture that matches the targeted processor architecture of your project.
In Configuration Manager, the Dll is building for Platform "Any CPU" and Wrapper is building for "x64". I changed the dll to x64. I still get the runtime error.
Could not load file or assembly or one of its dependencies
Performed a clean, deleted build directory contents. No change.
Re-opened Visual Studio. No change.
Tried changing assembly name, default namespace, and project name to match. No change.
Could not load file or assembly 'xxx' or one of its dependencies. An attempt was made to load a program with an incorrect format
I believe we have to compile for 64 bit. We are dependant on a 64 bit C++ library.
Could not load file or assembly ... The parameter is incorrect
I am local administrator.
How to enable assembly bind failure logging (Fusion) in .NET
Tried the registry settings, but they appear to just be fuslogvw settings. No improvement in available log data.
Many other similar questions had ASP or service install specific answers.
I found the problem. There was more than one version of the library available to me, and I was using the include files from one version and the compiled library of the other. Dependancy Walker was key to finding which library had the problem, and if Aschratt had posted that as an answer rather than as a comment I would have accepted his answer.
I had the same error with a similar project earlier this week. First, when using C++/CLI, "Any CPU" doesn't exist. I had to build both for x86 to get through things.
Also, though my wrapper's dependencies were satisfied, it's the wrapper's exception that is caught by the CLR so it claims that the wrapper is missing a dependency. I was actually missing a dependency for the C++ DLL my wrapper wrapped (in my case, it was just a matter of forgetting to drop SDL2.dll and OpenAL32.dll in my new "Release" configuration output folder... I'd previously only worked with the Debug configuration where I'd already dropped those DLLs.
If you're positive you've got your wrapper's dependencies worked out, I'd recommend using Dependency Walker to check out the native DLL's dependencies. You could use Assembly.GetReferencedAssemblies with an outside script/program to double-check your wrapper as well (or ILSpy: http://ilspy.net/).
Last but not least, take a look at this: http://www.codeproject.com/Articles/442784/Best-gotchas-of-Cplusplus-CLI. He details the first two recommendations I made as well as some others.
Good luck!
One alternate root cause I've been running into quite a bit later, is that the application I'm working on uses impersonation, and the impersonated user does not have permissions to the GAC or to the folder that some dlls are in. You need to either give that user permissions, or change the way you do impersonation to load the dependencies before impersonation starts.

appdomain c++ c#

when we write
Assembly asm = AppDomain.CurrentDomain.Load(SomeByteArray);
when SomeByteArray read from .net .exe all is ok, and when from c++, error.
for this function is important using .net exe ?
if yes please other way to do this.
thanks
Are the calling assembly and the one you're loading both the same kind of build (x86, x64 or AnyCpu)? You can check the build by looking at them in ILDASM (look at the manifest, scroll to bottom and look for corflags).
If you built both assemblies yourself, then just check the property pages of the project for how you built it.
You either have to make sure they are the same, or if the .exe is AnyCpu, then the assembly you are loading must match whatever the OS is. Meaning it must be x86 on a 32-bit OS or x64 on a 64-bit OS.
(this is all assuming that SomeByteArray was loaded from the file correctly)

Categories

Resources