I got a unsafe assembly using easyhook in c#.. this code bellows works well but sum time, I got a NullReferenceException when this method is called..
The instance of this class is a Singleton pattern It passe several time and one point it got a null reference
This class if for intercept all writing in some I/O file to proceed some operation
Any ideas See code..
OCCURS ONLY WHEN SQLCOMMAND executed
at ....SystemHook.WriteFileHook(IntPtr hFile, Void* lpBuffer, UInt32 nNumberOfBytesToWrite, UInt32& lpNumberOfBytesWritten, NativeOverlapped& lpOverlapped)
at SNIWriteSyncOverAsync(SNI_ConnWrapper* , SNI_Packet* )
at SNINativeMethodWrapper.SNIWritePacket(SafeHandle pConn, SafeHandle packet, Boolean sync)
at System.Data.SqlClient.TdsParserStateObject.SNIWritePacket(SNIHandle handle, SNIPacket packet, UInt32& sniError, Boolean canAccumulate, Boolean callerHasConnectionLock)
at System.Data.SqlClient.TdsParserStateObject.WriteSni(Boolean canAccumulate)
at System.Data.SqlClient.TdsParserStateObject.WritePacket(Byte flushMode, Boolean canAccumulate)
at System.Data.SqlClient.TdsParserStateObject.ExecuteFlush()
at System.Data.SqlClient.TdsParser.TdsExecuteSQLBatch(String text, Int32 timeout, SqlNotificationRequest notificationRequest, TdsParserStateObject stateObj, Boolean sync, Boolean callerHasConnectionLock, Byte[] enclavePackage)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader()
bool WriteFileHook(IntPtr hFile, void* lpBuffer, uint nNumberOfBytesToWrite, out uint lpNumberOfBytesWritten, [In] ref System.Threading.NativeOverlapped lpOverlapped)
{
bool results = false;
bool write = true;
lpNumberOfBytesWritten = 0; // CRASH!!!
...
}