算术运算导致溢出c#

Arithmetic operation resulted in an overflow c#

解锁文件时出现以下错误

Arithmetic operation resulted in an overflow

System.IntPtr.ToInt32

我怀疑是 pBuffer.ToInt32() 的下一行:

IntPtr iPtr = new IntPtr(pBuffer.ToInt32() + (i * Marshal.SizeOf(fi3)));

我自己无法重现错误,错误是没有显示正确的行号。我正在寻找一种方法来重现此问题或有关可能原因的任何反馈。谢谢

public void Close()
{
        const int MAX_PREFERRED_LENGTH = -1;
        int readEntries;
        int totalEntries;
        IntPtr pBuffer = IntPtr.Zero;
        FILE_INFO_3 fi3 = new FILE_INFO_3();
        int iStatus = NetFileEnum(this.HostName, this.HostPathToShare + this.FileNameFromShare, null, 3, ref pBuffer, MAX_PREFERRED_LENGTH, out readEntries, out totalEntries, pBuffer);
        if (iStatus == 0)
        {
            for (int i = 0; i < readEntries; i++)
            {
                IntPtr iPtr = new IntPtr(pBuffer.ToInt32() + (i * Marshal.SizeOf(fi3)));
                fi3 = (FILE_INFO_3)Marshal.PtrToStructure(iPtr, typeof(FILE_INFO_3));
                NetFileClose(this.HostName, fi3.fi3_id);
            }
        }
        NetApiBufferFree(pBuffer);
}

[DllImport("netapi32.dll", SetLastError=true, CharSet = CharSet.Unicode)]
static extern int NetFileClose(
    string servername,
    int id);

[DllImport("Netapi32.dll", SetLastError=true)]
static extern int NetApiBufferFree(IntPtr Buffer);

[DllImport("netapi32.dll", SetLastError=true, CharSet=CharSet.Unicode)]
static extern int NetFileEnum(
     string servername,
     string basepath,
     string username,
     int level,
     ref IntPtr bufptr,
     int prefmaxlen,
     out int entriesread,
     out int totalentries,
     IntPtr resume_handle
);

更新

我添加了 win32 api 代码。

下面的答案看起来是正确的,机器是 64 位的。但是我无法在开发服务器上重现它,尽管开发环境是 64 位的。有重现错误的想法吗?

我马上看到代码中有两个错误:

  1. 您将 pBuffer 设置为 0,但从未实际分配它。它 应该 将它传递给 NetFileEnum 时出错,尽管这是一个 Win32 API 函数,所以它可能不会注意到。

  2. 你转换 pBuffer .ToInt32(),它 应该 专门为 x86 编译时工作,但是如果你有 Any CPU 或 x64作为您的目标平台,这也会成为一个问题。

错误是由于您的代码 运行 在 64 位上下文中返回的指针地址超出了 32 位可寻址范围,因此 .ToInt32() 抛出。

调用Environment.Is64BitProcess检测你的进程是运行32位还是64位,并相应转换地址:

long pointerAddress;

if (Environment.Is64BitProcess)
{
    pointerAddress = pBuffer.ToInt64(); 
}
else
{
    pointerAddress = pBuffer.ToInt32(); 
}

var fileInfoPointer = new IntPtr(pointerAddress + (i * Marshal.SizeOf(fi3)));