ManagedDism icon indicating copy to clipboard operation
ManagedDism copied to clipboard

Error: An attempt was made to load a program with an incorrect format

Open Douuuglas opened this issue 5 years ago • 10 comments

First of all: thanks for sharing this well written library!

Situation

I've made an installer with WixSharp, in this installer i validate if the windows machine has the component "WCF-HTTP-Activation" activated. On some machines it's working, but in an azure Windows Server 2012 machine i'm having problems when i execute the code above. See if you can help me!

Here's the code

internal static bool IsHTTActivation35Ativated()
{
    try
    {
        DismApi.Initialize(DismLogLevel.LogErrors);
        using (var session = DismApi.OpenOnlineSession())
        {
            var feature = DismApi.GetFeatureInfo(session, "WCF-HTTP-Activation");
            if (feature == null)
                return false;
            if (feature.FeatureState != DismPackageFeatureState.Installed)
                return false;
            return true;
        }
    }
    catch
    {
        return false;
    }
    finally
    {
        DismApi.Shutdown();
    }
}

Here's the error messages

An attempt was made to load a program with an incorrect format.

StackTrace

   at BecomexInstaller.InstallerConfig.PrerequisitesInstallerConfig.IsHTTActivation35Ativated()   at Microsoft.Dism.DismUtilities.ThrowIfFail(Int32 hresult, DismSession session)
   at Microsoft.Dism.DismSession.Reload()
   at Microsoft.Dism.DismApi.OpenOnlineSession()

Project config

image

System where it's not working

image

System where it's working

image

Douuuglas avatar Jan 29 '20 14:01 Douuuglas

Nothing jumps out, both machines appear to be 64-bit. Can you try unchecking Prefer 32-bit? You might want to just build against x64 instead of Any CPU as well. The exception is only thrown when a 32-bit process tries to load a 64-bit assembly or visa versa.

jeffkl avatar Feb 03 '20 16:02 jeffkl

I tried both, none have worked. Wich archecture is the nuget of ManagedDism compiled with?

Douuuglas avatar Feb 18 '20 17:02 Douuuglas

I've downloaded the source, built with Any CPU and imported into my project and it worked!

Douuuglas avatar Feb 18 '20 17:02 Douuuglas

That's really weird, the assembly I published in the NuGet package is Any CPU

// C:\Users\jeffkl\AppData\Local\Temp\9f30a26a-fb72-48bb-b476-fb23fdac0d39\Microsoft.Dism.dll
// Microsoft.Dism, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5bba90053b345501
// Global type: <Module>
// Architecture: AnyCPU (64-bit preferred)
// Runtime: .NET 4.0

I've downloaded the source, built with Any CPU and imported into my project and it worked!

Did you have to modify how Microsoft.Dism.dll was being built?

jeffkl avatar Feb 20 '20 17:02 jeffkl

That's really weird! I haven't modified, i've just downloaded the master branch and built with Any CPU option 🤔

Douuuglas avatar Feb 26 '20 12:02 Douuuglas

Can you please attach your locally built Microsoft.Dism.dll so I can do a comparison?

jeffkl avatar Feb 26 '20 16:02 jeffkl

Sure!

Here it is: Microsoft.Dism.dll.zip

Douuuglas avatar Feb 27 '20 13:02 Douuuglas

That is very bizarre, everything seems to be the same. I'm going to do some minor cleanup and push a new package, maybe that'll work?

jeffkl avatar Feb 27 '20 16:02 jeffkl

We were also getting this same error on previous versions of windows, namely Windows Server 2016 x64. But that was because we were compiling the host application against x86... Not sure why Windows 10 allows it but it works there, just not on older versions.

Solution for me: change application to use x64 or Any CPU

Coffee-Boyy avatar Apr 05 '21 14:04 Coffee-Boyy

I can confirm the bug report of @ConnorChristie . The exception is thrown on OpenOnlineSession on Windows Server 2016 x64 whilst compiling the host application for win-x86. This is working fine for any flavor of windows 10 and windows server 2012, but throwing on windows server 2016.

I will try @ConnorChristie 's workaround and change to host target to win-x64 for publish.

fondencn avatar Jul 16 '21 08:07 fondencn