Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7554981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:28:53+00:00 2026-05-30T11:28:53+00:00

I am developing an excel addin and in this addin there are several AppDomains.

  • 0

I am developing an excel addin and in this addin there are several AppDomains. I need to have access to some shared data across each AppDomain so I decided to use a cross-AppDomain singleton. I followed what was described in this thread :

http://www.dolittle.com/blogs/einar/archive/2007/05/18/cross-appdomain-singleton.aspx

Because this is an excel addin, I had to modify it a little when creating the AppDomain that contains the singleton so that the correct base directory is used when searching for assemblies. Below is my modified version:

public class CrossAppDomainSingleton<T> : MarshalByRefObject where T : new()
{
    private static readonly string AppDomainName = "Singleton AppDomain";
    private static T _instance;

    private static AppDomain GetAppDomain(string friendlyName)
    {
        IntPtr enumHandle = IntPtr.Zero;
        mscoree.CorRuntimeHostClass host = new mscoree.CorRuntimeHostClass();
        try
        {
            host.EnumDomains(out enumHandle);

            object domain = null;
            while (true)
            {
                host.NextDomain(enumHandle, out domain);
                if (domain == null)
                {
                    break;
                }
                AppDomain appDomain = (AppDomain)domain;
                if (appDomain.FriendlyName.Equals(friendlyName))
                {
                    return appDomain;
                }
            }
        }
        finally
        {
            host.CloseEnum(enumHandle);
            Marshal.ReleaseComObject(host);
            host = null;
        }
        return null;
    }


    public static T Instance
    {
        get
        {
            if (null == _instance)
            {
                AppDomain appDomain = GetAppDomain(AppDomainName);
                if (null == appDomain)
                {
                    string baseDir = AppDomain.CurrentDomain.BaseDirectory;
                    appDomain = AppDomain.CreateDomain(AppDomainName, null, baseDir, null, false);
                }
                Type type = typeof(T);
                T instance = (T)appDomain.GetData(type.FullName);
                if (null == instance)
                {
                    instance = (T)appDomain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName);
                    appDomain.SetData(type.FullName, instance);
                }
                _instance = instance;
            }

            return _instance;
        }
    }
}

Here is my implementation of the CrossAppDomainSingleton :

public class RealGlobal : CrossAppDomainSingleton<RealGlobal>
{
    //ExcelApp Value Shared
    private Microsoft.Office.Interop.Excel.Application s_excelApp = null;

    public Microsoft.Office.Interop.Excel.Application GetExcelApp()
    {
        return s_excelApp;
    }

    public void SetExcelApp(Microsoft.Office.Interop.Excel.Application app)
    {
        s_excelApp = app;
    }
}

Once I try to use either the get or set method (I tried a property also but got no further), I systematically get an exception:

Nom inconnu. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))

or in English:
Unknown Name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))

Marshalling works fine when I keep built-in types, but given that the object I want to access (Microsoft.Office.Interop.Excel.Application) is a COM object, I fear that this is the problem.

I’m very new to Remoting and Marshalling. Any ideas? Does it have something to do with the serialization of a COM object?

Many thanks in advance!
Sean

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T11:28:54+00:00Added an answer on May 30, 2026 at 11:28 am

    You certainly should not be passing that Application object around, it will cause endless trouble.

    I suggest you write a small helper that you can call from each AppDomain to get the right Application object. There is a small snag in doing this, since the usual CreateObject approach will not always get the Excel Application instance for the process you are in. Andrew Whitechapel has an explanation and the right code here: http://blogs.officezealot.com/whitechapel/archive/2005/04/10/4514.aspx.

    Finally, you should take some care with locale issues when calling the Excel COM object in other language environments. Sometimes calls need to be localised, or you need to swith the thread’s UI language. Some info here: http://msdn.microsoft.com/en-us/library/aa168494(v=office.11).aspx and some info on what they do in VSTO here: http://blogs.msdn.com/b/eric_carter/archive/2005/06/15/429515.aspx.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Developing a project of mine I realize I have a need for some level
I am getting started with developing an Excel-DNA addin using IronPython with some C#
I have a huge excel workbook that I've been developing to do some cost
I have this application I'm developing in JSP and I wish to export some
I am developing a small excel plugin. As part of it I need to
I am (unfortunately) developing an application in Excel 2000 VBA. I believe I have
I am developing a Excel shared Add-in which has the menu called Custom which
I plan on developing Excel Add-ins and I have looked around on ways of
I am developing an application to read an excel spreadsheet, validate the data and
I am developing a C# add in for Excel (using VSTO tools). I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.