I am trying to create & send IOCTL request to remote target synchronously. I am getting the status as success but the output buffer is empty.
Please find the attached code that I am using for the same. I have been struck with this issues for last 3-4 days and I dont know what I am missing. Any help or direction to debug would be great.
Notes:
- I am opening the target in PnP callback routine
- I am new to Windows driver development
##Opening the target
devExt = GetDeviceExtension(Device);
DbgPrint("Opened %wZ\n", devExt->symbolicName);
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&attributes, TARGET_DEVICE_INFO);
status = WdfIoTargetCreate(devExt->WdfDevice, &attributes, &ioTarget);
if (!NT_SUCCESS(status)) {
DbgPrint("WdfIoTargetCreate failed 0x%x\n", status);
return status;
}
targetDeviceInfo = GetTargetDeviceInfo(ioTarget);
targetDeviceInfo->DeviceExtension = devExt;
WDF_IO_TARGET_OPEN_PARAMS_INIT_OPEN_BY_NAME( &openParams,
SymbolicLink,
FILE_WRITE_ACCESS | FILE_READ_ACCESS);
openParams.ShareAccess = FILE_SHARE_WRITE | FILE_SHARE_READ;
openParams.EvtIoTargetQueryRemove = EvtIoTargetQueryRemove;
openParams.EvtIoTargetRemoveCanceled = EvtIoTargetRemoveCanceled;
openParams.EvtIoTargetRemoveComplete = EvtIoTargetRemoveComplete;
status = WdfIoTargetOpen(ioTarget, &openParams);
if (!NT_SUCCESS(status)) {
WdfObjectDelete(ioTarget);
DbgPrint("SST Target open failed\n");
return status;
}
*Target = ioTarget;
devExt->TargetDevice = ioTarget;
DbgPrint("Target Device 0x%p, PDO 0x%p, Fileobject 0x%p, Filehandle 0x%p\n",
WdfIoTargetWdmGetTargetDeviceObject(ioTarget),
WdfIoTargetWdmGetTargetPhysicalDevice(ioTarget),
WdfIoTargetWdmGetTargetFileObject(ioTarget),
WdfIoTargetWdmGetTargetFileHandle(ioTarget));
WDF_OBJECT_ATTRIBUTES_INIT(&attributes);
attributes.ParentObject = ioTarget;
status = WdfRequestCreate(&attributes,
ioTarget,
&targetDeviceInfo->IOCTLRequest);
if (!NT_SUCCESS(status)) {
WdfObjectDelete(ioTarget);
return status;
}
Sending the request
WDF_OBJECT_ATTRIBUTES attributes;
TARGET_DEVICE_INFO *targetInfo = GetTargetDeviceInfo(devExt->TargetDevice);
WDFMEMORY ipMemory, opMemory;
PVOID ipAddr, opAddr;
WDF_MEMORY_DESCRIPTOR ipMemDescp, opMemDescp;
status = WdfMemoryCreate(&attributes, NonPagedPoolNx, '4LeM',
sizeof(hdr) + sizeof(hsk), &ipMemory, &ipAddr);
status = WdfMemoryCreate(&attributes, NonPagedPoolNx, '5LeM',
sizeof(hdr) + sizeof(hsk), &opMemory, &opAddr);
status |= WdfMemoryCopyFromBuffer(ipMemory, 0, &hdr, sizeof(hdr)); //Filling the input data
status |= WdfMemoryCopyFromBuffer(ipMemory, sizeof(hdr), &hsk, sizeof(hsk));//Filling the input data
if (!NT_SUCCESS(status)) {
DbgPrint("Mem copy failed\n");
WdfObjectDelete(ipMemory);
WdfObjectDelete(opMemory);
return status;
}
WDF_MEMORY_DESCRIPTOR_INIT_HANDLE(&ipMemDescp, ipMemory, 0);
WDF_MEMORY_DESCRIPTOR_INIT_HANDLE(&opMemDescp, opMemory, 0);
WDF_REQUEST_SEND_OPTIONS options;
WDF_REQUEST_SEND_OPTIONS_INIT(&options, WDF_REQUEST_SEND_OPTION_SYNCHRONOUS);
WDF_REQUEST_SEND_OPTIONS_SET_TIMEOUT(&options, WDF_REL_TIMEOUT_IN_MS(1000));
status = WdfIoTargetFormatRequestForIoctl(devExt->TargetDevice, targetInfo->IOCTLRequest, IOCTL_CODE, ipMemory,
NULL, opMemory, NULL);
DbgPrint("Format status 0x%x\n", status);
if (NT_SUCCESS(status)) {
WdfRequestSetCompletionRoutine(targetInfo->IOCTLRequest,
IoctlRequestCompletionRoutine,
targetInfo);
BOOLEAN ret = WdfRequestSend(targetInfo->IOCTLRequest, devExt->TargetDevice,
&options);
if (!ret) {
status = WdfRequestGetStatus(targetInfo->IOCTLRequest);
DbgPrint("Request status 0x%x\n", status);
}
}
##Completion Routine
targetInfo = GetTargetDeviceInfo(Target);
status = WdfRequestGetStatus(Request);
DbgPrint("Get Request status 0x%x\n", status);
DbgPrint("Ioctl buffer 0x%p\n", CompletionParams->Parameters.Ioctl.Output.Buffer);
DbgPrint("Control code 0x%x\n", CompletionParams->Parameters.Ioctl.IoControlCode);
DbgPrint("Ioctl buffer length 0x%d\n", CompletionParams->Parameters.Ioctl.Output.Length);