5

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.

Khayralla
  • 177
  • 1
  • 4
  • 15
  • 1
    You are calling CloseHandle() with a buffer pointer, not a handle. This code could never have worked correctly before, apparently you are being reminded about it now when running in 64-bit mode. – Hans Passant Sep 25 '12 at 19:25
  • Peter Ritchie ReadFile will read a file portion from a media [SecuritySafeCritical] [DllImport(dllPath, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall, ThrowOnUnmappableChar = true)] internal static extern UdfResult ReadFile( ref StructSqudf.SQFile openFsObject, [In] ulong numBytesToBeRead, IntPtr buffer, ref ulong numBytesRead); – Khayralla Sep 25 '12 at 19:30
  • Hans Passant, but there was no error in the catch block before ? – Khayralla Sep 25 '12 at 19:36
  • @Khayralla Please edit your question to include the ReadFile declaration you put in the comment. – Trisped Sep 25 '12 at 19:37

1 Answers1

4

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?

Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98
  • CloseHandle takes a "valid handle to an open object." which would be a pointer. The real question is what this.ReadFile is returning. http://msdn.microsoft.com/en-us/library/windows/desktop/ms724211(v=vs.85).aspx http://www.pinvoke.net/default.aspx/kernel32.closehandle – Trisped Sep 25 '12 at 19:35
  • 2
    @Trisped It's not going to be a valid handle if it's a pointer to memory being written to as a block of memory. Yes a HANDLE is *technically* a pointer; but a pointer to kernel memory, which an app can't write to. – Peter Ritchie Sep 25 '12 at 19:41
  • Correct your answer and I will remove -1. Your statement "You can't use CloseHandle with a pointer." is incorrect. – Trisped Sep 25 '12 at 19:43
  • @Tripsed You can't allocate kernel memory anyway... but I've clarified the answer... – Peter Ritchie Sep 25 '12 at 19:47
  • Peter, static extern bool CloseHandle(IntPtr hObject); so the code is correct, ReadFile will return a valid intPtr to the object ? – Khayralla Sep 25 '12 at 19:47
  • @Khayralla but you're not using the return value from `ReadFile`, you're using the value in `readBuffer` – Peter Ritchie Sep 25 '12 at 19:49
  • @Peter, yes you are right, byte[] readBuffer = new byte[buffer_size] which is not updated in ReadFile. so there is no need to call CLoseHandle ? – Khayralla Sep 25 '12 at 20:02