Khayralla Khayralla - 2 months ago 66
C# Question

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

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.

ERROR - CloseHandle

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

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.

Answer

You can't use CloseHandle with a pointer to user memory. You need to allocate a HANDLE in order to close it. What do you expect CloseHandle to close?