Introduction
Step by step instructions to achieve the registration free activation of the .NET assembly side by side with fox-pro application through COM, SXS and CLR.
Using the code
Step 1 - Create a .NET assembly as COM Sever
- Create a .NET class and Include the namespace
System.Runtime.InteropServices;
- Generate and apply a guid attribute for the class. I didn't apply the
ClassInterfaceType
attribute, as the default is AutoDispatch
. You can change the ClassInterfaceType
to none, if you want to expose functionality through interfaces implemented explicitly by the class.
using System.Runtime.InteropServices;
namespace Netnamespace
{
[Guid("503322EE-482A-442F-8984-4FC3FDC53A0C")]
public class NetClass
{
public string NetMethod(string input)
{
}
}
}
Step 2 - Sign the .NET assembly
It's mandatory to sign the assembly with a strong name for registration free interoperability. Hence generate a key pair by execute the command sn -k keypair.snk in the command prompt. Edit the following assembly attributes in AssemblyInfo.cs and build the project.
[assembly: ComVisible(true)]
Note - This attribute can be applied on specific methods to avoid exposing entire assembly.
[assembly: AssemblyKeyFileAttribute("..\\..\\keypair.snk")]
Note - Use the absolute or relative path to locate the .snk file.
[assembly: Guid("A67D75D0-7AE7-434F-8A41-2C1E0B48ADF3")]
Step 3 - Embed Sxs manifest into the .NET assembly
The .NET assembly must have a SxS manifest describing the COM registration information. Add a manifest file in your project and enter the assembly identity as below.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
type="win32"
name="<Assembly name>"
version="<Assembly version>"
publicKeyToken="<Assembly PublicKey Token>" />
<clrClass
clsid="<GUID applied over the .Net class>"
progid="<namespace.classname>"
threadingModel="Both"
name="<namespace.classname>"
runtimeVersion="<CLR Version>" >
</clrClass>
</assembly>
Download the GenMan32.zip and extract the GenMan32.exe. Execute the below command in the command prompt to embed the manifest.
genman32.exe "..\bin\Release\assemblyname.dll" /add /manifest:"..\assemblyname.dll.manifest"
Note - Alter the relative path according to your project or use absolute path in the command.
Step 4 - Create external manifest for the foxpro application(COM client)
Download the projecthookex.zip and extract the projecthookex.vct. Edit getdefaultmanifest
method of moduleresourceeditor as below through fox-pro. Then Hook the edited projecthookex.vct as a Project Class in the Project Info of fox-pro. The project class is applied only after close and reopen the fox-pro application. Build the fox-pro application will generate an external manifest.
LOCAL lcReturn
*!* The following is the default manifest in a compiled VFP app
TEXT TO m.lcReturn textmerge noshow
="1.0"="yes"
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
version="9.0.0.0"
type="win32"
name="<Foxpro EXE Name>"
processorArchitecture="x86"
/>
<description><Description about Foxpro EXE></description>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
language="*"
processorArchitecture="x86"
publicKeyToken="6595b64144ccf1df"
/>
</dependentAssembly>
</dependency>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="<.Net Assembly name>"
version="<.Net Assembly version>"
publicKeyToken="<.Net Assembly publickey token>"
/>
</dependentAssembly>
</dependency>
</assembly>
ENDTEXT
RETURN (m.lcReturn)
Step 5: Call the .Net class from foxpro application(COM Client)
objVFP = CreateObject("<progId>")
lresult = objVFP.NetMethod("")
Note - progid configured in .NET assembly manifest.
Step 6 - SXS Trace to trace the COM activation error
If the COM activation failed due to invalid configuration in manifest, then the fox-pro application launch shows the following error.
"The application has failed to start because its side by side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail".
Execute the below command in the command prompt to start the trace.
C:\Windows\System32>SxsTrace Trace -logfile:SxsTrace.etl
Launch the foxpro application(COM client) and execute the event to trigger the activation of .NET assembly. Stop the tracing in the command prompt.
Execute the following command to output the error:
C:\Windows\System32>SxsTrace Parse -logfile:SxsTrace.etl -outfile:SxsTrace.txt
Find the generated SxsTrace.txt in C:\Windows\System32 explains about the invalid configuration of the manifest.
Reference Links
Points of Interest
SxsTrace tool is interesting to trace the COM activation issues.
History
- 25-Jan-2018 - Created the article.
- 27-Jan-2018 - Updated the License and also formatted the xml content.