1

我将 cimg(本机 c++ 头库)包装到 dot net 并对其进行了测试,它在 c++ CLR 中运行良好,我使用的数学方法看起来像这样。因为 cimg 只是一个头文件,所以我创建了一个 win32 静态库项目并构建了它,然后我在 CLR c++ 中使用了头文件,并将我的 win32 静态库添加到了附加库中(我这样做的原因是为了避免链接器错误),并且它工作正常,最后我尝试从 CLR 制作一个 DLL,以便我能够在我的 c# GUI 项目中使用它,所以我将 CLR c++ 项目的输出更改为 DLL,然后我在 c# winform App 中引用它然后当我尝试运行它时,我收到此错误消息。

    System.BadImageFormatException was unhandled
  HResult=-2147024885
  Message=Could not load file or assembly 'cpp tester, Version=1.0.4775.2046, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.
  Source=WindowsFormsApplication1
  FileName=cpp tester, Version=1.0.4775.2046, Culture=neutral, PublicKeyToken=null
  FusionLog==== Pre-bind state information ===
LOG: User = serak-PC\serak
LOG: DisplayName = cpp tester, Version=1.0.4775.2046, Culture=neutral, PublicKeyToken=null
 (Fully-specified)
LOG: Appbase = file:///C:/Users/serak/Desktop/Cimg Wrapper/WindowsFormsApplication1/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : WindowsFormsApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: No application configuration file found.
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Users/serak/Desktop/Cimg Wrapper/WindowsFormsApplication1/bin/Debug/cpp tester.DLL.
ERR: Failed to complete setup of assembly (hr = 0x8007000b). Probing terminated.

  StackTrace:
       at WindowsFormsApplication1.Form1.button2_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 WindowsFormsApplication1.Program.Main() in c:\Users\serak\Desktop\Cimg Wrapper\WindowsFormsApplication1\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
4

1 回答 1

2

您的 DLL 和应用程序的“位数”是否可能不匹配?如果您正在执行 32 位构建,请确保 C++/CLI 包装器 .DLL 和 WinForms C# GUI .EXE 都是使用平台目标x86(而不是“任何 CPU ”)构建的。要检查这一点,在 VS2010 中,您可以转到项目属性,然后选择“构建”选项卡。

于 2013-01-29T10:03:48.327 回答