Microsoft KB Archive/906467

= You may receive the System.Reflection.TargetInvocationException unhandled exception when you use the FormsFactory class to open a form in Accounting Professional, in Accounting Express, or in Small Business Accounting =

Article ID: 906467

Article Last Modified on 11/28/2007

-

APPLIES TO


 * Microsoft Office Accounting Express 2008
 * Microsoft Office Accounting Professional 2008
 * Microsoft Office Accounting Express 2007
 * Microsoft Office Accounting Professional 2007
 * Microsoft Office Small Business Accounting 2006
 * Microsoft Office Small Business Management Edition 2006
 * Microsoft Office Small Business Accounting 2006 Software Development Kit 1.2

-





SYMPTOMS
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.



CAUSE
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.



RESOLUTION
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:  \Program Files\Microsoft Small Business\Small Business Accounting \Program Files\Microsoft Small Business\My Integration

In Accounting Express 2007 and in Accounting Professional 2007, use the following installation path:  C:\Program Files\Microsoft Small Business\Small Business Accounting 2007

In Accounting Express 2008 and in Accounting Professional 2008, use the following installation path:  C:\Program Files\Microsoft Small Business\Office Accounting 2008</li></ul> </li> 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 &quot;More Information&quot; section.</li></ol>

<div class="moreinformation_section">

MORE INFORMATION
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(   &quot;MyDomain&quot;,     null,     GetSbaFullBusinessLogicAssemblyPath(&quot;&quot;),     &quot;My Integration;Small Business Accounting&quot;,     false);

ObjectHandle objectHandle = myDomain.CreateInstance(   Assembly.GetExecutingAssembly.FullName,    &quot;SBA.ExecutingCode&quot;);

ExecutingCode ec = (ExecutingCode) objectHandle.Unwrap;

ec.LaunchCustomerForm; 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 = @&quot;SOFTWARE\Microsoft\Business Solutions eCRM\2.0&quot;; const string MagellanInstallPathRegKey = @&quot;SOFTWARE\Microsoft\Business Solutions eCRM&quot;; const string InstallPathRegValue = &quot;AccountingUI&quot;; const string DefaultPath = @&quot;C:\Program Files\Microsoft Small Business\&quot;;

StringBuilder installPath = new StringBuilder;

RegistryKey regkey = Registry.LocalMachine.OpenSubKey(InstallPathRegKey);

if (regkey != null) {       // A Small Business Accounting installation exists. installPath.Append(           regkey.GetValue(InstallPathRegValue).ToString);

// This line removes the file name and the Small Business Accounting folder. if (installPath.ToString.EndsWith(&quot;sbaui.dll&quot;)) {           int lastSlash = installPath.ToString.LastIndexOf(@&quot;\&quot;); int charToEnd = installPath.Length - (lastSlash);

// This line tells where the last slash is. installPath.Remove(lastSlash, charToEnd);

// This line finds the newest last slash. lastSlash = installPath.ToString.LastIndexOf(@&quot;\&quot;);

// This line determines how many characters the Remove method must remove. charToEnd = installPath.Length - (lastSlash);

// This line removes the directory name. installPath.Remove(lastSlash, charToEnd); }   }    else {       // This line looks for a Magellan installation. regkey =           Registry.LocalMachine.OpenSubKey(MagellanInstallPathRegKey);

if (regkey != null) {           Object temp = regkey.GetValue(InstallPathRegValue);

if (temp != null) {               installPath.Append(temp.ToString ); }       }    }

if (regkey != null) {       regkey.Close; }

if (installPath.Length == 0) {       // This line appends the default value of the installation path. installPath.Append(DefaultPath); }

installPath.Append(assemblyName);

return installPath.ToString; } The following code creates an instance of the ExecutingCode class that opens the form by using the FormsFactory class. [Serializable] public class ExecutingCode : MarshalByRefObject {   public void LaunchCustomerForm {       ObjectHandle sbiHandle = AppDomain.CurrentDomain.CreateInstance(               &quot;SBAAPI&quot;,                &quot;Microsoft.BusinessSolutions.SmallBusinessAccounting.SmallBusinessInstance&quot;);

ISmallBusinessInstance iSbi = (ISmallBusinessInstance) sbiHandle.Unwrap;

// This line determines that you can log on by using the database name and the server name. iSbi.LogOn(companyName, serverName);

DataRow dr = iSbi.CustomerAccounts.DataView.Table.Rows[6]; ICustomerAccount cust = (ICustomerAccount)iSbi.CustomerAccounts.GetByDataRow(dr);

ObjectHandle ffHandle = AppDomain.CurrentDomain.CreateInstance(           &quot;SBAUI&quot;,             &quot;Microsoft.BusinessSolutions.SmallBusinessAccounting.UI.FormsFactory&quot;); IFormsFactory iFF = (IFormsFactory) ffHandle.Unwrap; iFF.Initialize(iSbi); Form form = iFF.CreateForm(cust); form.ShowDialog; form.Dispose; iFF.UnInitialize(false); } }

Additional query words: Content Maintenance 44549, SBA, SDK

Keywords: kbtshoot kbprb kbmbsmigrate KB906467

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.