function ScannerWorker(Context:PSCANNER_THREAD_CONTEXT): DWORD;
var
notification: PSCANNER_NOTIFICATION;
replyMessage: SCANNER_REPLY_MESSAGE;
msg: PSCANNER_MESSAGE;
pOvlp:POVERLAPPED;
bResult: BOOL;
outSize: DWORD;
hr: HRESULT;
key: DWORD;
begin
While True
Do
begin
bResult := GetQueuedCompletionStatus(Context^.Completion,OutSize,Key,pOvlp,INFINITE);
Msg := PSCANNER_MESSAGE(@PSCANNER_MESSAGE(pOvlp)^.Ovlp);
If Not(bResult)
Then
begin
HR := HRESULT_FROM_WIN32(GetLastError());
Break;
end;
WriteLN(Format('
Received message: %d',[pOvlp.InternalHigh]));
notification := @msg.Notification;
assert(notification.BytesToScan<=SCANNER_READ_BUFFER_SIZE);
// result := ScanBuffer( notification^.Contents, notification.BytesToScan );
replyMessage.ReplyHeader.Status:= 0;
replyMessage.ReplyHeader.MessageId:= msg.MessageHeader.MessageId;
replyMessage.Reply.SafeToOpen:=not bResult;
WriteLN(Format('
Replying message, SafeToOpen: %d',[ReplyMessage.Reply.SafeToOpen]));
HR := FilterReplyMessage(Context^.Port,PFILTER_REPLY_HEADER(@ReplyMessage),SizeOf(ReplyMessage));
If SUCCEEDED(HR)
Then
begin
WriteLN('
Replied message');
end
Else
begin
WriteLN(('
Scanner: Error replying message. Error = 0x'+inttoHex(HR,2)));
//[b]EXCEPTION:0x800703E6[/b]
Break;
end;
FillChar(Msg^.FOverlapped,0,SizeOf(OVERLAPPED));
//#define FIELD_OFFSET(t, f) ((unsigned int)(unsigned long)&(((t *)0)->f))
HR := FilterGetMessage(Context^.Port,@Msg^.MessageHeader,DWORD(@PSCANNER_MESSAGE(0)^.FOverlapped),@Msg^.FOverlapped);
If HR <> HRESULT_FROM_WIN32(ERROR_IO_PENDING)
Then Break;
end;
If Not(SUCCEEDED(HR))
Then
begin
If HR = HRESULT_FROM_WIN32(ERROR_INVALID_HANDLE)
Then
begin
WriteLN('
Scanner: Port is disconnected, probably due to scanner filter unloading.');
end
Else
begin
WriteLN(Format('
Scanner: Unknown error occured. Error = 0x%x',<hr>));
end;
end;
FreeMem(Msg);
Result := HR;
end;