You may receive the System.Reflection.TargetInvocationException
unhandled exception when you use the FormsFactory
class to open a form that has a user interface add-in installed. This problem occurs in Microsoft Office Accounting Professional, in Microsoft Office Accounting Express, and in Microsoft Office Small Business Accounting.
This problem occurs because the Microsoft .NET Framework deserializer that is defined in the mscorlib assembly tries to load the assembly in which the object to be deserialized is defined. The .NET Framework cannot locate the Accounting Professional assemblies, the Accounting Express assemblies, or the Small Business Accounting assemblies. The .NET Framework cannot locate the assemblies because the application that made the call was started from a different directory.
To resolve this problem, you must explicitly create an application domain. You must set up this domain to include the path of the accounting program and the path of the integrating application. To do this, follow these steps:
- Install the accounting program and the integrating application in the same root directory. The default installation directory for Small Business Accounting is C:\Program Files\Microsoft Small Business\Small Business Accounting. If this directory is the directory where Small Business Accounting is installed, use the following installation paths:
In Accounting Express 2007 and in Accounting Professional 2007, use the following installation path:
- \Program Files\Microsoft Small Business\Small Business Accounting
- \Program Files\Microsoft Small Business\My Integration
In Accounting Express 2008 and in Accounting Professional 2008, use the following installation path:
- C:\Program Files\Microsoft Small Business\Small Business Accounting 2007
- C:\Program Files\Microsoft Small Business\Office Accounting 2008
- Put the code that opens the form by using the FormsFactory class in its own class. Also, put the code that calls the FormsFactory class in its own class. To do this, see the code in the "More Information" section.
Run the following code. This code constructs and then uses the FormsFactory
class and the accounting program form in the domain that is explicitly created.
The following code creates the application domain.
AppDomain myDomain = AppDomain.CreateDomain(
"My Integration;Small Business Accounting",
ObjectHandle objectHandle = myDomain.CreateInstance(
ExecutingCode ec = (ExecutingCode) objectHandle.Unwrap();
The following code adds the directories that are involved in the application domain.
static string GetSbaFullBusinessLogicAssemblyPath(string assemblyName)
// If you run the Setup program, the installation path is in the registry.
const string InstallPathRegKey =
@"SOFTWARE\Microsoft\Business Solutions eCRM\2.0";
const string MagellanInstallPathRegKey =
@"SOFTWARE\Microsoft\Business Solutions eCRM";
const string InstallPathRegValue = "AccountingUI";
const string DefaultPath =
@"C:\Program Files\Microsoft Small Business\";
StringBuilder installPath = new StringBuilder();
RegistryKey regkey =
if (regkey != null)
// A Small Business Accounting installation exists.
// This line removes the file name and the Small Business Accounting folder.
int lastSlash = installPath.ToString().LastIndexOf(@"\");
int charToEnd = installPath.Length - (lastSlash);
// This line tells where the last slash is.
// This line finds the newest last slash.
lastSlash = installPath.ToString().LastIndexOf(@"\");
// This line determines how many characters the Remove method must remove.
charToEnd = installPath.Length - (lastSlash);
// This line removes the directory name.
// This line looks for a Magellan installation.
regkey = Registry.LocalMachine.OpenSubKey(MagellanInstallPathRegKey);
if (regkey != null)
Object temp = regkey.GetValue(InstallPathRegValue);
if (temp != null)
if (regkey != null)
if (installPath.Length == 0)
// This line appends the default value of the installation path.
The following code creates an instance of the ExecutingCode
class that opens the form by using the FormsFactory
public class ExecutingCode : MarshalByRefObject
public void LaunchCustomerForm()
ObjectHandle sbiHandle =
ISmallBusinessInstance iSbi =
// This line determines that you can log on by using the database name and the server name.
DataRow dr = iSbi.CustomerAccounts.DataView.Table.Rows;
ICustomerAccount cust =
ObjectHandle ffHandle = AppDomain.CurrentDomain.CreateInstance(
IFormsFactory iFF = (IFormsFactory) ffHandle.Unwrap();
Form form = iFF.CreateForm(cust);
Article ID: 906467 - Last Review: September 10, 2011 - Revision: 2.0
- Microsoft Office Accounting Express 2008
- Microsoft Office Accounting Professional 2008
- Microsoft Office Accounting Express 2007
- Microsoft Office Accounting Professional 2007
- Microsoft Office Small Business Management Edition 2006
- Microsoft Office Small Business Accounting 2006 Software Development Kit 1.2
|kbtshoot kbmbsmigrate kbprb KB906467|