前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >SQLite BadImageFormatException 处理

SQLite BadImageFormatException 处理

作者头像
jgrass
发布2024-12-25 18:30:54
发布2024-12-25 18:30:54
8200
代码可运行
举报
文章被收录于专栏:蔻丁杂记蔻丁杂记
运行总次数:0
代码可运行

问题背景

在一个 MFC 嵌 WPF 的项目中,WPF 使用了 sqlite 数据库,使用的是 System.Data.SQLite.Core 这个包,SQLite.Interop.dll 文件有 x64 x86 等多个版本, 如果拷贝到 MFC 输出目录中的版本不对,就会出现这个问题。

简单来说,就是 SQLite.Interop.dll 的框架版本与主应用程序的框架版本不匹配。

解决方案1

如果主应用程序的框架是确定的,可以直接使用 System.Data.SQLite.x64 或者 System.Data.SQLite.x86 这两个 nuget 包,直接解决问题。

问题是会引入很多其它可能不需要的包,而且固定版本,少了一点灵活性。

解决方案2

在编译后通过脚本自动拷贝

System.Data.SQLite: Downloads Page

Using XCOPY deployment in conjunction with the native library pre-loading feature, especially for customer machines, is highly recommended.

代码语言:javascript
代码运行次数:0
复制
<!-- 将 sqlite 依赖按照 x64/x86 框架,复制到 MFC 输出目录 --><Target Name="CopySqliteDllToOutput" AfterTargets="Build" Condition="'$(Configuration)' == 'Debug'">  <PropertyGroup>    <x64Source>$(OutputPath)runtimes\win-x64\native\SQLite.Interop.dll</x64Source>    <x86Source>$(OutputPath)runtimes\win-x86\native\SQLite.Interop.dll</x86Source>    <TargetDir>$(ProjectDir)..\xxx\xxx\..</TargetDir>    <x64Target>$(TargetDir)x64\SQLite.Interop.dll</x64Target>    <x86Target>$(TargetDir)x86\SQLite.Interop.dll</x86Target>    <ToDeleteFile>$(TargetDir)SQLite.Interop.dll</ToDeleteFile>  </PropertyGroup>
  <!-- 创建目标目录 -->  <MakeDir Directories="$(TargetDir)x64" />  <MakeDir Directories="$(TargetDir)x86" />
  <!-- 检查是否存在 SQLite.Interop.dll 源文件,如果不存在则报错 -->  <Error Condition="!Exists('$(x64Source)')" Text="x64 SQLite.Interop.dll not found at '$(x64Source)'. Build failed." />  <Error Condition="!Exists('$(x86Source)')" Text="x86 SQLite.Interop.dll not found at '$(x86Source)'. Build failed." />
  <!-- 删除原本存在的 SQLite.Interop.dll 文件 -->  <Delete Files="$(ToDeleteFile)" Condition="Exists('$(ToDeleteFile)')" />
  <!-- 复制 x64 版本的 DLL -->  <Copy SourceFiles="$(x64Source)" DestinationFiles="$(x64Target)" Condition="Exists('$(x64Source)')" />  <!-- 复制 x86 版本的 DLL -->  <Copy SourceFiles="$(x86Source)" DestinationFiles="$(x86Target)" Condition="Exists('$(x86Source)')" />
</Target>

sqlite - System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B) at - Stack Overflow

原文链接: https://cloud.tencent.com/developer/article/2481577

本作品采用 「署名 4.0 国际」 许可协议进行许可,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024年11月22日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 问题背景
  • 解决方案1
  • 解决方案2
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档