GDAL C# “OSGeo.GDAL.GdalPINVOKE”的类型初始值设定项引发异常 解决方法

        在使用C#版本的GDAL开发的时候,编译正常,启动的时候就会提示:“OSGeo.GDAL.GdalPINVOKE”的类型初始值设定项引发异常。

        对于这个问题,原因主要就是没有找到dll导致的,在使用的时候不但要把C#的那八个dll拷贝到exe所在目录,还需要把gdal19.dll及其依赖的dll都拷贝过去,很多同学以为只要有了那八个C#版本的dll就ok了,其实是不对的,C#版本的8个dll只是导出了C#的接口而已,调用的还是C++版本的gdal库。

        对于Python版本也是一样,需要将gdal19.dll及其依赖的dll全部拷贝到python的安装目录下。


————————华丽的分割线,下面2012-12-14日添加————————————————

       在使用C#的版本的时候一定要注意编译GDAL的时候用的编译器和版本,比如你下载的GDAL的用VS2008编译的32位的dll,那么在C#中使用的时候先要安装Visual Studio2008的C++运行库,具体可以去微软的官方搜(注意VS的版本也有好多,有SP1的版本,还有没有打过SP的版本,运行库都不一样)。完成了这些步骤,建议使用GDAL的那些exe工具测试一下,双击其中一个,如果可以弹出帮助信息说明OK,如果不行请继续找对应的C++运行库。

      上面只是说明GDAL在你的系统中可以正常工作,下面是在使用C#调用的时候,需要把配置管理器中的anycpu进行修改,如果GDAL是32位的,那么这里需要创建一个X86的,如果是64位的,创建一个X64的,切忌一定要对应,否则就会引发一些异常,比如下面的图片所示。


System.TypeInitializationException HResult=0x80131534 Message=“OSGeo.GDAL.GdalPINVOKE”的类型初始值设定项引发异常。 Source=gdal_csharp StackTrace: 在 OSGeo.GDAL.GdalPINVOKE.Open(Byte[] jarg1, Int32 jarg2) 在 OSGeo.GDAL.Gdal.Open(String utf8_path, Access eAccess) 在 遥感影像地物分类.Form1.导入数据ToolStripMenuItem_Click(Object sender, EventArgs e) 在 F:\实习\遥感和GIS课设\遥感课设\地物分类算法\C#\遥感影像地物分类\遥感影像地物分类\Form1.cs 中: 第 46 行 在 System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e) 在 System.Windows.Forms.ToolStripButton.OnClick(EventArgs e) 在 System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e) 在 System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e) 在 System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met) 在 System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met) 在 System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea) 在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.ScrollableControl.WndProc(Message& m) 在 System.Windows.Forms.ToolStrip.WndProc(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 在 System.Windows.Forms.Application.Run(Form mainForm) 在 遥感影像地物分类.Program.Main() 在 F:\实习\遥感和GIS课设\遥感课设\地物分类算法\C#\遥感影像地物分类\遥感影像地物分类\Program.cs 中: 第 19 行 内部异常 1: TypeInitializationException: “SWIGExceptionHelper”的类型初始值设定项引发异常。 内部异常 2: BadImageFormatException: 试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B) 是什么意思要怎么解决
最新发布
06-25
评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值