잊지 않겠습니다.

'COM'에 해당되는 글 1건

  1. 2010.01.07 0011. CSServicedComponent
.NET으로 COM+ 객체를 만드는 예제.

.NET으로 COM+객체를 만들때는 먼저 Project Property에 Comvisible(true)로 설정이 되어 있어야지 된다. 그리고 assembly:Guid 속성에 component guid를 등록시켜야지만 com 객체를 만드는 가장 기초적인 설정이 끝이 난다. 이와같은 설정을 하면 작성된 dll이나 exe를 com+ 객체로 등록할 수 있다.

1.// Setting ComVisible to false makes the types in this assembly not visible
2.// to COM components.  If you need to access a type in this assembly from
3.// COM, set the ComVisible attribute to true on that type.
4.[assembly: ComVisible(true)]
5.// The following GUID is for the ID of the typelib if this project is exposed to COM
6.[assembly: Guid("3308202e-a355-4c3b-805d-b527d1ef5fa3")]

그러나, dll을 등록한다고해서 library안의 class들이 모두 com객체로 사용할 수 있는 것이 아니다. dll은 여러개의 com객체를 담을 수 있는 상자이고, 그 상자안에 채워야지 될 com객체들은 각각 작성되어야지 된다. com을 이용하는 것은 com 객체 뿐 아니라, com 객체를 구성하고 있는 interface역시 사용할 수 있어야지 되기 때문에, com으로 외부에서 노출될 모든 interface와 class들은 Guid Class Property를 갖고, 각 Guid 값을 가져야지 된다.

com으로 사용되기 위해서, class와 interface가 가져야지 될 속성은 각각 다른데, interface는 ComInterfaceType Property와 Guid를 가져야지 되며, class는 Com으로 제공될 ComSourceInterfaces, ClassInterface, Transaction, Guid, Com 객체 사용에서 Pool과 CreateTimeOut을 제한하는 ObjectPooling attribute를 가져야지 된다.

com interface 구현 예제
01.#region Interfaces
02. 
03.[Description("ICSSimpleObject description")]
04.[Guid("2A59630E-4232-4582-AE47-706E2B648579")]
05.// The public interface describing the COM interface of the coclass
06.public interface ICSSimpleObject
07.{
08.    #region Properties
09. 
10.    float FloatProperty { get; set; }
11. 
12.    #endregion
13. 
14.    #region Methods
15. 
16.    [Description("HelloWorld description")]
17.    string HelloWorld();
18. 
19.    void GetProcessThreadID(out uint processId, out uint threadId);
20. 
21.    [Description("DoTransaction description")]
22.    void DoTransaction();
23. 
24.    #endregion
25.}
26. 
27.[Guid("A06C000A-7A85-42dc-BA6D-BE736B6EB97A")]
28.[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
29.// The public interface describing the events the coclass can sink
30.public interface ICSSimpleObjectEvents
31.{
32.    #region Events
33. 
34.    [DispId(1)]
35.    void FloatPropertyChanging(float NewValue, ref bool Cancel);
36. 
37.    #endregion
38.}
39.#endregion

com class 구현 예제
01.// The ObjectPooling attribute is used to configure the component's object
02.// pooling. It can enable or disables object pooling and set the min. or 
03.// max. pool size and object creation timeout.
04.[ObjectPooling(MinPoolSize=2, MaxPoolSize=10, CreationTimeout=20)]
05.// Creates the component with a new transaction, regardless of the state
06.// of the current context.
07.[Transaction(TransactionOption.Required)]
08.[ClassInterface(ClassInterfaceType.None)]
09.[ComSourceInterfaces(typeof(ICSSimpleObjectEvents))]
10.[Guid("14EAD156-F55E-4d9b-A3C5-658D4BB56D30")]
11.public class CSSimpleObject : ServicedComponent, ICSSimpleObject
12.{
13.    #region Properties
14. 
15.    /// <summary>
16.    /// The private members don't make it into the type-library and are
17.    /// hidden from the COM clients.
18.    /// </summary>
19.    private float fField = 0;
20. 
21.    /// <summary>
22.    /// A public property with both get and set accessor methods.
23.    /// </summary>
24.    public float FloatProperty
25.    {
26.        get { return this.fField; }
27.        set
28.        {
29.            bool cancel = false;
30.            // Raise the event FloatPropertyChanging
31.            if (null != FloatPropertyChanging)
32.                FloatPropertyChanging(value, ref cancel);
33.            if (!cancel)
34.                this.fField = value;
35.        }
36.    }
37. 
38.    #endregion
39. 
40.    #region Methods
41. 
42.    /// <summary>
43.    /// A public method that returns a string "HelloWorld".
44.    /// </summary>
45.    /// <returns>"HelloWorld"</returns>
46.    public string HelloWorld()
47.    {
48.        return "HelloWorld";
49.    }
50. 
51.    /// <summary>
52.    /// A public method with two outputs: the current process Id and the
53.    /// current thread Id.
54.    /// </summary>
55.    /// <param name="processId">[out] The current process Id
56.    /// <param name="threadId">[out] The current thread Id
57.    public void GetProcessThreadID(out uint processId, out uint threadId)
58.    {
59.        processId = NativeMethod.GetCurrentProcessId();
60.        threadId = NativeMethod.GetCurrentThreadId();
61.    }
62. 
63.    public void DoTransaction()
64.    {
65.        try
66.        {
67.            // Operate on the resource managers like DBMS
68.            // ...
69.     
70.            ContextUtil.SetComplete();  // Commit
71.        }
72.        catch (Exception ex)
73.        {
74.            ContextUtil.SetAbort();     // Rollback
75.            throw ex;
76.        }
77.    }
78. 
79.    #endregion
80. 
81.    #region Events
82. 
83.    [ComVisible(false)]
84.    public delegate void FloatPropertyChangingEventHandler(float NewValue, ref bool Cancel);
85. 
86.    /// <summary>
87.    /// A public event that is fired before new value is set to the
88.    /// FloatProperty property. The Cancel parameter allows the client
89.    /// to cancel the change of FloatProperty.
90.    /// </summary>
91.    public event FloatPropertyChangingEventHandler FloatPropertyChanging;
92. 
93.    #endregion
94.}
Posted by Y2K
,