BadImageFormatException - Advanced Installer Community

We’re moving our forums to a new platform.

You can still browse and search existing topics, but you can no longer create new topics or post replies.

For questions, technical issues, or feature requests, please don’t hesitate to contact our support team at support@advancedinstaller.com.

Thank you for your understanding!

dsr140080
Posts: 9
Joined: Tue Sep 29, 2009 9:12 pm

BadImageFormatException

I am creating an installation version of my application that includes a trial licensing option. Several of my users have reported receiving the following error message (and I was able to reproduce this behavior on my machine as well):

System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
at <MyApplication>.InitTrial(String aKeyCode, IntPtr aHWnd)

I have version 7.2 of Advanced Installer. I am running Visual Studio 2008 with .NET 3.5 and am compiling to x86. I copied the code for using the trial dll from the TrialTestExample.NET sample in the help file. I also added the following to the post-build event command line:
call "$(DevEnvDir)..\Tools\vsvars32.bat"
editbin.exe /NXCOMPAT:NO "$(TargetPath)"

What do I need to modify in my application/installation to prevent this error.
GabrielBarbu
Posts: 2146
Joined: Thu Jul 09, 2009 11:24 am
Contact: Website

Re: BadImageFormatException

Hello and welcome to Advanced Installer Forums,

I am unsure as to when you are encountering this error. Could you give me more details about when this message appears?

Thank you,
Gabriel
Gabriel Barbu
Advanced Installer Team
http://www.advancedinstaller.com/
dsr140080
Posts: 9
Joined: Tue Sep 29, 2009 9:12 pm

Re: BadImageFormatException

The message occurs when the main form is loaded at program startup.

In the constructor of the main form, I have the following:

Code: Select all

            Thread thread = new Thread(InitTrial);
            thread.Start();
This is what I have for the InitTrial() method:

Code: Select all

        private static void InitTrial()
        {
            try
            {
                // Wait 2 second for the main window to be visible.
                Thread.Sleep(2000);

                Process process = Process.GetCurrentProcess();
                InitTrial(kLibraryKey, process.MainWindowHandle);
            }
            catch (DllNotFoundException ex)
            {
                // Trial dll is missing close the application immediately.
                MessageBox.Show(ex.ToString());
                Process.GetCurrentProcess().Kill();
            }
            catch (Exception ex1)
            {
                MessageBox.Show(ex1.ToString());
            }
        }

and in the OnShown event handler, I have the following:

Code: Select all

            try
            {
                Process process = Process.GetCurrentProcess();
                DisplayRegistration(kLibraryKey, process.MainWindowHandle);
            }
            catch (DllNotFoundException ex)
            {
                // Trial dll is missing close the application immediately.
                MessageBox.Show(ex.ToString());
                Close();
            }
            catch (Exception ex1)
            {
                MessageBox.Show(ex1.ToString());
                //Close();
            }
The exception message references both InitTrial and the OnShown event handler.
GabrielBarbu
Posts: 2146
Joined: Thu Jul 09, 2009 11:24 am
Contact: Website

Re: BadImageFormatException

Hello,

It appears that the target platforms for trial.dll and your own application do not match. In your Visual Studio project options, please make sure you have either "x86" or "x64" selected as "Target CPU" (Do not select "AnyCPU", as this will not work). Nonetheless make sure the target CPU for your application matches the Platform field that you set in Advanced Installer, on the "Licensing" page, options tab.

Regards,
Gabriel
Gabriel Barbu
Advanced Installer Team
http://www.advancedinstaller.com/
dsr140080
Posts: 9
Joined: Tue Sep 29, 2009 9:12 pm

Re: BadImageFormatException

Hi Gabriel,

Thank you for your quick response. I checked the items you mentioned and I'm still having a problem.

My project is compiled as 'x86'. In the Advanced Installer Licensing page, I have selected 32-bit Unicode Library (DEP aware).

The dll routines are identified in my code as

Code: Select all

        // Main trial function
        // This function does all the work
        [DllImport("Trial.dll", EntryPoint = "ReadSettingsStr", CharSet = CharSet.Unicode)]
        static extern uint InitTrial(String aKeyCode, IntPtr aHWnd);

        // Use this function to register the application using the provided serial
        [DllImport("Trial.dll", EntryPoint = "DisplayRegistrationStr", CharSet = CharSet.Unicode)]
        static extern uint DisplayRegistration(String aKeyCode, IntPtr aHWnd);
They were originally set as CharSet = CharSet.Ansi, but changing them to CharSet.Unicode did not alleviate the problem.

Thank you for your help,

Scott
GabrielBarbu
Posts: 2146
Joined: Thu Jul 09, 2009 11:24 am
Contact: Website

Re: BadImageFormatException

Hi Scott,

If that did not solve your problem, then the cause might be different.
The code on this page contains an example on how to integrate licensing support into your application. Please observe the kLibraryKey #define on that page. This string must be set to the value found on the Licensing page->Registration tab, in the "Library Key" field.

Regards,
Gabriel
Gabriel Barbu
Advanced Installer Team
http://www.advancedinstaller.com/
dsr140080
Posts: 9
Joined: Tue Sep 29, 2009 9:12 pm

Re: BadImageFormatException

Gabriel,

Thank you for your response. I have the library key defined as a private static const string (I'm using C#).

The trial licensing worked in the initial installations of my program. I'm still in beta so I've been changing the version number each time I create a new installer. I respond 'Yes' the prompt that asks whether I want to generate a new product code. In the Upgrades panel, I have 'Automatically upgrade older product versions' and 'Uninstall old version first and then install new version' selected.

Is my problem somehow related to the different versions that I'm creating with the installer?

Thanks for your help.

Scott
GabrielBarbu
Posts: 2146
Joined: Thu Jul 09, 2009 11:24 am
Contact: Website

Re: BadImageFormatException

Hello Scott,
Is my problem somehow related to the different versions that I'm creating with the installer?
No, this should not affect your application as long as the Library Key remains the same.

I am not sure what is causing this behavior. Would it be possible for you to create a sample project (.aip and source code for the sample trial app) that accurately reproduces this behavior so we can analyse the implementation? If so, please send it to support at advancedinstaller dot com.

Thank you,
Gabriel
Gabriel Barbu
Advanced Installer Team
http://www.advancedinstaller.com/
dsr140080
Posts: 9
Joined: Tue Sep 29, 2009 9:12 pm

Re: BadImageFormatException

Hi Gabriel,

I was able to reproduce the problem in a stripped-down VS project. I have sent it via email.

Thank you,

Scott
dsr140080
Posts: 9
Joined: Tue Sep 29, 2009 9:12 pm

Re: BadImageFormatException

Have you been able to diagnose the problem?

Scott
GabrielBarbu
Posts: 2146
Joined: Thu Jul 09, 2009 11:24 am
Contact: Website

Re: BadImageFormatException

Hi Scott,

Unfortunately we did not receive your email. Could you please send it again so we can take a look at it?

Regards,
Gabriel
Gabriel Barbu
Advanced Installer Team
http://www.advancedinstaller.com/
dsr140080
Posts: 9
Joined: Tue Sep 29, 2009 9:12 pm

Re: BadImageFormatException

I sent the email again. The attachment was 9MB so I sent it using YouSendIt this time in case that was the problem.

Thanks,

Scott
GabrielBarbu
Posts: 2146
Joined: Thu Jul 09, 2009 11:24 am
Contact: Website

Re: BadImageFormatException

Hello,

Unfortunately we still did not receive the email. Please try to reduce the size of the attachment and then send it again. Alternately you can try uploading it somewhere and give us the link.

Regards,
Gabriel
Gabriel Barbu
Advanced Installer Team
http://www.advancedinstaller.com/
dsr140080
Posts: 9
Joined: Tue Sep 29, 2009 9:12 pm

Re: BadImageFormatException

Gabriel,

The zip file for the project can be found at http://www.utm.edu/staff/sroberts/AIF%20Test/.

Thanks,

Scott
GabrielBarbu
Posts: 2146
Joined: Thu Jul 09, 2009 11:24 am
Contact: Website

Re: BadImageFormatException

Hello,

We have studied your project. The issue was the following:
On lines 14 and 17 in MT1on1Main.cs you are using

Code: Select all

CharSet = CharSet.Unicode
This causes the Library Key to be read incorrectly. Change the above code to:

Code: Select all

CharSet = CharSet.Ansi
on both lines.

Also, we noticed you are calling DisplayRegistration() which prompts the user for the registration key when your application is started. We recommend calling InitTrial() instead, so that your client has a choice of whether to try out your product, register it or buy it.

I have sent you a private message on your forum account with further suggestions.

Regards,
Gabriel
Gabriel Barbu
Advanced Installer Team
http://www.advancedinstaller.com/

Return to “Common Problems”