Still keep getting failure on API writefile even the lastest release!

Feb 17, 2014 at 4:14 AM
dear spazzarama, thx a lot for responsing me!!


environment: vs2010 c#, windows 2008, .Net 4.0, complied with anycpu,x86,x64 seperately!

can inject x64 applications and seems work normally, but always causes the x86 apps cashed!

I have tried several P-Invoke implementations ,never seccessed


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
[UnmanagedFunctionPointer(CallingConvention.StdCall, SetLastError = true)]
delegate bool DWriteFile(
    IntPtr hFile,
    System.Text.StringBuilder lpBuffer,
    uint nNumberOfBytesToWrite,
    out uint lpNumberOfBytesWritten,
    [In] ref System.Threading.NativeOverlapped lpOverlapped);

[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
static extern bool WriteFile(
    IntPtr hFile,
    System.Text.StringBuilder lpBuffer,
    uint nNumberOfBytesToWrite,
    out uint lpNumberOfBytesWritten,
    [In] ref System.Threading.NativeOverlapped lpOverlapped);

WriteFileHook = LocalHook.Create(LocalHook.GetProcAddress("kernel32.dll", "WriteFile"), new DWriteFile(WriteFile_Hooked), this);
WriteFileHook.ThreadACL.SetExclusiveACL(new Int32[1]);


static bool WriteFile_Hooked(
    IntPtr hFile,
    System.Text.StringBuilder lpBuffer,
    uint nNumberOfBytesToWrite,
    out uint lpNumberOfBytesWritten,
    [In] ref System.Threading.NativeOverlapped lpOverlapped)
{
    return WriteFile(
        hFile,
        lpBuffer,
        nNumberOfBytesToWrite,
        out lpNumberOfBytesWritten,
        ref lpOverlapped);
}
Coordinator
Feb 18, 2014 at 7:12 AM
I tested the code you posted using the x64 and x86 builds and it worked without any issues.

Have you tried with that cut-down version? Or do you have other code within your WriteFile_Hooked method?