Beispiel #1
0
        /// <summary>
        /// Create a console on Windows...
        /// </summary>
        public static StreamReader CreateConsole()
        {
            // Make sure we have a console...
            if (TwainLocalScanner.GetPlatform() == TwainLocalScanner.Platform.WINDOWS)
            {
                // Get our console...
                NativeMethods.AllocConsole();

                // Only do this junk if running under Visual Studio, note that we'll
                // lose color.  So it goes...
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    var handle = NativeMethods.CreateFile
                                 (
                        "CONOUT$",
                        NativeMethods.DesiredAccess.GenericWrite | NativeMethods.DesiredAccess.GenericWrite,
                        NativeMethods.FileShare.Read | NativeMethods.FileShare.Write,
                        IntPtr.Zero,
                        NativeMethods.CreationDisposition.OpenExisting,
                        NativeMethods.FileAttributes.Normal,
                        IntPtr.Zero
                                 );
                    SafeFileHandle safefilehandle     = new SafeFileHandle(handle, true);
                    FileStream     fileStream         = new FileStream(safefilehandle, FileAccess.Write);
                    StreamWriter   streamwriterStdout = new StreamWriter(fileStream, Encoding.ASCII);
                    streamwriterStdout.AutoFlush = true;
                    Console.SetOut(streamwriterStdout);
                }
            }

            // And because life is hard, we need to up the size of standard input...
            StreamReader streamreaderConsole = new StreamReader(Console.OpenStandardInput(65536));

            return(streamreaderConsole);
        }
Beispiel #2
0
 /// <summary>
 /// Get the desktop windows for Windows systems...
 /// </summary>
 /// <returns></returns>
 public static IntPtr GetDesktopWindow()
 {
     // Get an hwnd...
     if (TwainLocalScanner.GetPlatform() == TwainLocalScanner.Platform.WINDOWS)
     {
         return(NativeMethods.GetDesktopWindow());
     }
     else
     {
         return(IntPtr.Zero);
     }
 }
Beispiel #3
0
        ///////////////////////////////////////////////////////////////////////////////
        // Public Methods...
        ///////////////////////////////////////////////////////////////////////////////
        #region Public Methods...

        /// <summary>
        /// Initialize interesting stuff...
        /// </summary>
        /// <param name="a_displaycallback">display callback function or null</param>
        /// <param name="a_stopnotification">notification that we've stopped monitoring</param>
        /// <param name="m_confirmscan">confirmation function or null</param>
        /// <param name="a_blNoDevices">true if we have no devices</param>
        public Scanner
        (
            TwainLocalScanner.DisplayCallback a_displaycallback,
            StopNotification a_stopnotification,
            TwainLocalScanner.ConfirmScan a_confirmscan,
            float a_fConfirmScanScale,
            out bool a_blNoDevices
        )
        {
            bool   blUseSane;
            string szExecutablePath = null;
            string szReadFolder     = null;
            string szWriteFolder    = null;

            // Init stuff...
            a_blNoDevices       = true;
            m_displaycallback   = a_displaycallback;
            m_stopnotification  = a_stopnotification;
            m_confirmscan       = a_confirmscan;
            m_fConfirmScanScale = a_fConfirmScanScale;

            // Get the config and command line argument values...
            szExecutablePath = Config.Get("executablePath", "");
            szReadFolder     = Config.Get("readFolder", "");
            szWriteFolder    = Config.Get("writeFolder", "");
            blUseSane        = (Config.Get("usesane", null) != null);

            // Sanity check...
            if (!FindTwainDirectOnTwain(szExecutablePath, blUseSane))
            {
                if (blUseSane)
                {
                    Log.Error("Unable to locate TwainDirect.OnSane.exe");
                    throw new Exception("Unable to locate TwainDirect.OnSane.exe");
                }
                else
                {
                    Log.Error("Unable to locate TwainDirect.OnTwain.exe");
                    throw new Exception("Unable to locate TwainDirect.OnTwain.exe");
                }
            }

            // Get our TWAIN Local interface...
            m_twainlocalscanner = new TwainLocalScanner(a_confirmscan, a_fConfirmScanScale, null, null, Display, true);
            if (m_twainlocalscanner == null)
            {
                Log.Error("Failed to create TwainLocalScanner");
                throw new Exception("Failed to create TwainLocalScanner");
            }

            // Do we have any devices?
            a_blNoDevices = RefreshDeviceList();
        }
Beispiel #4
0
        /// <summary>
        /// Create a console on Windows...
        /// </summary>
        public static StreamReader CreateConsole()
        {
            // Make sure we have a console...
            if (TwainLocalScanner.GetPlatform() == TwainLocalScanner.Platform.WINDOWS)
            {
                // Get our console...
                NativeMethods.AllocConsole();

                // We have to do some additional work to get out text in the console instead
                // of having it redirected to Visual Studio's output window...
                IntPtr         stdHandle          = NativeMethods.GetStdHandle(NativeMethods.STD_OUTPUT_HANDLE);
                SafeFileHandle safefilehandle     = new SafeFileHandle(stdHandle, true);
                FileStream     fileStream         = new FileStream(safefilehandle, FileAccess.Write);
                Encoding       encoding           = System.Text.Encoding.GetEncoding(Encoding.Default.CodePage);
                StreamWriter   streamwriterStdout = new StreamWriter(fileStream, encoding);
                streamwriterStdout.AutoFlush = true;
                Console.SetOut(streamwriterStdout);
            }

            // And because life is hard, we need to up the size of standard input...
            StreamReader streamreaderConsole = new StreamReader(Console.OpenStandardInput(65536));

            return(streamreaderConsole);
        }
Beispiel #5
0
        ///////////////////////////////////////////////////////////////////////////////
        // Public Methods...
        ///////////////////////////////////////////////////////////////////////////////
        #region Public Methods...

        /// <summary>
        /// Init stuff...
        /// </summary>
        /// <param name="a_dnssddeviceinfo">the device we're talking to</param>
        /// <param name="a_twainlocalscanner">our interface to the scanner</param>
        public FormSetup(Dnssd.DnssdDeviceInfo a_dnssddeviceinfo, TwainLocalScanner a_twainlocalscanner)
        {
            float  fScale;
            string szWriteFolder;

            // Init stuff...
            InitializeComponent();
            m_dnssddeviceinfo = a_dnssddeviceinfo;

            // Find our write folder...
            szWriteFolder = Config.Get("writeFolder", "");

            // Handle scaling...
            fScale = (float)Config.Get("scale", 1.0);
            if (fScale <= 1)
            {
                fScale = 1;
            }
            else if (fScale > 2)
            {
                fScale = 2;
            }
            if (fScale != 1)
            {
                this.Font = new Font(this.Font.FontFamily, this.Font.Size * fScale, this.Font.Style);
            }

            // Localize...
            string szCurrentUiCulture = "." + Thread.CurrentThread.CurrentUICulture.ToString();

            if (szCurrentUiCulture == ".en-US")
            {
                szCurrentUiCulture = "";
            }
            try
            {
                m_resourcemanager = new ResourceManager("TwainDirect.App.WinFormStrings" + szCurrentUiCulture, typeof(FormSelect).Assembly);
            }
            catch
            {
                m_resourcemanager = new ResourceManager("TwainDirect.App.WinFormStrings", typeof(FormSelect).Assembly);
            }
            m_labelSelectDestinationFolder.Text = m_resourcemanager.GetString("strLabelSelectImageDestination");
            this.Text = m_resourcemanager.GetString("strFormSetupTitle");

            // More init stuff...
            m_twainlocalscanner = a_twainlocalscanner;
            this.FormClosing   += new FormClosingEventHandler(FormSetup_FormClosing);

            // Location of current task...
            m_szCurrentTaskFile = Path.Combine(szWriteFolder, "currenttask");

            // We're putting the tasks into the write folder...
            m_szTasksFolder = Path.Combine(szWriteFolder, "tasks");
            if (!Directory.Exists(m_szTasksFolder))
            {
                try
                {
                    Directory.CreateDirectory(m_szTasksFolder);
                }
                catch (Exception exception)
                {
                    Log.Error("Can't create folder <" + m_szTasksFolder + ">, so using current folder - " + exception.Message);
                    m_szTasksFolder = Directory.GetCurrentDirectory();
                }
            }

            // Restore values...
            m_textboxFolder.Text        = RestoreFolder();
            m_textboxUseUiSettings.Text = "";
            if (File.Exists(m_szCurrentTaskFile))
            {
                m_textboxUseUiSettings.Text = File.ReadAllText(m_szCurrentTaskFile);
            }
        }
Beispiel #6
0
        /// <summary>
        /// Find TWAINDirect-on-TWAIN, we're assuming something about the
        /// construction of the kit to make this work.  Needless to
        /// say, this could be done better.
        ///
        /// We're assuming something like this:
        /// C:\Users\user\Desktop\SWORD on TWAIN Kit\Device Proxy\bin\Debug\thing.exe
        ///
        /// We want to get to this:
        ///  C:\Users\user\Desktop\SWORD on TWAIN Kit\...\TwainDirectOnTwain.exe
        ///
        /// We'd also like to honor the debug/release, if possible.
        /// </summary>
        /// <returns>true if we find it</returns>
        internal bool FindTwainDirectOnTwain(string a_szExecutablePath, bool a_blUseSane)
        {
            // Just in case...
            try
            {
                // Find SWORD-on-TWAIN...
                string szTwainDirectOn;
                string szDataFolder = a_szExecutablePath;
                string szName       = Path.GetFileNameWithoutExtension(szDataFolder);
                string szConfiguration;
                if (a_blUseSane)
                {
                    szTwainDirectOn = "TwainDirectOnSane";
                }
                else
                {
                    szTwainDirectOn = "TwainDirectOnTwain";
                }
                if (szDataFolder.Contains("Debug"))
                {
                    szConfiguration = "Debug";
                }
                else
                {
                    szConfiguration = "Release";
                }
                string szPlatform;
                if (szDataFolder.Contains("AnyCPU"))
                {
                    szPlatform = "AnyCPU";
                }
                else if (IntPtr.Size > 4) // szDataFolder.Contains("x64"))
                {
                    if (TwainLocalScanner.GetPlatform() == TwainLocalScanner.Platform.WINDOWS)
                    {
                        szPlatform = "x64";
                    }
                    else
                    {
                        szPlatform = "x86";
                    }
                }
                else
                {
                    szPlatform = "x86";
                }
                m_szTwainDirectOn = szDataFolder.Split(new string[] { szName }, StringSplitOptions.None)[0];
                m_szTwainDirectOn = Path.Combine(m_szTwainDirectOn, szTwainDirectOn);
                m_szTwainDirectOn = Path.Combine(m_szTwainDirectOn, "bin");
                m_szTwainDirectOn = Path.Combine(m_szTwainDirectOn, szPlatform);
                m_szTwainDirectOn = Path.Combine(m_szTwainDirectOn, szConfiguration);
                m_szTwainDirectOn = Path.Combine(m_szTwainDirectOn, szTwainDirectOn + ".exe");
                Log.Info("Using: " + m_szTwainDirectOn);

                // Validate...
                if (!File.Exists(m_szTwainDirectOn))
                {
                    Log.Error("Failed to find: " + m_szTwainDirectOn);
                    return(false);
                }

                // We're good...
                return(true);
            }
            catch
            {
                // Nothing to do, just bail...
            }

            // Uh-oh...
            m_szTwainDirectOn = null;
            return(false);
        }
Beispiel #7
0
        /// <summary>
        /// Find TWAINDirect-on-TWAIN, we're assuming something about the
        /// construction of the kit to make this work.  Needless to
        /// say, this could be done better.
        ///
        /// We're assuming something like this:
        /// C:\Users\user\Desktop\SWORD on TWAIN Kit\Device Proxy\bin\Debug\thing.exe
        ///
        /// We want to get to this:
        ///  C:\Users\user\Desktop\SWORD on TWAIN Kit\...\TwainDirect.OnTwain.exe
        ///
        /// We'd also like to honor the debug/release, if possible.
        /// </summary>
        /// <returns>true if we find it</returns>
        internal bool FindTwainDirectOnTwain(string a_szExecutablePath, bool a_blUseSane)
        {
            // Just in case...
            try
            {
                // Find our bridge...
                string szTwainDirectOn;
                string szDataFolder = a_szExecutablePath;
                string szName       = Path.GetFileNameWithoutExtension(szDataFolder);
                string szConfiguration;
                if (a_blUseSane)
                {
                    szTwainDirectOn = "TwainDirect.OnSane";
                }
                else
                {
                    szTwainDirectOn = "TwainDirect.OnTwain";
                }

                // Try to load TwainDirect.OnTwain from the same execution folder first
                var localOnTwainApplication = Path.Combine(Path.GetDirectoryName(szDataFolder), szTwainDirectOn + ".exe");
                if (File.Exists(localOnTwainApplication))
                {
                    m_szTwainDirectOn = localOnTwainApplication;
                    return(true);
                }

                // Otherwise, assume we are in DEV environment
                // TODO: simplify this, overcomplicated

                if (szDataFolder.Contains("Debug"))
                {
                    szConfiguration = "Debug";
                }
                else
                {
                    szConfiguration = "Release";
                }
                string szPlatform;
                if (szDataFolder.Contains("AnyCPU"))
                {
                    szPlatform = "AnyCPU";
                }
                else if (IntPtr.Size > 4) // szDataFolder.Contains("x64"))
                {
                    if (TwainLocalScanner.GetPlatform() == TwainLocalScanner.Platform.WINDOWS)
                    {
                        szPlatform = "x64";
                    }
                    else
                    {
                        szPlatform = "x86";
                    }
                }
                else
                {
                    szPlatform = "x86";
                }
                m_szTwainDirectOn = szDataFolder.Split(new string[] { szName }, StringSplitOptions.None)[0];
                m_szTwainDirectOn = Path.Combine(m_szTwainDirectOn, szTwainDirectOn);
                m_szTwainDirectOn = Path.Combine(m_szTwainDirectOn, "bin");
                m_szTwainDirectOn = Path.Combine(m_szTwainDirectOn, szPlatform);
                m_szTwainDirectOn = Path.Combine(m_szTwainDirectOn, szConfiguration);
                m_szTwainDirectOn = Path.Combine(m_szTwainDirectOn, szTwainDirectOn + ".exe");
                Log.Info("Using: " + m_szTwainDirectOn);

                // Validate...
                if (!File.Exists(m_szTwainDirectOn))
                {
                    Log.Error("Failed to find: " + m_szTwainDirectOn);
                    return(false);
                }

                // We're good...
                return(true);
            }
            catch
            {
                // Nothing to do, just bail...
            }

            // Uh-oh...
            m_szTwainDirectOn = null;
            return(false);
        }
Beispiel #8
0
        static void Main(string[] a_aszArgs)
        {
            string   szExecutableName;
            string   szWriteFolder;
            float    fScale;
            FormMain form1;

            // Are we already running?
            Process[] aprocess = Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location));
            foreach (Process process in aprocess)
            {
                // If it ain't us, it's somebody else...
                if (process.Id != Process.GetCurrentProcess().Id)
                {
                    MessageBox.Show("This program is already running.  If you don't see it on the screen, check the system tray for the TWAIN Direct icon, and right click on it for the list of options.", "TWAIN Direct on TWAIN Bridge");
                    Environment.Exit(1);
                }
            }

            // Load our configuration information and our arguments,
            // so that we can access them from anywhere in the code...
            if (!Config.Load(Application.ExecutablePath, a_aszArgs, "appdata.txt"))
            {
                MessageBox.Show("Error starting.  Try uninstalling and reinstalling this software.", "Error");
                Environment.Exit(1);
            }

            // Set up our data folders...
            szWriteFolder    = Config.Get("writeFolder", "");
            szExecutableName = Config.Get("executableName", "");

            // Turn on logging...
            Log.Open(szExecutableName, szWriteFolder, 1);
            Log.SetLevel((int)Config.Get("logLevel", 0));
            Log.Info(szExecutableName + " Log Started...");

            // Make sure that any stale TwainDirectOnTwain processes are gone...
            foreach (Process processTwainDirectOnTwain in Process.GetProcessesByName("TwainDirect.OnTwain"))
            {
                try
                {
                    processTwainDirectOnTwain.Kill();
                }
                catch (Exception exception)
                {
                    Log.Error("unable to kill TwainDirect.OnTwain - " + exception.Message);
                }
            }

            // Figure out what we're doing...
            string szCommand;
            Mode   mode = SelectMode(out szCommand, out fScale);

            // Pick our command...
            switch (mode)
            {
            // Uh-oh...
            default:
                Log.Error("Unrecognized mode: " + mode);
                break;

            case Mode.SERVICE:
                //Service service = new Service();
                //ServiceBase.Run(service);
                break;

            case Mode.TERMINAL:
                if (TwainLocalScanner.GetPlatform() == TwainLocalScanner.Platform.WINDOWS)
                {
                    Interpreter.CreateConsole();
                }
                Terminal terminal = new TwainDirect.Scanner.Terminal();
                switch (szCommand.ToLower())
                {
                default:
                    Log.Error("Unrecognized command: " + szCommand);
                    break;

                case "register":
                    terminal.Register();
                    break;

                case "start":
                    terminal.Start();
                    break;
                }
                terminal.Dispose();
                break;

            // Fire up our application window...
            case Mode.WINDOW:
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                form1 = new FormMain();
                Application.Run(form1);
                form1.Dispose();
                break;
            }


            // All done...
            Log.Info(szExecutableName + " Log Ended...");
            Log.Close();
            Environment.Exit(0);
        }
Beispiel #9
0
        static void Main(string[] a_aszArgs)
        {
            string szExecutableName;
            string szWriteFolder;
            float  fScale;

            // Load our configuration information and our arguments,
            // so that we can access them from anywhere in the code...
            if (!Config.Load(Application.ExecutablePath, a_aszArgs, "appdata.txt"))
            {
                MessageBox.Show("Error starting, is appdata.txt damaged?");
                Environment.Exit(1);
            }

            // Set up our data folders...
            szWriteFolder    = Config.Get("writeFolder", "");
            szExecutableName = Config.Get("executableName", "");

            // Turn on logging...
            Log.Open(szExecutableName, szWriteFolder, 1);
            Log.SetLevel((int)Config.Get("logLevel", 0));
            Log.Info(szExecutableName + " Log Started...");

            // Figure out what we're doing...
            string szCommand;
            Mode   mode = SelectMode(out szCommand, out fScale);

            // Pick our command...
            switch (mode)
            {
            // Uh-oh...
            default:
                Log.Error("Unrecognized mode: " + mode);
                break;

            case Mode.SERVICE:
                //Service service = new Service();
                //ServiceBase.Run(service);
                break;

            case Mode.TERMINAL:
                if (TwainLocalScanner.GetPlatform() == TwainLocalScanner.Platform.WINDOWS)
                {
                    Interpreter.CreateConsole();
                }
                Terminal terminal = new TwainDirect.Scanner.Terminal();
                switch (szCommand.ToLower())
                {
                default:
                    Log.Error("Unrecognized command: " + szCommand);
                    break;

                case "register":
                    terminal.Register();
                    break;

                case "start":
                    terminal.Start();
                    break;
                }
                break;

            // Fire up our application window...
            case Mode.WINDOW:
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
                break;
            }


            // All done...
            Log.Info(szExecutableName + " Log Ended...");
            Log.Close();
            Environment.Exit(0);
        }
Beispiel #10
0
        static void Main(string[] a_aszArgs)
        {
            string szExecutableName;
            string szWriteFolder;
            float  fScale;
            Form1  form1;

            // Are we already running?
            Process[] aprocessTwainDirectScanner = Process.GetProcessesByName("TwainDirectScanner");
            if ((aprocessTwainDirectScanner != null) && (aprocessTwainDirectScanner.Length > 1))
            {
                //tbd it would be nice to send a message to the running program
                // so that it can make itself visible...
                Environment.Exit(1);
            }

            // Load our configuration information and our arguments,
            // so that we can access them from anywhere in the code...
            if (!Config.Load(Application.ExecutablePath, a_aszArgs, "appdata.txt"))
            {
                MessageBox.Show("Error starting, is appdata.txt damaged?");
                Environment.Exit(1);
            }

            // Set up our data folders...
            szWriteFolder    = Config.Get("writeFolder", "");
            szExecutableName = Config.Get("executableName", "");

            // Turn on logging...
            Log.Open(szExecutableName, szWriteFolder, 1);
            Log.SetLevel((int)Config.Get("logLevel", 0));
            Log.Info(szExecutableName + " Log Started...");

            // Make sure that any stale TwainDirectOnTwain processes are gone...
            foreach (Process processTwainDirectOnTwain in Process.GetProcessesByName("TwainDirectOnTwain"))
            {
                try
                {
                    processTwainDirectOnTwain.Kill();
                }
                catch (Exception exception)
                {
                    Log.Error("unable to kill TwainDirectOnTwain - " + exception.Message);
                }
            }

            // Figure out what we're doing...
            string szCommand;
            Mode   mode = SelectMode(out szCommand, out fScale);

            // Pick our command...
            switch (mode)
            {
            // Uh-oh...
            default:
                Log.Error("Unrecognized mode: " + mode);
                break;

            case Mode.SERVICE:
                //Service service = new Service();
                //ServiceBase.Run(service);
                break;

            case Mode.TERMINAL:
                if (TwainLocalScanner.GetPlatform() == TwainLocalScanner.Platform.WINDOWS)
                {
                    Interpreter.CreateConsole();
                }
                Terminal terminal = new TwainDirect.Scanner.Terminal();
                switch (szCommand.ToLower())
                {
                default:
                    Log.Error("Unrecognized command: " + szCommand);
                    break;

                case "register":
                    terminal.Register();
                    break;

                case "start":
                    terminal.Start();
                    break;
                }
                terminal.Dispose();
                break;

            // Fire up our application window...
            case Mode.WINDOW:
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                form1 = new Form1();
                Application.Run(form1);
                form1.Dispose();
                break;
            }


            // All done...
            Log.Info(szExecutableName + " Log Ended...");
            Log.Close();
            Environment.Exit(0);
        }