Home › Forums › Discussions › General › I’m newbie in kernel programming, TDI program please help!:(
- This topic has 9 replies, 2 voices, and was last updated 20 years, 3 months ago by rvd.
-
AuthorPosts
-
September 21, 2004 at 12:42 pm #4883
I have struggled with a sea of difficulties. Pass a error, other errors occur.
My TDI kernel socket driver (export driver) there are steps as following:
1. Create a endpoint connection (OK).
2. Create a tdi transport address handle (OK).
3. Set Event Handle for connection (OK).
4. Bind endpoint connection to transport address handle (OK)
(TdiBuildAssociateAddress).
5. Connect to Server (TdiBuildConnect OK).
6. Send data (TdiBuidSend OK).
7. Receive data (TdiBuidReceive OK).
8. Disconnect (TdiBuildDisconnect OK).
9. Close connection file object, address object, connection handle,
address handle OK.But only success in the firt time. After disconnecting in the first connection session, do new session again and error always happend at “ASSERT( KeGetCurrentIrql() == PASSIVE_LEVEL )” command line. If remove this line, error will happend at the first connection session after closing the connection. I post some my code follow, please spend your time to see and help me.
////////////////////////////////////////////////
////Set event handle function ////
__declspec(dllexport) NTSTATUS
SetEventHandler(PFILE_OBJECT FileObject,LONG EventType, PVOID EventHandler, PVOID EventContext)
{
NTSTATUS status;
KEVENT Event;
PIRP Irp;
PDEVICE_OBJECT DeviceObject;
IO_STATUS_BLOCK IoStatus;DeviceObject = IoGetRelatedDeviceObject(FileObject);
pIrp = TdiBuildInternalDeviceControlIrp(TDI_SET_EVENT_HANDLER, DeviceObject, FileObject, &Event, &IoStatus);
if (pIrp == 0)
{
DbgPrint(“VLSD: Set event handle fail.n”);
return STATUS_INSUFFICIENT_RESOURCES;
}do{
TdiBuildSetEventHandler(Irp, DeviceObject, FileObject, NULL, NULL, EventType, EventHandler, EventContext);DbgPrint(“VLSD: Set event handle OK.n”);
ASSERT( KeGetCurrentIrql() == PASSIVE_LEVEL );
KeInitializeEvent(&Event, NotificationEvent, FALSE);IoSetCompletionRoutine( Irp, // The IRP
VLSDSimpleTdiRequestComplete, // The completion routine
&Event, // The completion context
TRUE, // Invoke On Success
TRUE, // Invoke On Error
TRUE // Invoke On Cancel
);status = IoCallDriver(DeviceObject, Irp);
if(status == STATUS_PENDING)
{
ASSERT( KeGetCurrentIrql() == PASSIVE_LEVEL );
status = KeWaitForSingleObject(&Event, Executive,
KernelMode, FALSE, 0);
if(!NT_SUCCESS(status))
break;
}
else if(!NT_SUCCESS(status))
break;}while(0);
return status == STATUS_SUCCESS ? IoStatus.Status : status;
}///// Receiving event handle function /////
__declspec(dllexport) NTSTATUS
EventReceive(PVOID eventContext,
CONNECTION_CONTEXT connectionContext,
ULONG Flags,
ULONG Indicated,
ULONG Available,
PULONG Taken,
PVOID tsduBuff,
PIRP *Irp)
{
DbgPrint(“VLSD: Receive Flags = %lx, Ind = %ld, Avl = %ld.n”,
Flags, Indicated, Available);//*Taken = Available; *Irp = 0;
return STATUS_SUCCESS;
}//////// Receiving data function /////////
__declspec(dllexport) NTSTATUS Recv(PFILE_OBJECT FileObject, PVOID Data, ULONG Length)
{
NTSTATUS status;
PIRP Irp;
PDEVICE_OBJECT DeviceObject;
PMDL Mdl;
KEVENT Event;
IO_STATUS_BLOCK IoStatus;ULONG tsize = 0;
ULONG len = 0;
PUCHAR rBuf;rBuf = (PUCHAR)ExAllocatePool(PagedPool, Length + 1);
RtlZeroMemory(rBuf, Length + 1);
while(tsize < Length)
{
PUCHAR buff;
ULONG i;
if(Length – tsize < 1024)
len = Length – tsize;
else
len = 1024;
buff = (PUCHAR)ExAllocatePool(PagedPool, len + 1);
RtlZeroMemory(buff, len + 1);ASSERT( KeGetCurrentIrql() == PASSIVE_LEVEL );
KeInitializeEvent(&Event, NotificationEvent,FALSE);DeviceObject = IoGetRelatedDeviceObject(FileObject);
Irp = TdiBuildInternalDeviceControlIrp(TDI_RECEIVE,
DeviceObject,
FileObject,
&Event, &IoStatus);
if (Irp == NULL)
{
DbgPrint(“VLSD: Receiving data fail because of insufficient MDL resources.n”);
status = STATUS_INSUFFICIENT_RESOURCES;
break;
}Mdl = IoAllocateMdl(buff, len, FALSE, FALSE, Irp);
if (Mdl == NULL)
{
DbgPrint(“VLSD: Receiving data fail because of insufficient MDL resources.n”);
status = STATUS_INSUFFICIENT_RESOURCES;
break;
}Mdl->Next = NULL;
__try{
MmProbeAndLockPages ( Mdl, KernelMode, IoModifyAccess );
} __except ( EXCEPTION_EXECUTE_HANDLER ) {
DbgPrint ( “VLSD: Exception at MmProbeAndLockPagesn” );
IoFreeMdl(Mdl);
Mdl = NULL;
status = STATUS_INSUFFICIENT_RESOURCES;
break;
}TdiBuildReceive(Irp, DeviceObject, FileObject, NULL, NULL, Mdl,
TDI_RECEIVE_NORMAL, len);DbgPrint ( “VLSD: Begin read data n” );
status = IoCallDriver(DeviceObject, Irp);
if ((status == STATUS_SUCCESS) || (status == STATUS_PENDING))
{
DbgPrint(“VLSD: Wait a moment for receiving data.n”);ASSERT( KeGetCurrentIrql() == PASSIVE_LEVEL );
status = KeWaitForSingleObject(&Event, Executive,
KernelMode, FALSE, NULL);
if(!NT_SUCCESS(status))
{
DbgPrint(“VLSD: Cannot wait, time out or error.n”);
}
}buff[len] = ‘