【问题标题】:Where I did something wrong? error while execution (runtime error)我哪里做错了?执行时出错(运行时错误)
【发布时间】:2015-08-05 09:14:06
【问题描述】:

我做错了什么;我收到一个错误: 在这一行:

Buffer.BlockCopy((Array) numArray, icondirentry.ImageOffset, (Array)
                 iconFile.iconImage[index], 0, icondirentry.BytesInRes);

错误:数组的偏移量和长度超出范围,或者计数大于从索引到源集合末尾的元素数。

该方法的完整代码为:

 public static IconClass.IconFile FromFile(string filename)
      {
        IconClass.IconFile iconFile = new IconClass.IconFile();
        byte[] numArray = File.ReadAllBytes(filename);
        GCHandle gcHandle = GCHandle.Alloc((object) numArray, GCHandleType.Pinned);
        iconFile.iconDir = (IconClass.ICONDIR) Marshal.PtrToStructure(gcHandle.AddrOfPinnedObject(), typeof (IconClass.ICONDIR));
        iconFile.iconEntry = new IconClass.ICONDIRENTRY[checked ((int) iconFile.iconDir.Count - 1 )];
        iconFile.iconImage = new byte[checked ((int) iconFile.iconDir.Count - 1)][];
        int num1 = Marshal.SizeOf((object) iconFile.iconDir);
        Type type = typeof (IconClass.ICONDIRENTRY);
        int num2 = Marshal.SizeOf(type);
        int num3 = 0;
        int num4 = checked ((int) iconFile.iconDir.Count - 1);
        int index = num3;
        while (index <= num4)
        {
          IconClass.ICONDIRENTRY icondirentry = (IconClass.ICONDIRENTRY) Marshal.PtrToStructure(new IntPtr(checked (gcHandle.AddrOfPinnedObject().ToInt64() + (long) num1)), type);
          iconFile.iconEntry[index] = icondirentry;
          iconFile.iconImage[index] = new byte[checked (icondirentry.BytesInRes - 1 )];
          Buffer.BlockCopy((Array) numArray, icondirentry.ImageOffset, (Array) iconFile.iconImage[index], 0, icondirentry.BytesInRes);
          checked { num1 += num2; }
          checked { ++index; }
        }
        gcHandle.Free();
        return iconFile;
      }

【问题讨论】:

  • icondirentry.BytesInRes - 1 + 1,对不起,但是什么?
  • IconClass.IconFile 是什么?有大量我们不知道的相关代码......
  • @PakkuDon 你有什么建议?
  • icondirentry.BytesInRes - 1 + 1???? icondirentry.BytesInRes - 0 怎么样?大声笑...
  • @PakkuDon 和 Jon Skeet;抱歉,将代码放在发生错误的位置时有点错误,我现在才更改它,如果您可以帮助我,请

标签: c# winforms


【解决方案1】:
(Array) iconFile.iconImage[index]

检查 index 是否不大于 IconImage(数组、列表或其他)成员的大小。

【讨论】:

  • @Xorraxx “我的意图是更改 exe 文件的图标” - 你为什么要这样做?对我来说听起来有点可疑
【解决方案2】:

我已经解决了这个问题... 由 Niraj Doshi 提供... 谢谢 Niraj Doshi。

代码如下:

public static IconClass.IconFile FromFile(string filename)
  {
    IconClass.IconFile iconFile = new IconClass.IconFile();
    byte[] numArray = File.ReadAllBytes(filename);
    GCHandle gcHandle = GCHandle.Alloc((object) numArray, GCHandleType.Pinned);
    iconFile.iconDir = (IconClass.ICONDIR) Marshal.PtrToStructure(gcHandle.AddrOfPinnedObject(), typeof (IconClass.ICONDIR));
    iconFile.iconEntry = new IconClass.ICONDIRENTRY[checked ((int) iconFile.iconDir.Count + 1 )];
    iconFile.iconImage = new byte[checked ((int) iconFile.iconDir.Count + 1)][];
    int num1 = Marshal.SizeOf((object) iconFile.iconDir);
    Type type = typeof (IconClass.ICONDIRENTRY);
    int num2 = Marshal.SizeOf(type);
    int num3 = 0;
    int num4 = checked ((int) iconFile.iconDir.Count-1);
    int index = num3;
    while (index <= num4)
    {
      IconClass.ICONDIRENTRY icondirentry = (IconClass.ICONDIRENTRY) Marshal.PtrToStructure(new IntPtr(checked (gcHandle.AddrOfPinnedObject().ToInt64() + (long) num1)), type);
      iconFile.iconEntry[index] = icondirentry;
      iconFile.iconImage[index] = new byte[checked (icondirentry.BytesInRes + 1)];
      Buffer.BlockCopy((Array) numArray, icondirentry.ImageOffset, (Array) iconFile.iconImage[index], 0, icondirentry.BytesInRes);
      checked { num1 += num2; }
      checked { ++index; }
    }
    gcHandle.Free();
    return iconFile;
  }

问题在于传递给进程的索引。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 2015-03-21
    • 2011-02-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-28
    相关资源
    最近更新 更多