Retired Microsoft Blog disclaimer

This directory is a mirror of retired "Decrypt My World" MSDN blog and is provided as is. All posting authorship and copyrights belong to respective authors.
Original URL: https://blogs.msdn.microsoft.com/alejacma/2011/11/07/problems-when-compiling-and-running-code-that-uses-certenroll-with-net-4-0-and-x64/
Post name: Problems when compiling and running code that uses CertEnroll with .NET 4.0 and x64
Original author: Alejandro Campos Magencio
Posting date: 2011-11-07T09:14:04+00:00


Hi all,

 

The other day a customer of mine was trying to compile and run this CertEnrollsample of mine: How to create a certificate request with CertEnroll and .NET (C#)

I had developed this sample against .NET 2.0 some time ago, and it compiled and run just fine.

But my customer created a .NET 4.0 project with Visual Studio 2010, he added the required references to CERTCLIENTLib (CertCli 1.0 Type Library in certcli.dll) and CERTENROLLLib (CertEnroll 1.0 Type Library in certenroll.dll), and when he tried to copile in Debug mode with Any CPU platform, he got many errors like this:

Error3The type 'CERTENROLLLib.CX509PrivateKeyClass' has no constructors definedC:\CATest\CATest\Form1.cs3745CATest
Error17The type 'CERTENROLLLib.CX509ExtensionKeyUsageClass' has no constructors definedC:\CATest\CATest\Form1.cs4459CATest
Error19The type 'CERTENROLLLib.CX509ExtensionEnhancedKeyUsageClass' has no constructors definedC:\CATest\CATest\Form1.cs4579CATest
Error11The type 'CERTENROLLLib.CX509EnrollmentClass' has no constructors definedC:\CATest\CATest\Form1.cs4141CATest
Error25The type 'CERTENROLLLib.CX509EnrollmentClass' has no constructors definedC:\CATest\CATest\Form1.cs18641CATest
Error1The type 'CERTENROLLLib.CX509CertificateRequestPkcs10Class' has no constructors definedC:\CATest\CATest\Form1.cs3655CATest
Error9The type 'CERTENROLLLib.CX500DistinguishedNameClass' has no constructors definedC:\CATest\CATest\Form1.cs4044CATest
Error13The type 'CERTENROLLLib.CObjectIdsClass' has no constructors definedC:\CATest\CATest\Form1.cs4239CATest
Error15The type 'CERTENROLLLib.CObjectIdClass' has no constructors definedC:\CATest\CATest\Form1.cs4337CATest
Error7The type 'CERTENROLLLib.CCspInformationsClass' has no constructors definedC:\CATest\CATest\Form1.cs3940CATest
Error5The type 'CERTENROLLLib.CCspInformationClass' has no constructors definedC:\CATest\CATest\Form1.cs3838CATest
Error23The type 'CERTCLIENTLib.CCertRequestClass' has no constructors definedC:\CATest\CATest\Form1.cs12843CATest
Error21The type 'CERTCLIENTLib.CCertConfigClass' has no constructors definedC:\CATest\CATest\Form1.cs12741CATest
Error4Interop type 'CERTENROLLLib.CX509PrivateKeyClass' cannot be embedded. Use the applicable interface instead.C:\CATest\CATest\Form1.cs3749CATest
Error18Interop type 'CERTENROLLLib.CX509ExtensionKeyUsageClass' cannot be embedded. Use the applicable interface instead.C:\CATest\CATest\Form1.cs4463CATest
Error20Interop type 'CERTENROLLLib.CX509ExtensionEnhancedKeyUsageClass' cannot be embedded. Use the applicable interface instead.C:\CATest\CATest\Form1.cs4583CATest
Error12Interop type 'CERTENROLLLib.CX509EnrollmentClass' cannot be embedded. Use the applicable interface instead.C:\CATest\CATest\Form1.cs4145CATest
Error26Interop type 'CERTENROLLLib.CX509EnrollmentClass' cannot be embedded. Use the applicable interface instead.C:\CATest\CATest\Form1.cs18645CATest
Error2Interop type 'CERTENROLLLib.CX509CertificateRequestPkcs10Class' cannot be embedded. Use the applicable interface instead.C:\CATest\CATest\Form1.cs3659CATest
Error10Interop type 'CERTENROLLLib.CX500DistinguishedNameClass' cannot be embedded. Use the applicable interface instead.C:\CATest\CATest\Form1.cs4048CATest
Error14Interop type 'CERTENROLLLib.CObjectIdsClass' cannot be embedded. Use the applicable interface instead.C:\CATest\CATest\Form1.cs4243CATest
Error16Interop type 'CERTENROLLLib.CObjectIdClass' cannot be embedded. Use the applicable interface instead.C:\CATest\CATest\Form1.cs4341CATest
Error8Interop type 'CERTENROLLLib.CCspInformationsClass' cannot be embedded. Use the applicable interface instead.C:\CATest\CATest\Form1.cs3944CATest
Error6Interop type 'CERTENROLLLib.CCspInformationClass' cannot be embedded. Use the applicable interface instead.C:\CATest\CATest\Form1.cs3842CATest
Error24Interop type 'CERTCLIENTLib.CCertRequestClass' cannot be embedded. Use the applicable interface instead.C:\CATest\CATest\Form1.cs12847CATest
Error22Interop type 'CERTCLIENTLib.CCertConfigClass' cannot be embedded. Use the applicable interface instead.C:\CATest\CATest\Form1.cs12745CATest

To get rid of these compilation errors we changed "Embed Interop Types" to "False" in the Properties of the CERTCLIENTLib and CERTENROLLLib references.

Still, when we tried to run the code, we got this exception:

System.BadImageFormatException was unhandled
Message=Could not load file or assembly 'Interop.CERTENROLLLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.
Source=CATest
FileName=Interop.CERTENROLLLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
StackTrace:
at CATest.Form1.createRequestButton_Click(Object sender, EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at CATest.Program.Main() in C:\_PUBLIC\CATest\CATest\Program.cs:line 17
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

I still didn't have time to figure out why this happens, but if you compile against x86 platform instead of Any CPU platform (which makes the code to run against x64 platform on x64 systems by default), it will work fine.

I hope this helps. Please, if someone knows why this happens, please share it with the community. And if someone needs to run this code in x64 with .NET 4.0 and we still don't know the answer, open a Technical Support case with us so we can spend the time required to investigate this. Thank you.

Regards,

 

Alex (Alejandro Campos Magencio)


Share this article:

Comments:

Comments are closed.