Mihai
Mihai

Reputation: 13

64 bit ATL Control does not work with VC++ Forms app

I have created an ATL control in a 64 bit unmanaged dll.

I want to use this control in a VC++ WinForms app.

When I run the application I get badimageformatexception.

Then I have created an VB app and a C# app. In both cases it works with no problem. I have then tested my control with TstCon (MFC-64bit) provided by microsoft. The control was recognized with no problems and I was able to call some of its methods.

The only case where I was NOT able to make it work is VC++ Forms (I work with VS 2010 pro).

I have created a small test example.

Please download it from here:

https://rapidshare.com/files/2799321813/atl.zip

The ATL Control is located in atl_project_64 folder. It is a simple control with 1 property (Number) and one event (OnChange). I have followed all steps from here : http://msdn.microsoft.com/en-us/library/599w5e7x.aspx

Compile the project.

Then, I have created 2 WinForms apps for testing the control. The C# is working with no problems. Dont forget to add the reference to Atl_project_64Lib. If you cannot find it in the list of Com objects, you may browse it to the folder where the dll is created.

However, VC++ project is not working. You will get badimageformatexception if you try to run it. Sometimes the error is raised when I press the button which sets the value of the property.

All are 64 bit.

Do you have any idea what I have done wrong ?

thanks, mihai

Upvotes: 1

Views: 480

Answers (1)

Hans Passant
Hans Passant

Reputation: 942518

The BadImageFormatException is always a sure sign that your program is using a DLL with the wrong bitness. The troublemaker in your case is the interop library that was generated for the ActiveX control in your C++/CLI project. This is what you'll see what you run corflags.exe on it:

C:\projects\atl\test_atl - VC++\x64\Debug>corflags Interop.atl_project_64Lib.1.0
.dll
Microsoft (R) .NET Framework CorFlags Conversion Tool.  Version  4.0.30319.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Version   : v4.0.30319
CLR Header: 2.5
PE        : PE32
CorFlags  : 3
ILONLY    : 1
32BIT     : 1
Signed    : 0

Note that the 32BIT flag is turned on, kaboom there. This trouble started when you added a reference to the type library in your C++/CLI project. Unfortunately, the tool that generates the interop library ("Managed Wrapper Generator Tool") is creating an interop library that's 32-bit only. Hard to call that anything else but a bug. I can see how it happened though, type libraries do have a (small) bitness dependency too.

Fix this problem by running Tlbimp.exe yourself instead of relying on the IDE to get it wrong. You already have an interop library that's good btw, the one in the C# project is proper and has the 32BIT flag off. Something weird with that project btw, the interop library got stuck in the obj directory. Not sure how that happened, didn't look.

Upvotes: 2

Related Questions