views:

407

answers:

1

I have an ActiveX control in MFC that manipulates images and I am trying to add TWAIN scanning functionality to it.

I need to be able to receive a Windows Message back from the TWAIN driver that tells my control when an image has been scanned, so I have created a CDialog and I pass the HWND of the Dialog to the driver.

ALl the sample code I have seen on the net then uses PreTranslateMessage to capture the message from TWAIN, but in my ActiveX control this method is never being called.

Does anyone know how I can get the messages for that Dialog? I have also tried using PeekMessage with no success.

Many Thanks

+1  A: 

You don't need to create a CDialog. You just need any window to process the messages. Anything dealing with TWAIN is best handled in its own thread. So, create a new thread for MFC (via CWinThread or AfxBeginThread). In that thread, create a CWnd. The HWND of this CWnd is the one you will pass with all the calls to the DSM, etc. Each thread has its own message queue, so set one up in there. Communicate with that thread via PostMessage, SendMessage, PostThreadMessage, etc. Assuming you post a message MY_SPECIAL_MESSAGE to signal to being acquiring an image, your message loop should look something like this:

MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
    if (msg.message == MY_SPECIAL_MESSAGE)
    { 
     GetImageFromTWAIN();
    }
    else if (!ProcessTWAINMessage(&msg)) {
     TranslateMessage(&msg); 
     DispatchMessage(&msg); 
    }
}

Definitely look at the source code in the TWAIN development kit to see how this all works in detail. TWAIN is a tricky creature.

Trust me, this is the best approach. You can do it all in a single thread using your main thread's message queue, but it's to be avoided.

adzm
Spike0xff
Yes, this exact method is currently in production at over a thousand medical institutions, some of which scan hundreds of images a day.
adzm
(and has been in use for several years)
adzm