简体   繁体   English

在 c# 中设置 out 参数时出现 NullReferenceException

[英]NullReferenceException when setting a out parameter in c#

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..我在 c# 中使用 easyhook 得到了一个不安全的程序集.. 这段代码波纹管运行良好,但总而言之,调用此方法时出现 NullReferenceException ..

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此类用于拦截某些 I/O 文件中的所有写入以进行某些操作

Any ideas See code..任何想法见代码..

OCCURS ONLY WHEN SQLCOMMAND executed仅在执行 SQLCOMMAND 时发生

at ....SystemHook.WriteFileHook(IntPtr hFile, Void* lpBuffer, UInt32 nNumberOfBytesToWrite, UInt32& lpNumberOfBytesWritten, NativeOverlapped& lpOverlapped)在 ....SystemHook.WriteFileHook(IntPtr hFile, Void* lpBuffer, UInt32 nNumberOfBytesToWrite, UInt32& lpNumberOfBytesWritten, NativeOverlapped& lpOverlapped)
at SNIWriteSyncOverAsync(SNI_ConnWrapper* , SNI_Packet* )在 SNIWriteSyncOverAsync(SNI_ConnWrapper* , SNI_Packet* )
at SNINativeMethodWrapper.SNIWritePacket(SafeHandle pConn, SafeHandle packet, Boolean sync)在 SNINativeMethodWrapper.SNIWritePacket(SafeHandle pConn, SafeHandle packet, Boolean sync)
at System.Data.SqlClient.TdsParserStateObject.SNIWritePacket(SNIHandle handle, SNIPacket packet, UInt32& sniError, Boolean canAccumulate, Boolean callerHasConnectionLock)在 System.Data.SqlClient.TdsParserStateObject.SNIWritePacket(SNIHandle 句柄,SNIPacket 数据包,UInt32& sniError,Boolean canAccumulate,Boolean callerHasConnectionLock)
at System.Data.SqlClient.TdsParserStateObject.WriteSni(Boolean canAccumulate)在 System.Data.SqlClient.TdsParserStateObject.WriteSni(Boolean canAccumulate)
at System.Data.SqlClient.TdsParserStateObject.WritePacket(Byte flushMode, Boolean canAccumulate)在 System.Data.SqlClient.TdsParserStateObject.WritePacket(Byte flushMode, Boolean canAccumulate)
at System.Data.SqlClient.TdsParserStateObject.ExecuteFlush()在 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)在 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)在 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)在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String 方法, TaskCompletionSource`1 完成, Int32 超时, Task& 任务, Boolean&usedCache, Boolean asyncWrite, Boolean inRetry)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)在 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)在 System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior 行为,String 方法)
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)在 System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior 行为)
at System.Data.Common.DbCommand.ExecuteReader()在 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!!!

    ... 
}

Justin Stenning was right in his comment :贾斯汀斯坦宁他的评论中是正确的:

Try uint* or ref uint instead to correctly match the native signature LPDWORD.尝试使用 uint* 或 ref uint 来正确匹配本地签名 LPDWORD。

I had the wrong signature.我有错误的签名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM