WPF form crashes on Windows 10 when trying to acess Outlook adress book

0

I haven't been able to figure this one out by myself so I thought I'd give you a shot at it.

The program itself is comparing a list of employees to a PDL from the Outlook adress book. The issue is that my WPF form works perfectly fine on Windows 7, however the program crashes on Windows 10 when I try to access the Outlook adress book, everything else works the way it's supposed to.

This is the code to access Outlook:

    public class Outlookhelper
    {

    public List<String> GetDistributionListMembers()
    {
        List <String> returnlist = new List <String>();
        Outlook.Application application = new Outlook.Application();
        Outlook.SelectNamesDialog snd =
            application.Session.GetSelectNamesDialog();
        Outlook.AddressLists addrLists =
            application.Session.AddressLists;
        foreach (Outlook.AddressList addrList in addrLists) 
        {
            if (addrList.Name == "Global Adress List")
            {
                snd.InitialAddressList = addrList;
                break;
            }
        }
        snd.NumberOfRecipientSelectors =
            Outlook.OlRecipientSelectors.olShowTo;
        snd.ToLabel = "D/L";
        snd.ShowOnlyInitialAddressList = true;
        snd.AllowMultipleSelection = false;
        snd.Display();
        if (snd.Recipients.Count > 0)
        {
            Outlook.AddressEntry addrEntry =
                snd.Recipients[1].AddressEntry;
            if (addrEntry.AddressEntryUserType ==
                Outlook.OlAddressEntryUserType.
                olExchangeDistributionListAddressEntry)
            {
                Outlook.ExchangeDistributionList exchDL =
                    addrEntry.GetExchangeDistributionList();
                Outlook.AddressEntries addrEntries =
                    exchDL.GetExchangeDistributionListMembers();
                if (addrEntries != null)
                    foreach (Outlook.AddressEntry exchDLMember
                        in addrEntries)
                    {
                        //System.Windows.Forms.MessageBox.Show(exchDLMember.Name);
                        returnlist.Add(exchDLMember.Name);
                    }
            }
        }
    return returnlist;
    }
}

And this is the error message I get from the Event Viewer on the Windows 10 machine:

Application: PDL Updater.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Runtime.InteropServices.COMException
   at



System.Runtime.Remoting.RemotingServices.AllocateUninitializedObject(System.RuntimeType)
   at System.Runtime.Remoting.Activation.ActivationServices.CreateInstance(System.RuntimeType)
   at System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(System.RuntimeType, System.Object[], Boolean)
   at System.RuntimeTypeHandle.CreateInstance(System.RuntimeType, Boolean, Boolean, Boolean ByRef, System.RuntimeMethodHandleInternal ByRef, Boolean ByRef)
   at System.RuntimeType.CreateInstanceSlow(Boolean, Boolean, Boolean, System.Threading.StackCrawlMark ByRef)
   at System.Activator.CreateInstance(System.Type, Boolean)
   at System.Activator.CreateInstance(System.Type)
   at PDLUpdater.Outlookhelper.GetDistributionListMembers()
   at PDLUpdater.MainWindow.getPDLnames_Click(System.Object, System.Windows.RoutedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(System.Object, System.Windows.RoutedEventArgs, Boolean)
   at System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject, System.Windows.RoutedEventArgs)
   at System.Windows.Controls.Primitives.ButtonBase.OnClick()
   at System.Windows.Controls.Button.OnClick()
   at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(System.Windows.Input.MouseButtonEventArgs)
   at System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate, System.Object)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(System.Object, System.Windows.RoutedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(System.Object, System.Windows.RoutedEventArgs, Boolean)
   at System.Windows.UIElement.ReRaiseEventAs(System.Windows.DependencyObject, System.Windows.RoutedEventArgs, System.Windows.RoutedEvent)
   at System.Windows.UIElement.OnMouseUpThunk(System.Object, System.Windows.Input.MouseButtonEventArgs)
   at System.Windows.RoutedEventArgs.InvokeHandler(System.Delegate, System.Object)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(System.Object, System.Windows.RoutedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(System.Object, System.Windows.RoutedEventArgs, Boolean)
   at System.Windows.UIElement.RaiseEventImpl(System.Windows.DependencyObject, System.Windows.RoutedEventArgs)
   at System.Windows.UIElement.RaiseTrustedEvent(System.Windows.RoutedEventArgs)
   at System.Windows.Input.InputManager.ProcessStagingArea()
   at System.Windows.Input.InputProviderSite.ReportInput(System.Windows.Input.InputReport)
   at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr, System.Windows.Input.InputMode, Int32, System.Windows.Input.RawMouseActions, Int32, Int32, Int32)
   at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr, MS.Internal.Interop.WindowMessage, IntPtr, IntPtr, Boolean ByRef)
   at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
   at MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSG ByRef)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame)
   at System.Windows.Application.RunDispatcher(System.Object)
   at System.Windows.Application.RunInternal(System.Windows.Window)
   at Contacts.App.Main()

And

   Faulting application name: PDL Updater.exe, version: 1.2.9.0, time stamp: 0x58aae81e
Faulting module name: KERNELBASE.dll, version: 10.0.10586.589, time stamp: 0x57cf948c
Exception code: 0xe0434352
Fault offset: 0x0000000000071f28
Faulting process id: 0x2948
Faulting application start time: 0x01d28b7a4e508b20
Faulting application path: C:\Program Files\PDL Updater\PDL Updater.exe
Faulting module path: C:\WINDOWS\system32\KERNELBASE.dll
Report Id: dd5f897a-24c9-4dd0-979e-4f0cbd747a94
Faulting package full name: 
Faulting package-relative application ID:

*edit This is the exception Message it throws me, hope it helps: Error Message

If you need anything else I'd be happy to provide it. Looking forward to getting this over with, so thank you already in advance!

c#
wpf
outlook
asked on Stack Overflow Feb 21, 2017 by Lennart • edited Feb 21, 2017 by Lennart

3 Answers

0

Try to target your platform:

In Visual Studio → project properties → in the Build tab → platform target = X86/X64/Any CPU

Change the platform target and test your application.

answered on Stack Overflow Feb 21, 2017 by Thiyagu Rajendran • edited Feb 21, 2017 by Uwe Keim
0

Do you run the code on a secondary thread?

Please remember that Office applications use the single threaded apartment model. You should use OOM on the main thread only. If you want to use multithreading you need to consider using a low-level API - Extended MAPI. Or just any wrapper around that API (for example, such as Redemption).

answered on Stack Overflow Feb 21, 2017 by Eugene Astafiev
0

It sure sounds like it blows up when you try to create an instance of the Outlook.Application object.

Make sure both Outlook and your app are running in the same security context. Is either app running with elevated privileges (Run As Administrator)?


User contributions licensed under CC BY-SA 3.0