This chapter will introduce how to access the DLL created by C++(.Net) or C# in Origin C.
The following example shows how to create a DLL in Microsoft Visual Studio 2005 with C#, and then access it in Origin C. This Dll provides a class with properties and functions. Function Sum shows how to pass data array from Origin C vector to C# function.
using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace temp { [Guid("4A5BFDFA-7D41-49d1-BB57-C6816E9EDC87")] public interface INet_Temp { double Celsius{ get; set; } double Fahrenheit{ get; set; } double GetCelsius(); double GetFahrenheit(); double Sum(object obj); } } namespace temp { [Guid("2AE913C6-795F-49cc-B8DF-FAF7FBA49538")] public class NET_Temperature : INet_Temp { private double celsius; private double fahrenheit; public NET_Temperature() { } public double Celsius { get{ return celsius; } set { celsius = value; fahrenheit = celsius + 273.5; } } public double Fahrenheit { get { return fahrenheit; } set { fahrenheit = value; celsius = fahrenheit - 273.5; } } public double GetCelsius() { return celsius; } public double GetFahrenheit() { return fahrenheit; } public double Sum(object obj) { double[] arr = (double[])obj; double sum = 0.0; for (int nn = 0; nn < arr.Length; nn++) { sum += arr[nn]; } return sum; } } }
"%Windir%\Microsoft.NET\Framework64\v4.0.30319\regasm" "$(TargetPath)" /CodeBase
void access_DLL() { Object obj = CreateObject("temp.NET_Temperature"); obj.Celsius = 0; // access property out_double("", obj.GetFahrenheit()); // access function obj.Fahrenheit = 300; out_double("", obj.GetCelsius()); vector vec; vec.Data(1,10,1); _VARIANT var = vec.GetAs1DArray(); out_double("", obj.Sum(var)); }
The following shows how to create an ActiveX control by C#, and use it in a dialog in Origin C.
Steps 1~7 shows how to create an C# ActiveX control in Microsoft Visual Studio 2005.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace SampleControl { [Guid("A31FE123-FD5C-41a1-9102-D25EBD5FDFAF"), ComSourceInterfaces(typeof(UserEvents)), ClassInterface(ClassInterfaceType.None),] public partial class UserControl1 : UserControl, UserControl1Interface { public UserControl1() { InitializeComponent(); } protected override void OnPaint(PaintEventArgs pe) { Brush brush = new SolidBrush(Color.Beige); pe.Graphics.FillRectangle(brush, ClientRectangle); } public void SetBorder(bool bSet) { this.BorderStyle = bSet ? BorderStyle.FixedSingle : BorderStyle.None; Refresh(); } } //declare an Interface for the control settings [Guid("CCBD6133-813D-4dbb-BB91-16E3EFAE66B0")] public interface UserControl1Interface { void SetBorder(bool bSet); } }
//declare delegates for events public delegate void MouseAction(int x, int y);
public event MouseAction OnUserClick; public event MouseAction OnUserDbClick; private void UserControl1_MouseClick(object sender, MouseEventArgs e) { if (OnUserClick != null) { OnUserClick(e.X, e.Y); } } private void UserControl1_MouseDoubleClick(object sender, MouseEventArgs e) { if (OnUserDbClick != null) { OnUserDbClick(e.X, e.Y); } }
//declare interface for events [Guid("DA090A6F-FFAC-4a39-ACD3-351FA509CA86"), InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] public interface UserEvents { [DispIdAttribute(0x60020001)] void OnUserClick(int x, int y); [DispIdAttribute(0x60020002)] void OnUserDbClick(int x, int y); }
//Dialog and control Ids. Need change dialog id according to real case. #define IDD_DIALOG1 101 #define IDC_DOTNET 1000 //Exposed events from Control #define ON_INTEROP_CLICK(_idCntl, _ocFunc) ON_ACTIVEX_EVENT(0x60020001, _idCntl, _ocFunc, VTS_CTRL VTS_I4 VTS_I4) #define ON_INTEROP_DBLCLICK(_idCntl, _ocFunc) ON_ACTIVEX_EVENT(0x60020002, _idCntl, _ocFunc, VTS_CTRL VTS_I4 VTS_I4) class CDotNetComInteropDlg : public Dialog { public: CDotNetComInteropDlg(); EVENTS_BEGIN ON_INIT(OnInitDialog) //Event handler entries for the exposed events ON_INTEROP_CLICK(IDC_DOTNET, OnClick) ON_INTEROP_DBLCLICK(IDC_DOTNET, OnDblClick) EVENTS_END BOOL OnClick(Control ctrl, int x, int y) { printf("Clicked at (%d,%d)\n", x,y); return true; } BOOL OnDblClick(Control ctrl, int x, int y) { printf("DblClicked at (%d,%d)\n", x,y); return true; } BOOL OnInitDialog(); Control m_ctrlDotNet; }; // Do not specify DLL file path here. Assume the dll file under the same // path with the current c file. CDotNetComInteropDlg::CDotNetComInteropDlg() : Dialog(IDD_DIALOG1, "DialogBuilder.dll") { InitMsgMap(); } BOOL CDotNetComInteropDlg::OnInitDialog() { //[Guid("A31FE123-FD5C-41a1-9102-D25EBD5FDFAF")] GUID guid = {0xA31FE123, 0xFD5C, 0x41a1, {0x91, 0x02, 0xD2, 0x5E, 0xBD, 0x5F, 0xDF, 0xAF}}; RECT rect = {20,20,200,100}; if(m_ctrlDotNet.CreateActiveXControl(guid, WS_CHILD|WS_VISIBLE, rect, GetSafeHwnd(), IDC_DOTNET)) { //Control intialization using the exposed interface of //the DotNet control Object ctrlObj = m_ctrlDotNet.GetActiveXControl(); ctrlObj.SetBorder(true); } return TRUE; } void Launch_CDotNetComInteropDlg() { CDotNetComInteropDlg dlgDotNet; dlgDotNet.DoModal(); }