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
        /// <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 #4
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 #5
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 #6
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 #7
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 #8
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);
        }