TWAIN Working Group

Newsletter Signup
Donate
Help keep TWAIN free
  • About TWAIN
    • What’s New?
    • News
    • Events
    • Membership
    • Consider a Donation
    • Contact Us
  • Why TWAIN?
  • Developers
    • Driver Developer
    • Application Developer
    • TWAIN Features
    • Specification & Tools
    • Self Certification Process
  • Support Forums
  • Scanner End-User
  • Find Certified Drivers
    • Facebook
    • LinkedIn
    • Vimeo

Failure Message from DSMident API

Forums › TWAIN Classic › Failure Message from DSMident API

  • This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • May 29, 2017 at 5:33 am #30060 Reply
    Guru Prasad
    • Topics - 102
    • Replies - 197
    • Total Posts - 299

    When we use the following code with Fujitsu scanner or any other scanner it works fine. But when we use it with HP scanner any model, the

    following line ” TwRC rc = NativeMethods.DSMident(m_Appid, IntPtr.Zero, TwDG.Control, TwDAT.Identity, TwMSG.OpenDS, m_Srcds);” in the code returns failure.

    While testing the code with a test tool using STAThread(windows forms application) for the main function produces success with HP scanners too but when we do not use STAThread(windows forms application) it throws failure but works for other scanners except HP any model.

     private void btnScan_Click(object sender, EventArgs e)
            {
                try
                {
                    // MessageBox.Show("Start scan on:" + m_ScannerName);
    
                    try
                    {
                        bool ShowUI = false;
                        bool isMultiScan = false;
                        List<string> scannedFiles = new List<string>();
    
                        ScanSourceInfo scanSourceList = new ScanSourceInfo();
    
                        //AquireImageInfo aquireImageList = new AquireImageInfo();
                        //List<string> scannedFiles = new List<string>();
                        string errorInScannedFile = string.Empty;
                        using (TwainScanner form = new TwainScanner(true))
                        {
                            form.CurrentScannerName = m_ScannerName;
                            form.DisplayScannerSettings = ShowUI;
                            form.IsMultiScan = isMultiScan;
                            form.ImageFolder = "c:\\scannedImages";
                            form.Opacity = 0;
                            form.TopMost = true;
                            NativeMethods.BringWindowToTop(form.Handle);
                            IntPtr flag = new IntPtr(-1);
                            NativeMethods.SetWindowPos(form.Handle, flag, 0, 0, 1, 1, SetWindowPosFlags.IgnoreResize);
                            try { form.ShowDialog(); } catch { }
                            scannedFiles = form.AcquiredImageFile;
                            errorInScannedFile = form.ScannerStatusMessage;
                            //previousScannerName = form.GetPreviousScannerSettings();
                        }
                        if (scannedFiles.Count == 0)
                        {
                            //response = Request.CreateResponse(HttpStatusCode.NoContent, "No Scanned Images");
                            //return response;
                        }
    
                    }
                    catch (Exception exp)
                    {
                        MessageBox.Show("Caught exception: " + exp.Message);
                    }
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error Not able to start WebScan Test. " + ex.Message);
                }
    
                MessageBox.Show("Finished scan, check logs for more information.");
            }
    
            public bool Acquire(short showGUI, short multiPage, bool isDuplex, short scanType) //
            {
                try
                {
                    CloseSrc();
                    if (m_Appid.Id == IntPtr.Zero)
                    {
                        Init(m_Hwnd);
                        if (m_Appid.Id == IntPtr.Zero)
                        {
                            MTICommon.LogEvent("MTITwain - Acquire - Application Id is empty", EventLogEntryType.Information);
                            return false;
                        }
                    }
    
                    TwRC rc = NativeMethods.DSMident(m_Appid, IntPtr.Zero, TwDG.Control, TwDAT.Identity, TwMSG.OpenDS, m_Srcds);
                    if (rc != TwRC.Success)
                    {
                        MTICommon.LogEvent("MTITwain - Acquire - DSMident Identity unexpected status. " + rc, EventLogEntryType.Information);
                        return false;
                    }
    
                    TwCapability capUnit = new TwCapability(TwCap.IUnits, 0);
                    rc = NativeMethods.DScap(m_Appid, m_Srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capUnit);
                    if (rc != TwRC.Success)
                    {
                        MTICommon.LogEvent("MTITwain - Acquire - DScap Capability unexpected status. " + rc, EventLogEntryType.Information);
                    }
    
                    //multiPage:(1 - Single page scanning, -1 - Multi page scanning)
                    TwCapability cap = new TwCapability(TwCap.XferCount, multiPage);
                    rc = NativeMethods.DScap(m_Appid, m_Srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, cap);
                    if (rc != TwRC.Success)
                    {
                        MTICommon.LogEvent("MTITwain - Acquire - Failed to set scanning type (Single or multi page)" + rc, EventLogEntryType.Information);
                        CloseSrc();
                        return false;
                    }
    
                    if (isDuplex)
                    {
                        // DUPLEX
                        TwCapability capDuplex = new TwCapability(TwCap.CAP_DUPLEXENABLED, 0, true);
                        rc = NativeMethods.DScap(m_Appid, m_Srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capDuplex);
                        if (rc != TwRC.Success)
                        {
                            MTICommon.LogEvent("MTITwain - Acquire - DScap Capability unexpected status. " + rc, EventLogEntryType.Information);
                            CloseSrc();
                            return false;
                        }
                    }
    
                    //MessageBox.Show(scanType.ToString());
                    if (scanType != -1)
                    {
                        //scanType: 0 = BW | 1 = Gray | 2 = Colour
                        TwCapability capPixelType = new TwCapability(TwCap.IPixelType, scanType, TwType.UInt16);
                        rc = NativeMethods.DScap(m_Appid, m_Srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capPixelType);
                        if (rc != TwRC.Success)
                        {
                            MTICommon.LogEvent("MTITwain - Acquire - DScap Capability unexpected status. " + rc, EventLogEntryType.Information);
                            CloseSrc();
                            return false;
                        }
                    }
    
                    TwUserInterface guif = new TwUserInterface();
                    //showGUI:(1 - Display GUI, 0 - none)
                    guif.ShowUI = showGUI;
                    guif.ModalUI = 1;
                    guif.ParentHand = m_Hwnd;
                    NativeMethods.SetForegroundWindow(m_Hwnd.ToInt32());
                    rc = NativeMethods.DSuserif(m_Appid, m_Srcds, TwDG.Control, TwDAT.UserInterface, TwMSG.EnableDS, guif);
                    if (rc != TwRC.Success)
                    {
                        m_Failed = true;
                        MTICommon.LogEvent("MTITwain - Acquire - DSuserif Capability unexpected status. " + rc, EventLogEntryType.Information);
                        CloseSrc();
                        return false;
                    }
    
                    return true;
                }
                catch (Exception ex)
                {
                    MTICommon.LogEvent("MTITwain - Acquire", ex, EventLogEntryType.Error);
                    return false;
                }
    
            }

    internal static class NativeMethods
    {
    const string entryPoint = “#1″;//”DSM_Entry”; //”#1″
    //const string api = “twain_32.dll”;
    const string api = “TWAINDSM.dll”;

    // —— DSM entry point DAT_ variants:
    [DllImport(api, EntryPoint = entryPoint)]
    internal static extern TwRC DSMparent([In, Out] TwIdentity origin, IntPtr zeroptr, TwDG dg, TwDAT dat, TwMSG msg, ref IntPtr refptr);

    [DllImport(api, EntryPoint = entryPoint)]
    internal static extern TwRC DSMident([In, Out] TwIdentity origin, IntPtr zeroptr, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwIdentity idds);

    [DllImport(api, EntryPoint = entryPoint)]
    internal static extern TwRC DSMstatus([In, Out] TwIdentity origin, IntPtr zeroptr, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwStatus dsmstat);

    // —— DSM entry point DAT_ variants to DS:
    [DllImport(api, EntryPoint = entryPoint)]
    internal static extern TwRC DSuserif([In, Out] TwIdentity origin, [In, Out] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, TwUserInterface guif);

    [DllImport(api, EntryPoint = entryPoint)]
    internal static extern TwRC DSevent([In, Out] TwIdentity origin, [In, Out] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, ref TwEvent evt);

    [DllImport(api, EntryPoint = entryPoint)]
    internal static extern TwRC DSstatus([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwStatus dsmstat);

    [DllImport(api, EntryPoint = entryPoint)]
    internal static extern TwRC DScap([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwCapability capa);

    [DllImport(api, EntryPoint = entryPoint)]
    internal static extern TwRC DSiinf([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwImageInfo imginf);

    [DllImport(api, EntryPoint = entryPoint)]
    internal static extern TwRC DSixfer([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, ref IntPtr hbitmap);

    [DllImport(api, EntryPoint = entryPoint)]
    internal static extern TwRC DSpxfer([In, Out] TwIdentity origin, [In] TwIdentity dest, TwDG dg, TwDAT dat, TwMSG msg, [In, Out] TwPendingXfers pxfr);

    [DllImport(“kernel32.dll”, ExactSpelling = true)]
    internal static extern IntPtr GlobalAlloc(uint flags, UIntPtr size);
    [DllImport(“kernel32.dll”, ExactSpelling = true)]
    internal static extern IntPtr GlobalLock(IntPtr handle);
    [DllImport(“kernel32.dll”, ExactSpelling = true)]
    internal static extern bool GlobalUnlock(IntPtr handle);
    [DllImport(“kernel32.dll”, ExactSpelling = true)]
    internal static extern IntPtr GlobalFree(IntPtr handle);

    [DllImport(“user32.dll”, ExactSpelling = true)]
    internal static extern int GetMessagePos();
    [DllImport(“user32.dll”, ExactSpelling = true)]
    internal static extern int GetMessageTime();
    [DllImport(“User32.dll”)]
    internal static extern Int32 SetForegroundWindow(int hWnd);
    [DllImport(“gdi32.dll”, ExactSpelling = true)]
    internal static extern int GetDeviceCaps(IntPtr hDC, int nIndex);

    [DllImport(“gdi32.dll”, CharSet = CharSet.Auto)]
    internal static extern IntPtr CreateDC(string szdriver, string szdevice, string szoutput, IntPtr devmode);

    [DllImport(“gdi32.dll”, ExactSpelling = true)]
    internal static extern bool DeleteDC(IntPtr hdc);
    }

    Please let us know if you need any further details.

    Thanks
    Guru Prasad MS

  • Author
    Posts
Viewing 1 post (of 1 total)
Reply To: Failure Message from DSMident API
Your information:




Quick Links

Service Providers
TWAIN Support Forums
Membership
Contact Us
Privacy Policy

Newsletter Signup

TWAIN Working Group Family

TWAIN Working Group
TWAIN Direct®
TWAIN Resources
TWAIN Certified Drivers
PDF/raster

  • Facebook
  • GitHub
  • LinkedIn
  • Vimeo

Recent Topics

  • TWAIN for dental imaging integration
  • PDF/R For who and where?
  • Making searchable PDF with PDF/R
  • Backward compatibility with PDF/A and traditional PDF
  • could not open the twain source. Make sure there is a valid source for your sca
  • Quarterly Newsletter
  • TWAIN Working Group Membership
  • Logo Usage
  • TWAIN License
  • Contact Us
Privacy Policy • Privacy Tools • Copyright © 2021 TWAIN Working Group • by iHwy, LLC • Log in

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.