Rabu, 03 Oktober 2012

C #: Stub kode untuk menulis komponen ActiveX

Berikut adalah kode dasar untuk menulis komponen ActiveX, ini perlu diikuti dengan instruksi untuk dll setup seperti didokumentasikan di sini


mengkonversi c # untuk Vb.Net. di sini

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

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

//Selain itu ditambahkan untuk 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 Antarmuka definisi
//
[
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);
};

//
// Menyediakan Pelaksanaan default untuk scripting yang aman.
// Ini pada dasarnya berarti IE tidak akan mengeluh tentang objek ActiveX tidak menjadi aman

//
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
};
}

Tidak ada komentar:

Posting Komentar

Recommended Post Slide Out For Blogger
Related Posts Plugin for WordPress, Blogger...