Code after orignial API-Call does not get executed

Sep 17, 2009 at 2:37 PM

I worte a hooking for the ReadFile W32Api call and had to notice that the code after the original api call never gets executed. How can I avoid this?

        static bool ReadFile_Hooked(
            IntPtr hFile,
            IntPtr lpBuffer,
            uint nNumberOfBytesToRead,
            out uint lpNumberOfBytesRead,
            [In] IntPtr lpOverlapped)
        {
            try
            {
                Main This = (Main)HookRuntimeInfo.Callback;
                lock (This.Queue)
                {
                    This.Queue.Push("Pre ReadFile");
                }
            }
            catch{}

            uint NumberOfBytesRead = 0;
            byte[] bytes = new byte[nNumberOfBytesToRead];

            bool ReturnValue = ReadFile(hFile, bytes, nNumberOfBytesToRead, out NumberOfBytesRead, lpOverlapped);
            //Everything after that does not get executed
            try
            {
                Main This = (Main)HookRuntimeInfo.Callback;
                lock (This.Queue)
                {
                    This.Queue.Push("Post ReadFile");
                }
            }
            catch { }

            try
            {
                Main This = (Main)HookRuntimeInfo.Callback;

                try
                {
                    string output = "ReadFile [" + RemoteHooking.GetCurrentProcessId() + ":" +
                        RemoteHooking.GetCurrentThreadId() + "] DataSize: " + nNumberOfBytesToRead + " Data: ";
                    try
                    {
                        for (uint i = 0; i < nNumberOfBytesToRead; i++)
                        {
                            output += string.Format("{1:X2} ", i, bytes[i]);
                        }
                    }
                    catch (Exception ExtInfo)
                    {
                        This.Interface.ReportException(ExtInfo);
                    }

                    lock (This.Queue)
                    {
                        This.Queue.Push(output);
                    }
                }
                catch (Exception ExtInfo)
                {
                    This.Interface.ReportException(ExtInfo);
                }
            }
            catch { }

            for (uint i = 0; i < nNumberOfBytesToRead; i++)
                Marshal.WriteByte(lpBuffer, (int)i, bytes[i]);

            lpNumberOfBytesRead = NumberOfBytesRead;
            return ReturnValue;
        }*/

Sep 26, 2009 at 5:03 PM
Edited Sep 26, 2009 at 5:04 PM

I've solved the problem. The solution can be found at: http://3d.benjamin-thaut.de