/// <summary> /// Select the mode for this session, batch or interactive, /// based on the arguments. Don't be weirded out by this /// function calling TWAIN Local On Twain and then that function using /// Sword. This is a static function, it's just a convenience /// to use the Sword object to hold it, it could go anywhere /// and later on probably will (like as a function inside of /// the Program module). /// </summary> /// <returns>true for batch mode, false for interactive mode</returns> public static bool SelectMode() { int iPid = 0; string szIpc; string szTaskFile; string szImagesFolder; bool blTestPdfRaster; string szTestDnssd; // Check the arguments... string szWriteFolder = Config.Get("writeFolder", null); string szExecutableName = Config.Get("executableName", null); szTaskFile = Config.Get("task", null); blTestPdfRaster = (Config.Get("testpdfraster", null) != null); szTestDnssd = Config.Get("testdnssd", null); szIpc = Config.Get("ipc", null); szImagesFolder = Config.Get("images", null); if (string.IsNullOrEmpty(szImagesFolder)) { szImagesFolder = Path.Combine(szWriteFolder, "images"); } iPid = int.Parse(Config.Get("parentpid", "0")); // Run in IPC mode. The caller has set up a 'pipe' for us, so we'll use // that to send commands back and forth. This is the normal mode when // we're running with a scanner... if (szIpc != null) { // With Windows we need a window for the driver, but we can hide it... if (TWAIN.GetPlatform() == TWAIN.Platform.WINDOWS) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new FormTwain(szWriteFolder, szImagesFolder, szIpc, iPid)); return(true); } // Linux and Mac OS X are okay without a window... else { TwainLocalOnTwain twainlocalontwain; // Create our object... twainlocalontwain = new TwainLocalOnTwain(szWriteFolder, szImagesFolder, szIpc, iPid, null, null, IntPtr.Zero); // Run our object... twainlocalontwain.Run(); // All done... return(true); } } // Handle the TWAIN list, we use this during registration to find out // what drivers we can use, and collect some info about them... string szTwainList = Config.Get("twainlist", null); if (szTwainList != null) { string szTwainListAction = Config.Get("twainlistaction", null); string szTwainListData = Config.Get("twainlistdata", null); if (szTwainList == "") { szTwainList = Path.Combine(Config.Get("writeFolder", ""), "twainlist.txt"); } System.IO.File.WriteAllText(szTwainList, ProcessSwordTask.TwainListDrivers(szTwainListAction, szTwainListData)); return(true); } /// Test DNS-SD... if (!string.IsNullOrEmpty(szTestDnssd)) { if (szTestDnssd == "monitor") { int ii; int jj; Dnssd dnssd; bool blServiceIsAvailable; Interpreter.CreateConsole(); List <Dnssd.DnssdDeviceInfo> ldnssddeviceinfo = new List <Dnssd.DnssdDeviceInfo>(); dnssd = new Dnssd(Dnssd.Reason.Monitor, out blServiceIsAvailable); if (blServiceIsAvailable) { dnssd.MonitorStart(null, IntPtr.Zero); for (ii = 0; ii < 60; ii++) { bool blUpdated = false; bool blNoMonitor = false; Thread.Sleep(1000); ldnssddeviceinfo = dnssd.GetSnapshot(ldnssddeviceinfo, out blUpdated, out blNoMonitor); if (blUpdated) { Console.Out.WriteLine(""); if ((ldnssddeviceinfo == null) || (ldnssddeviceinfo.Count == 0)) { Console.Out.WriteLine("***empty***"); } else { for (jj = 0; jj < ldnssddeviceinfo.Count; jj++) { Console.Out.WriteLine(ldnssddeviceinfo[jj].GetInterface() + " " + ldnssddeviceinfo[jj].GetServiceName()); } } } } dnssd.MonitorStop(); } dnssd.Dispose(); dnssd = null; } else if (szTestDnssd == "register") { Dnssd dnssd; bool blServiceIsAvailable; Interpreter.CreateConsole(); dnssd = new Dnssd(Dnssd.Reason.Register, out blServiceIsAvailable); if (blServiceIsAvailable) { dnssd.RegisterStart("Instance", 55556, "Ty", "", "", "", "Note"); Thread.Sleep(60000); dnssd.RegisterStop(); } dnssd.Dispose(); dnssd = null; } // All done... return(true); } // Otherwise let the user interact with us... TWAINWorkingGroup.Log.Info("Interactive mode..."); return(false); }
static void Main(string[] a_aszArgs) { string szExecutableName; string szWriteFolder; // Load our configuration information and our arguments, // so that we can access them from anywhere in the code... if (!Config.Load(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), a_aszArgs, "appdata.txt")) { Console.Out.WriteLine("Error starting. Try uninstalling and reinstalling this software."); Environment.Exit(1); } // Set up our data folders... szWriteFolder = Config.Get("writeFolder", ""); szExecutableName = Config.Get("executableName", ""); // Turn on logging... Log.Open(szExecutableName, szWriteFolder, (int)Config.Get("logLevel", 0)); Log.Info(szExecutableName + " Log Started..."); // Windows needs a window, we need our console and window // in different threads for full control... if (TWAIN.GetPlatform() == TWAIN.Platform.WINDOWS) { // Init our form... Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); FormMain formmain = new FormMain(); // Launch the terminal window, we're doing this in // a thread so we can have a console and a form. When // the thread is done there's no reason to be subtle, // we can exit the application... Terminal terminal = new twaincscert.Terminal(formmain); Thread threadTerminal = new Thread( new ThreadStart( delegate() { terminal.Run(); Environment.Exit(0); } ) ); threadTerminal.Start(); // Run our form, the exit above will kill it off... formmain.SetTerminal(terminal); Application.Run(formmain); } // Linux and Mac come here, life is much simpler for them... else { Terminal terminal = new twaincscert.Terminal(null); terminal.Run(); } // All done... Log.Info(szExecutableName + " Log Ended..."); Log.Close(); Environment.Exit(0); }