简体   繁体   中英

How does this code work? [HARD]

LINK: http://www.codeproject.com/KB/dotnet/twaindotnet.aspx

I'm trying to create a wrapper class for this open source .NET implementation of TWAIN and I'm having trouble understand how it actually gets the image.

I've downloaded the source code and in the GUI there is a button called Acquire. When I click this button to go to it's event handler I find this code which I assume gets the image:

private void menuItemScan_Click(object sender, System.EventArgs e)
{
    if (!msgfilter)
    {
        this.Enabled = false;
        msgfilter = true;
        Application.AddMessageFilter(this);
    }
    tw.Acquire();
}

If I follow the Acquire() method to see it's contents, I see this:

public void Acquire()
{
    TwRC rc;
    CloseSrc();
    if (appid.Id == IntPtr.Zero)
    {
        Init(hwnd);
        if (appid.Id == IntPtr.Zero)
            return;
    }
    rc = DSMident(appid, IntPtr.Zero, TwDG.Control, TwDAT.Identity, TwMSG.OpenDS, srcds);
    if (rc != TwRC.Success)
        return;

    TwCapability cap = new TwCapability(TwCap.XferCount, 1);
    rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, cap);
    if (rc != TwRC.Success)
    {
        CloseSrc();
        return;
    }

    TwUserInterface guif = new TwUserInterface();
    guif.ShowUI = 1;
    guif.ModalUI = 1;
    guif.ParentHand = hwnd;
    rc = DSuserif(appid, srcds, TwDG.Control, TwDAT.UserInterface, TwMSG.EnableDS, guif);
    if (rc != TwRC.Success)
    {
        CloseSrc();
        return;
    }
}

What I don't understand is how a method with a 'void' return type can actually have a return statement. Also, where is it acquiring and returning an image?

Can anyone help out?

I'm trying to create a useful wrapper and open source it, because as it stands there is no easy drag and drop solution for scanning images in C#.

Thanks for the help!

Edit: Thanks for the help regarding early returns. TIL! Now I'm curious about how the application gets the images to display on the form.

Any guidance?

"Void" means it returns nothing, not that it doesn't return. So the return statement just terminates the function and returns to the caller

For your other question, there are a few other relevant stack overflow questions

The DSCap line is seeing if there are multiple images. The capture happens as part of the call to DSuserif

Infact, you set a message filter on your form by calling Application.AddMessageFilter(this) method. So, you have to listen to scanner events and when you get a TwainCommand.TransferReady event, you will call TransferPictures() to get the image collection.

The method simply returns void to avoid other code segments being executed. That is completely legal. The method is not acquiring an image it only prepares the hardware and UI that is acquiring the image, i'd say.

return; causes the control flow to exit the function.

Had a look at the library. It seems that Acquire() just causes the driver to perform an acquire, and TransferPictures() is called to retrieve the pictures (that one returns an ArrayList , so yes it is returning something).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM