Hi,
apologies for maybe a noob question, but am just starting with Windows Driver programming.
I am trying to create an IOCTL and communicate with the driver from a user mode application. I am sending a string (char) to KM and printing it out in Windbg. In turn, KM will send another string back (char again) and I want to print it on a Mesage box or window text or anything.
The string I am sending from my user mode application is arriving fine and I can print it out in WinDbg.
However, from the UM application, I cannot see the recieved string in the DeviceIOControl function.
Here is my User Mode function PART:
HANDLE hDevice;
DWORD nb;
CString Windowmessage = L" ";
char texttosend[4] = “arg”; //I am sending this
CHAR pRecievedString[250]; //I am supposed to recieve in this
bool bResult = 0;
Windowmessage.Append(L"Sending meessage to Kernel Mode\r\n");
GetDlgItem(OutputTextboxec)->SetWindowTextW(Windowmessage);
Windowmessage.Append(L"Creating FileIO\r\n");
GetDlgItem(OutputTextboxec)->SetWindowTextW(Windowmessage);
hDevice = CreateFile(
TEXT("\\\\.\\DOSXXXDeviceName"), //File Name of File or IO device
GENERIC_READ | GENERIC_WRITE, //Desired Access
0, //share mode (0-prevent other processes from opening a file f they request delete/read/write access)
//FILE_SHARE_DELETE(0x00000004),FILE_SHARE_READ(0x00000001),FILE_SHARE_WRITE(0x00000002),
NULL, //SecurityAttributes
OPEN_EXISTING, //CreationDisposition:An action to take on a file or device that exists or does not exist.
FILE_ATTRIBUTE_NORMAL, // FlagsAndAttributes
NULL); // TemplateFile
Windowmessage.Append(L"Sending Control Command \r\n");
GetDlgItem(OutputTextboxec)->SetWindowTextW(Windowmessage);
ZeroMemory(pRecievedString, sizeof(pRecievedString));
bResult = DeviceIoControl(
hDevice, IOCTL_ADDEXT,
&texttosend, sizeof(texttosend),
pRecievedString, sizeof(pRecievedString) ,
&nb, NULL);
Windowmessage.Append(L"Add Ext Cmnd sent\r\n");
GetDlgItem(OutputTextboxec)->SetWindowTextW(Windowmessage);
_**// I tried a lot of formatting to output PRecievedString here but to no avail. %c outputs rubbish. %s creates a memory error//**_
/*wchar_t op[200] = {L'0'};
swprintf(op, sizeof(op), L"in: % %c %s \r\n ", pRecievedString, pRecievedString);
Windowmessage.Append(op);
GetDlgItem(OutputTextboxec)->SetWindowTextW(Windowmessage);*/
CloseHandle(hDevice);
}
And here is my Kernel Mode function
RegisterExtension_BufferedIO(PIRP Irp, PIO_STACK_LOCATION pIoStackIrp, ULONG_PTR dwDataWritten)
{
PAGED_CODE();
NTSTATUS status = STATUS_UNSUCCESSFUL;
PCHAR pOutputBuffer, pInputBuffer;
ULONG dwDataRead = 0;
PCHAR pReturnData = "Hello from Kernel Buffered IO";
UINT16 dwDataSize = sizeof("Hello from Kernel Buffered IO");
KdPrint(("->->->In Register Extension Buffered IO\r\n"));
pInputBuffer = Irp->AssociatedIrp.SystemBuffer;
pOutputBuffer = Irp->AssociatedIrp.SystemBuffer;
if (pInputBuffer && pOutputBuffer)
{
if (IsStringTerminated(pInputBuffer, pIoStackIrp->Parameters.DeviceIoControl.InputBufferLength, dwDataRead))
{
KdPrint((" Message from User Mode: %s \r\n", pInputBuffer));
KdPrint((" Output buffer length: %i >= %i:DataSize to Send to UM? \r\n ", pIoStackIrp->Parameters.DeviceIoControl.OutputBufferLength, dwDataSize));
if (pIoStackIrp->Parameters.DeviceIoControl.OutputBufferLength >= dwDataSize)
{
RtlCopyMemory(pOutputBuffer, pReturnData, dwDataSize);
KdPrint(("Output Buffer = %s \r\n", pOutputBuffer)); _**//This prints fine but I do not see it in User Mode **_
dwDataWritten = dwDataSize;
status = STATUS_SUCCESS;
}
else
{
dwDataWritten = dwDataSize;
status = STATUS_BUFFER_TOO_SMALL;
}
}
}
return status;
}
I know that something is arriving back cause when I initiate pRecievedString, it prints out rubbish. However if I initiate it and do not call DeviceIOControl I see the initiated string in the TextWindow.
I hope it is just some stupid formatting issue.
thanks in advance for your help.
regards
Mario