C# COM-Visible Example
- Last UpdatedMar 19, 2016
- 2 minute read
The following C# code is an example of a COM object that contains an import method and an export method:
using System;
using System.Data;
using System.Data.OleDb;
using System.Runtime.InteropServices;
namespace SCCCOMObjNamespace
{
[GuidAttribute("B25215A6-CDB0-47A6-A3FD-DBDE69749198")]
public interface ISCCCOMObjClass
{
long ImportMethod(long session_id, string sched_id, string context);
long ExportMethod(long session_id, string sched_id, ADODB.Recordset rs, string context);
void Close();
}
[ComDefaultInterface(typeof(ISCCCOMObjClass))]
[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
[GuidAttribute("22C7350C-15CF-43E0-97C2-DE1A3150D4EE")]
[ProgId("SCCCOMObjNamespace.SCCCOMObjClass.1")]
public class SCCCOMObjClass : ISCCCOMObjClass, IDisposable
{
public long ImportMethod(long session_id, string sched_id, string context)
{
// Add processing code here
return 0;
}
public long ExportMethod(long sessionId, string sched_id, ADODB.Recordset rs, string context)
{
// Following 3 lines show an easy way to create a dataset ds with the contents of the ADODB recordset rs
OleDbDataAdapter da = new OleDbDataAdapter();
DataSet ds = new DataSet();
da.Fill(ds, rs, "ATableName");
// Add processing code here
return 0;
}
public void Close()
{
Dispose();
}
public void Dispose()
{
GC.SupclickFinalize(this);
}
}
}
The two GUIDs in the GuidAttributes decorating the interface and class are created using the Create GUID choice in Visual Studio’s Tools menu and will not be the same as those shown in this example. The DLL that will result from this code needs to be registered in the GAC using REGASM (found in the latest version folder beneath the Microsoft.NET\Framework folder in the Windows folder; for example, c:\Windows\ Microsoft.NET\Framework\v4.0.3019) using the /codebase option.
In this case the entries in the Data tab for the import schedule would be:
-
Import Data Type: Import from COM object
-
COM object: SCCCOMObjNamespace.SCCCOMObjClass.1
-
Method to Call on COM Object: ImportMethod
-
Method Context Parameter: <as needed>
and the entries on the Transport tab for the export schedule would be:
-
Transport Type: Call COM object
-
COM object: SCCCOMObjNamespace.SCCCOMObjClass.1
-
Method to Call on COM Object: ExportMethod
-
Method Context Parameter: <as needed>