C# System.Runtime.InteropServices.SEHException (0x80004005): 外部组件抛出异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12589604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:49:35  来源:igfitidea点击:

System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception

c#

提问by Khayralla

I have code written in C# on a Windows7 and VS2010, 32 bit using .Net 3.5 and it is runing without any problem, now I converted it to 64 bit using .Net 4.0 but I got an error when my code call CloseHandle.

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

ERROR - CloseHandle
System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.

错误 - CloseHandle
System.Runtime.InteropServices.SEHException (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);

Any help is appreciated.

任何帮助表示赞赏。

采纳答案by Peter Ritchie

You can't use CloseHandlewith a pointer to user memory. You need to allocate a HANDLEin order to close it. What do you expect CloseHandleto close?

您不能使用CloseHandle指向用户内存的指针。您需要分配 aHANDLE才能关闭它。您希望CloseHandle关闭什么?