Example #1
0
        private IEnumerable <ScanDevice> GetFullDeviceList()
        {
            var twainImpl = ScanProfile?.TwainImpl ?? TwainImpl.Default;

            if (UseWorker)
            {
                using (var worker = workerServiceFactory.Create())
                {
                    return(worker.Service.TwainGetDeviceList(twainImpl));
                }
            }
            return(twainWrapper.GetDeviceList(twainImpl));
        }
Example #2
0
        /// <summary>
        /// Sends an email described by the given message object.
        /// </summary>
        /// <param name="message">The object describing the email message.</param>
        /// <param name="progressCallback"></param>
        /// <param name="cancelToken"></param>
        /// <returns>Returns true if the message was sent, false if the user aborted.</returns>
        public async Task <bool> SendEmail(EmailMessage message, ProgressHandler progressCallback, CancellationToken cancelToken)
        {
            return(await Task.Factory.StartNew(() =>
            {
                MapiSendMailReturnCode returnCode;

                if (UseWorker)
                {
                    using (var worker = workerServiceFactory.Create())
                    {
                        returnCode = worker.Service.SendMapiEmail(message);
                    }
                }
                else
                {
                    returnCode = mapiWrapper.SendEmail(message);
                }

                // Process the result
                if (returnCode == MapiSendMailReturnCode.UserAbort)
                {
                    return false;
                }

                if (returnCode != MapiSendMailReturnCode.Success)
                {
                    Log.Error("Error sending email. MAPI error code: {0}", returnCode);
                    errorOutput.DisplayError(MiscResources.EmailError, $"MAPI returned error code: {returnCode}");
                    return false;
                }

                return true;
            }));
        }
Example #3
0
        /// <summary>
        /// Sends an email described by the given message object.
        /// </summary>
        /// <param name="message">The object describing the email message.</param>
        /// <param name="progressCallback"></param>
        /// <param name="cancelToken"></param>
        /// <returns>Returns true if the message was sent, false if the user aborted.</returns>
        public async Task <bool> SendEmail(EmailMessage message, ProgressHandler progressCallback, CancellationToken cancelToken)
        {
            return(await Task.Factory.StartNew(() =>
            {
                MapiSendMailReturnCode returnCode;

                if (UseWorker && !mapiWrapper.CanLoadClient)
                {
                    using (var worker = workerServiceFactory.Create())
                    {
                        returnCode = worker.Service.SendMapiEmail(message);
                    }
                }
                else
                {
                    returnCode = mapiWrapper.SendEmail(message);
                    // It's difficult to get 32/64 bit right when using mapi32.dll. This can help sometimes.
                    // https://docs.microsoft.com/en-us/office/client-developer/outlook/mapi/building-mapi-applications-on-32-bit-and-64-bit-platforms
                    if (UseWorker && returnCode == MapiSendMailReturnCode.Failure)
                    {
                        using (var worker = workerServiceFactory.Create())
                        {
                            returnCode = worker.Service.SendMapiEmail(message);
                        }
                    }
                }

                // Process the result
                if (returnCode == MapiSendMailReturnCode.UserAbort)
                {
                    return false;
                }

                if (returnCode != MapiSendMailReturnCode.Success)
                {
                    Log.Error("Error sending email. MAPI error code: {0}", returnCode);
                    errorOutput.DisplayError(MiscResources.EmailError, $"MAPI returned error code: {returnCode}");
                    return false;
                }

                return true;
            }));
        }
Example #4
0
 private WiaItem GetItem(WiaDevice device)
 {
     if (ScanProfile.UseNativeUI)
     {
         var  hwnd      = Invoker.Current.InvokeGet(() => DialogParent.Handle);
         bool useWorker = Environment.Is64BitProcess && device.Version == WiaVersion.Wia10;
         if (useWorker)
         {
             WiaConfiguration config;
             using (var worker = workerServiceFactory.Create())
             {
                 config = worker.Service.Wia10NativeUI(device.Id(), hwnd);
             }
             var item = device.FindSubItem(config.ItemName);
             device.Properties.DeserializeEditable(device.Properties.Delta(config.DeviceProps));
             item.Properties.DeserializeEditable(item.Properties.Delta(config.ItemProps));
             return(item);
         }
         else
         {
             return(device.PromptToConfigure(hwnd));
         }
     }
     else if (device.Version == WiaVersion.Wia10)
     {
         // In WIA 1.0, the root device only has a single child, "Scan"
         // https://docs.microsoft.com/en-us/windows-hardware/drivers/image/wia-scanner-tree
         return(device.GetSubItems().First());
     }
     else
     {
         // In WIA 2.0, the root device may have multiple children, i.e. "Flatbed" and "Feeder"
         // https://docs.microsoft.com/en-us/windows-hardware/drivers/image/non-duplex-capable-document-feeder
         // The "Feeder" child may also have a pair of children (for front/back sides with duplex)
         // https://docs.microsoft.com/en-us/windows-hardware/drivers/image/simple-duplex-capable-document-feeder
         var items             = device.GetSubItems();
         var preferredItemName = ScanProfile.PaperSource == ScanSource.Glass ? "Flatbed" : "Feeder";
         return(items.FirstOrDefault(x => x.Name() == preferredItemName) ?? items.First());
     }
 }
Example #5
0
        /// <summary>
        /// Sends an email described by the given message object.
        /// </summary>
        /// <param name="message">The object describing the email message.</param>
        /// <param name="progressCallback"></param>
        /// <param name="cancelToken"></param>
        /// <returns>Returns true if the message was sent, false if the user aborted.</returns>
        public async Task <bool> SendEmail(EmailMessage message, ProgressHandler progressCallback, CancellationToken cancelToken)
        {
            var configuredClientName = userConfigManager.Config.EmailSetup?.SystemProviderName;

            MapiSendMailReturnCode EmailInProc(string clientName) => mapIWrapper.SendEmail(clientName, message);

            MapiSendMailReturnCode EmailByWorker(string clientName)
            {
                using (var worker = workerServiceFactory.Create())
                {
                    return(worker.Service.SendMapiEmail(clientName, message));
                }
            }

            return(await Task.Factory.StartNew(() =>
            {
                // It's difficult to get 32/64 bit right when using mapi32.dll:
                // https://docs.microsoft.com/en-us/office/client-developer/outlook/mapi/building-mapi-applications-on-32-bit-and-64-bit-platforms
                // Also some people have had issues with bad DLL paths (?), so we can fall back to mapi32.dll.

                var emailFunctions = new List <Func <MapiSendMailReturnCode> >();
                if (configuredClientName != null)
                {
                    if (mapIWrapper.CanLoadClient(configuredClientName))
                    {
                        emailFunctions.Add(() => EmailInProc(configuredClientName));
                    }
                    if (UseWorker)
                    {
                        emailFunctions.Add(() => EmailByWorker(configuredClientName));
                    }
                }
                if (mapIWrapper.CanLoadClient(null))
                {
                    emailFunctions.Add(() => EmailInProc(null));
                }
                if (UseWorker)
                {
                    emailFunctions.Add(() => EmailByWorker(null));
                }

                var returnCode = MapiSendMailReturnCode.Failure;
                foreach (var func in emailFunctions)
                {
                    returnCode = func();
                    if (returnCode != MapiSendMailReturnCode.Failure)
                    {
                        break;
                    }
                }

                // Process the result
                if (returnCode == MapiSendMailReturnCode.UserAbort)
                {
                    return false;
                }

                if (returnCode == MapiSendMailReturnCode.Success)
                {
                    return true;
                }
                Log.Error("Error sending email. MAPI error code: {0}", returnCode);
                errorOutput.DisplayError(MiscResources.EmailError, $"MAPI returned error code: {returnCode}");
                return false;
            }));
        }