Code:
//version 1 //hook with detours
__int64 x=0;
unsigned char buffer[0xffffff];
//================================== ReadFile ===========================================
BOOL WINAPI Hook_ReadFile(HANDLE crt,LPVOID LpBuffer,DWORD buffLen,LPDWORD retLen,LPOVERLAPPED a)
{

    BOOL ret=Real_ReadFile(crt,LpBuffer,buffLen,retLen,a);
    if(!ret) return ret;
        
    memcpy(&buffer[x],LpBuffer,*retLen);

    x+=*retLen;

if(x>=0xffffff) MessageBox(0, "Buffer OverLoad", "Error",0);
return ret;
}
//-------------------------------------
I set bufferSize to 0xffffff,and it's ok for incoming data that are not greater than bufferSize,,,so I've made version 2.

Code:
//version 2
__int64 x=0;
unsigned char *buffer;
//@StartUp => buffer=(unsigned char*)GlobalAlloc(GPTR,1);
BOOL WINAPI Hook_ReadFile(HANDLE crt,LPVOID LpBuffer,DWORD buffLen,LPDWORD retLen,LPOVERLAPPED a)
{

    BOOL ret=Real_ReadFile(crt,LpBuffer,buffLen,retLen,a);
    if(!ret) return ret;
     
    buffer = (unsigned char*)GlobalReAlloc((HGLOBAL)buffer,x+*retLen+1,GMEM_MOVEABLE);
        
    memcpy(&buffer[x],LpBuffer,*retLen);
    x+=*retLen;

return ret;
}
//----------------------------------------------
No limit for bufferSize,it works on I/O operations,but when I try to grab data on serial port,the program crashes.
I tried to use fwite or WriteFile(Real_WriteFile) to save data to disk,but is still slow.
Even I've changed timeout(SetCommTimeouts),,,with no results.
----
does anybody have an idea how to solve it?
P.S: Compiler(Dev C++).