Home › Forums › TWAIN Classic › Failure Message from DSMident API
- This topic is empty.
- AuthorPosts
- Guru Prasad
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 - AuthorPosts