Thursday, September 17, 2009

C#: Stub code for writing ActiveX components

Here is the base code for writing the ActiveX component, this needs to be followed by instructions for setup etc. as documented here

***********************
Class: ActiveXDotNet.cs
***********************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//Additionally added for ActiveX
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Windows.Forms;

namespace NSActiveXDotNet
{
[Guid("E86A9038-368D-4e8f-B389-FDEF38935B2F"), InterfaceType(ComInterfaceType.InterfaceIsDual), ComVisible(true)]
public interface IActiveXDotNet
{
[DispId(1)]
void ShowDialog(string message);
};

[Guid("B769DA4B-FDDD-41BD-BDD2-7101D08E8E0C"), ClassInterface(ClassInterfaceType.None),
ComDefaultInterface(typeof(IActiveXDotNet)), ComVisible(true), ProgId("ActiveXDotNet.CActiveXDotNet")]
public class CActiveXDotNet : IObjectSafetyImpl, IActiveXDotNet
{

#region IActiveXDotNet Members

public void ShowDialog(string message)
{
MessageBox.Show(message, "Message From ActiveX", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

#endregion
}
}


***********************
Class: IObjectSafety.cs
***********************

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Text;

namespace NSActiveXDotNet
{
[
Serializable,
ComVisible(true)
]
public enum ObjectSafetyOptions
{
INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001,
INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002,
INTERFACE_USES_DISPEX = 0x00000004,
INTERFACE_USES_SECURITY_MANAGER = 0x00000008
};

//
// MS IObjectSafety Interface definition
//
[
ComImport(),
Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)
]
public interface IObjectSafety
{
[PreserveSig]
long GetInterfaceSafetyOptions(ref Guid iid, out int pdwSupportedOptions, out int pdwEnabledOptions);

[PreserveSig]
long SetInterfaceSafetyOptions(ref Guid iid, int dwOptionSetMask, int dwEnabledOptions);
};

//
// Provides a default Implementation for safe scripting.
// This basically means IE won't complain about the ActiveX object not being safe
//
public class IObjectSafetyImpl : IObjectSafety
{
private ObjectSafetyOptions m_options =
ObjectSafetyOptions.INTERFACESAFE_FOR_UNTRUSTED_CALLER |
ObjectSafetyOptions.INTERFACESAFE_FOR_UNTRUSTED_DATA;

#region [IObjectSafety implementation]
public long GetInterfaceSafetyOptions(ref Guid iid, out int pdwSupportedOptions, out int pdwEnabledOptions)
{
pdwSupportedOptions = (int)m_options;
pdwEnabledOptions = (int)m_options;
return 0;
}

public long SetInterfaceSafetyOptions(ref Guid iid, int dwOptionSetMask, int dwEnabledOptions)
{
return 0;
}
#endregion
};
}

1 comment:

  1. hELLO Man i see u are rich in C# iam Kind new please send me books for dummies if got sum nand tutorial on my email Mavimbela47@gmail.com

    ReplyDelete

Please use your common sense before making a comment, and I truly appreciate your constructive criticisms.