Re: Compiler Produces Incorrect Code

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Matt Taylor (com.sisecure_at_mtaylor)
Date: 12/30/04


Date: Thu, 30 Dec 2004 12:02:18 GMT


"cristalink" <cristalink@nospam.nospam> wrote in message
news:OJqmpLg7EHA.3836@tk2msftngp13.phx.gbl...
> eax may well be used as a parameter due to compiler optimizations, esp.
> because MyControlInternal is static. Where's the code for
MyControlInternal?
[...]

A bit lengthy, but here it is:

static NTSTATUS MyIoControlInternal(PDEVICE_OBJECT DeviceObject,
                                    PFILE_OBJECT FileObject,
                                    ULONG IoControlCode,
                                    PVOID InputBuffer,
                                    ULONG InputBufferLength,
                                    PVOID OutputBuffer,
                                    ULONG OutputBufferLength,
                                    IO_STATUS_BLOCK *pISB)
{
 KEVENT ke;
 PIRP Irp;
 PIO_STACK_LOCATION IrpSp;
 PVOID Buffer, TempInputBuffer, TempOutputBuffer;
 NTSTATUS stat;

 // NOTE: This can never overflow
 if ((InputBufferLength + OutputBufferLength) > 0)
 {
  if ((Buffer = ExAllocatePool(NonPagedPool, InputBufferLength +
OutputBufferLength)) == NULL)
  {
   MyDebugLog("ExAllocatePoolWithTag failed\n");
   return STATUS_INSUFFICIENT_RESOURCES;
  }

  TempInputBuffer = Buffer;
  TempOutputBuffer = (PVOID)((size_t) TempInputBuffer + InputBufferLength);

  if (InputBufferLength > 0)
   RtlCopyMemory(TempInputBuffer, InputBuffer, InputBufferLength);
 }
 else
 {
  Buffer = NULL;
 }

 KeInitializeEvent(&ke, NotificationEvent, FALSE);

 Irp = IoBuildDeviceIoControlRequest(IoControlCode,
                                     DeviceObject,
                                     TempInputBuffer,
                                     InputBufferLength,
                                     TempOutputBuffer,
                                     OutputBufferLength,
                                     FALSE,
                                     &ke,
                                     pISB);

 if (Irp == NULL)
 {
  if (Buffer != NULL)
   ExFreePool(Buffer);

  MyDebugLog("IoBuildDeviceIoControlRequest failed\n");
  return STATUS_INSUFFICIENT_RESOURCES;
 }

 IrpSp = IoGetCurrentIrpStackLocation(Irp) - 1;
 IrpSp->FileObject = FileObject;

 // NOTE: seems the event is not getting set for certain IOCTLs.
IoCompletion seems to fix it.
 IoSetCompletionRoutine(Irp, MySetEventIoCompletion, &ke, TRUE, TRUE, TRUE);
 stat = IoCallDriver(DeviceObject, Irp);
 KeWaitForSingleObject(&ke, Executive, KernelMode, FALSE, NULL);
 IoFreeIrp(Irp);

 stat = pISB->Status;

 if (Buffer != NULL)
 {
  if (OutputBufferLength > 0)
   RtlCopyMemory(OutputBuffer, TempOutputBuffer, OutputBufferLength);

  if (Buffer != NULL)
   ExFreePool(Buffer);
 }

 return stat;
}

-Matt



Relevant Pages

  • Re: Compiler Produces Incorrect Code
    ... > PIRP Irp; ... > PVOID Buffer, TempInputBuffer, TempOutputBuffer; ... > RtlCopyMemory(TempInputBuffer, InputBuffer, InputBufferLength); ... > stat = IoCallDriver; ...
    (microsoft.public.development.device.drivers)
  • Re: Compiler Produces Incorrect Code
    ... an event passed to IoBuildDevicexxx will be set only if IoCallDriver ... stat = IoCallDriver; ... will free the IRP for you. ... > RtlCopyMemory(TempInputBuffer, InputBuffer, InputBufferLength); ...
    (microsoft.public.development.device.drivers)
  • RE: How to protect a file from deleting or cuting.
    ... IN PFILE_OBJECT FileObject, ... This routine rolls an IRP to query the name of the ... nameInfo = ExAllocatePoolWithTag(PagedPool, ... then our buffer is too small. ...
    (microsoft.public.development.device.drivers)
  • Re: Calllib troubles
    ... I believe VB is passing the address of the bufferptr element of buffer to 'PmcaGetData' but MATLAB is passing the address of a single temporary long value to the function which is then trying to copy in count values causing a crash. ... > Dim stat As Long ... > Dim count As Long ...
    (comp.soft-sys.matlab)
  • Re: How to set up a USB driver to read an Interrupt End Point
    ... Give a buffer that is large enough to hold the maximum expected data. ... I want to setup this pending IRP ...
    (microsoft.public.development.device.drivers)