Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# System.Runtime.InteropServices.SehexException(0x80004005):外部组件引发了异常_C# - Fatal编程技术网

C# System.Runtime.InteropServices.SehexException(0x80004005):外部组件引发了异常

C# System.Runtime.InteropServices.SehexException(0x80004005):外部组件引发了异常,c#,C#,我有在Windows7和VS2010上用C#编写的代码,32位使用.Net 3.5,运行起来没有任何问题,现在我用.Net 4.0将其转换为64位,但我的代码调用CloseHandle时出错 错误-关闭句柄 System.Runtime.InteropServices.SehexException(0x80004005):外部组件引发了异常 unsafe { fixed (byte* p = readBuffer) { IntPtr intPtr = (IntP

我有在Windows7和VS2010上用C#编写的代码,32位使用.Net 3.5,运行起来没有任何问题,现在我用.Net 4.0将其转换为64位,但我的代码调用CloseHandle时出错

错误-关闭句柄
System.Runtime.InteropServices.SehexException(0x80004005):外部组件引发了异常

unsafe
{
    fixed (byte* p = readBuffer)
    {
         IntPtr intPtr = (IntPtr)p;
         this.ReadFile(ref sourceFile, (ulong)buffer_size, intPtr, ref nNumBytesRead);

         if (intPtr != IntPtr.Zero)
         {
             try
             {
                 FunctionSqudf.CloseHandle(intPtr);
             }
             catch (Exception ex)
             {
                  Hardware.LogHardware.Error("CloseHandle", ex);
             }
         }
      }
  }


    [SecuritySafeCritical]
    [DllImport("kernel32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    internal static extern bool CloseHandle(IntPtr hObject);

非常感谢您的帮助。

您不能将
CloseHandle
与指向用户内存的指针一起使用。您需要分配一个
句柄来关闭它。您希望关闭什么?

您使用的是缓冲区指针而不是句柄调用CloseHandle()。这段代码以前不可能正常工作,显然,在64位模式下运行时,您现在会被提醒。Peter Ritchie ReadFile将从媒体[SecuritySafeCritical][DllImport(dllPath,CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall,ThrowOnUnmappableChar=true]读取文件部分)内部静态外部UdfResult ReadFile(ref StructSqudf.SQFile openFsObject,[In]ulong numBytesToBeRead,IntPtr buffer,ref ulong numBytesRead);Hans Passant,但是之前catch块中没有错误?@Khayralla请编辑您的问题,将您在注释中添加的ReadFile声明包括在内。CloseHandle接受“打开对象的有效句柄”。这将是一个指针。真正的问题是这个.ReadFile返回的是什么@如果它是一个指向作为内存块写入的内存的指针,那么它就不是一个有效的句柄。是的,手柄在技术上是指针;但是一个指向内核内存的指针,应用程序无法写入。请更正您的答案,我将删除-1。您的语句“您不能将CloseHandle与指针一起使用。”不正确。@Tripsed您无论如何都不能分配内核内存。。。但我已经澄清了答案…Peter,静态外部bool CloseHandle(IntPtr hObject);那么代码是正确的,ReadFile会向对象返回有效的intPtr吗?