-1

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!!!

    ... 
}
  • Change to uint? The question mark will allow nulls. Then lpNumberOfBytesWritten = lpNumberOfBytesWritten == null ? null : 0; – jdweng Jan 12 '19 at 14:43
  • 1
    this scalar assignment cannot cause a (managed) NullReferenceException. post full exception details including call stack. – Cee McSharpface Jan 12 '19 at 14:45
  • 1
    You've got unsafe code here which means **all the usual safety systems are turned off**. You should be very happy that this is crashing when it is called incorrectly, rather than corrupting user data or CLR data structures; that means you can at least track down the problem. Start by looking at the call stack carefully. – Eric Lippert Jan 12 '19 at 14:52
  • Additionnal information, look like it occurs only when a sql command executed – Luc St-Pierre Jan 12 '19 at 15:18
  • now this could become interesting. I guess the sql command execution triggers the hook because you are connected to SQL Server via a named pipe? now you posted the call stack but still exception details are missing. full code of hook handler? which line in there? – Cee McSharpface Jan 12 '19 at 15:46
  • I'm connecting to local SQL serveur – Luc St-Pierre Jan 12 '19 at 16:27
  • Try uint* or ref uint instead to correctly match the native signature LPDWORD. – Justin Stenning Jan 12 '19 at 21:45
  • **thanks Justin!!! it works** – Luc St-Pierre Jan 13 '19 at 00:17

1 Answers1

0

Justin Stenning was right in his comment:

Try uint* or ref uint instead to correctly match the native signature LPDWORD.

I had the wrong signature.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92