none
Wrapper of a .net library with strongnames and enum's to a com class RRS feed

  • Question

  • Hi

    I'm a newbie in C# and VS2010 so please bear with me.

    I need to wrap a .net library into a com class for use with delphi.

    I have a problem wrapping the enum types from the .net library how do I expose them in the interface?

    e.g. ViewType

    This doesn't work!
    public Siemens.DesigoInsight.SODIAPI.Contract.ViewType diViewType;

    using System;
    using System.Collections.Generic;
    using System.Runtime.InteropServices;
    using System.ServiceModel;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using Siemens.DesigoInsight.SODIAPI.Contract;
    using Siemens.DesigoInsight.SODIAPI.ClientAPI;
    
    
    namespace AlarmDirector.ADmin.COMObjects.DesigoAdminBrowser
    {
      
      [Guid("9E65E974-6E66-4F38-BAFF-30F2B5B80F7E")]
      [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
      [ComVisible(true)]
      public interface IADDesigoBrowser
     {
    
       [DispId(1)]
       void Initialize(String serverURL,
                               String userName,
                               String password,
                               String certificateString);
       [DispId(2)]
       String GetDesignations();
     }
    
      [Guid("62A417B4-905C-4090-B4D2-D602724E9017")] 
      [ClassInterface(ClassInterfaceType.None)]
      [ComVisible(true)]
      
      public class ADDesigoBrowser : IADDesigoBrowser
      {
        protected String _serverURL;
        protected String _userName;
        protected String _password;
        protected String _certificateString;
        protected bool _initialized;
        
        //Remove when debug is completed.
        protected int _nodeCounter;
    
        protected Siemens.DesigoInsight.SODIAPI.ClientAPI.DIAPIServiceClient _client;
          //BrowserServiceClient _client;
    //------------------------------------------------------    
        public Siemens.DesigoInsight.SODIAPI.Contract.ViewType diViewType;
    
        public enum someenum
        {
          t1, t2, t3
        }
    //------------------------------------------------------    
    
        static ADDesigoBrowser()
        {
        }//constructor
    
        public void Initialize(String serverURL,
                               String userName,
                               String password,
                               String certificateString)
        {
          _serverURL = serverURL;
          _userName = userName;
          _password = password;
          _certificateString = certificateString;
    
          _client = new Siemens.DesigoInsight.SODIAPI.ClientAPI.DIAPIServiceClient(_serverURL, null, _userName, _password, _certificateString);
               
          _initialized = true;
          _nodeCounter = 0;
        }// Initialize
        
    //------------------------------------------------------    
    
        public bool Initialised { get { return _initialized; } }
    
    //------------------------------------------------------    
        public String GetDesignations()
        {
            return "test";
        }//
    //------------------------------------------------------    
    
      } // DesigoBrowser
    }
    


    Henry

    • Moved by Lisa Zhu Friday, October 19, 2012 8:29 AM CLR related (From:Visual C# Language)
    Thursday, October 18, 2012 12:49 PM

Answers

  • I don't know whether Delphi supports Enums.. But usually Enums are exposed as integers. Hence you have to Marsha the return type as integer. So, annotate that statement as below,

    [MarshalAs( UnmanagedType.I4)]
    public Siemens.DesigoInsight.SODIAPI.Contract.ViewType 
    diViewType; 

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    Thursday, October 18, 2012 4:08 PM
  • Hello Henry64,

    1. Like interfaces and structures, Enums, when exposed to COM, should be declared with the ComVisibleAttribute and the GuidAttribute, e.g. :

    namespace AlarmDirector.ADmin.COMObjects.DesigoAdminBrowser
    {
        [ComVisible(true)]
        [Guid("E3A77A14-0247-4d49-AC97-3FB8C9005BDD")]
        public enum someenum
        {
            t1, t2, t3
        }
    
    	...
    	...
    	...
    }

    1.1 With the above declaration, the type library generated for your class library will contain the appropriate IDL definition for someenum :

        typedef [uuid(E3A77A14-0247-4D49-AC97-3FB8C9005BDD), version(1.0)    ,
        custom({0F21F359-AB84-41E8-9A78-36D110E6D2F9}, "AlarmDirector.ADmin.COMObjects.DesigoAdminBrowser.someenum")    
    ]
        enum {
            someenum_t1 = 0,
            someenum_t2 = 1,
            someenum_t3 = 2
        } someenum;
    

    2. You may even include this enum as the type for a property of an interface, e.g. :

    [Guid("9E65E974-6E66-4F38-BAFF-30F2B5B80F7E")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [ComVisible(true)]
    public interface IADDesigoBrowser
    {
    
        [DispId(1)]
        void Initialize(String serverURL,
                                String userName,
                                String password,
                                String certificateString);
        [DispId(2)]
        String GetDesignations();
    
        someenum property_someenum
        {
            get;
            set;
        }
    }

    2.1 This will generate the following type library definition for IADDesigoBrowser :

        [
          odl,
          uuid(9E65E974-6E66-4F38-BAFF-30F2B5B80F7E),
          version(1.0),
          oleautomation,
            custom({0F21F359-AB84-41E8-9A78-36D110E6D2F9}, "AlarmDirector.ADmin.COMObjects.DesigoAdminBrowser.IADDesigoBrowser")    
    
        ]
        interface IADDesigoBrowser : IUnknown {
            HRESULT _stdcall Initialize(
                            [in] BSTR serverURL, 
                            [in] BSTR userName, 
                            [in] BSTR password, 
                            [in] BSTR certificateString);
            HRESULT _stdcall GetDesignations([out, retval] BSTR* pRetVal);
            [propget]
            HRESULT _stdcall property_someenum([out, retval] someenum* pRetVal);
            [propput]
            HRESULT _stdcall property_someenum([in] someenum pRetVal);
        };
    

    2.2 The following would be how class ADDesigoBrowser implement the property_someenum property :

        public class ADDesigoBrowser : IADDesigoBrowser
        {
    	...
    	...
    	...
            public someenum m_someenum;
            //------------------------------------------------------    
    
            static ADDesigoBrowser()
            {
            }//constructor
    
            public void Initialize(String serverURL,
                                   String userName,
                                   String password,
                                   String certificateString)
            {
    		...
    		...
    		...
            }// Initialize
    
            //------------------------------------------------------    
    
            public bool Initialised { get { return _initialized; } }
    
            //------------------------------------------------------    
            public String GetDesignations()
            {
                return "test";
            }//
    
            public someenum property_someenum
            {
                get
                {
                    return m_someenum;
                }
    
                set
                {
                    m_someenum = value;
                }
            }
            //------------------------------------------------------    
    
        } // DesigoBrowser
    

    3. I am not familiar with Delphi but as a modern language compiler that supports COM development, it should be able to process Type Libraries which is a starting point of COM client application development.

    3.1 As part of the processing for a type library, Delphi will produce a declaration for an enum type using either : its own equivalent of the IDL enum type or the system integer type.

    - Bio.


    Please visit my blog : http://limbioliong.wordpress.com/

    Friday, October 19, 2012 6:53 AM

All replies

  • I don't know whether Delphi supports Enums.. But usually Enums are exposed as integers. Hence you have to Marsha the return type as integer. So, annotate that statement as below,

    [MarshalAs( UnmanagedType.I4)]
    public Siemens.DesigoInsight.SODIAPI.Contract.ViewType 
    diViewType; 

    I hope this helps.

    Please mark this post as answer if it solved your problem. Happy Programming!

    Thursday, October 18, 2012 4:08 PM
  • Hello Henry64,

    1. Like interfaces and structures, Enums, when exposed to COM, should be declared with the ComVisibleAttribute and the GuidAttribute, e.g. :

    namespace AlarmDirector.ADmin.COMObjects.DesigoAdminBrowser
    {
        [ComVisible(true)]
        [Guid("E3A77A14-0247-4d49-AC97-3FB8C9005BDD")]
        public enum someenum
        {
            t1, t2, t3
        }
    
    	...
    	...
    	...
    }

    1.1 With the above declaration, the type library generated for your class library will contain the appropriate IDL definition for someenum :

        typedef [uuid(E3A77A14-0247-4D49-AC97-3FB8C9005BDD), version(1.0)    ,
        custom({0F21F359-AB84-41E8-9A78-36D110E6D2F9}, "AlarmDirector.ADmin.COMObjects.DesigoAdminBrowser.someenum")    
    ]
        enum {
            someenum_t1 = 0,
            someenum_t2 = 1,
            someenum_t3 = 2
        } someenum;
    

    2. You may even include this enum as the type for a property of an interface, e.g. :

    [Guid("9E65E974-6E66-4F38-BAFF-30F2B5B80F7E")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [ComVisible(true)]
    public interface IADDesigoBrowser
    {
    
        [DispId(1)]
        void Initialize(String serverURL,
                                String userName,
                                String password,
                                String certificateString);
        [DispId(2)]
        String GetDesignations();
    
        someenum property_someenum
        {
            get;
            set;
        }
    }

    2.1 This will generate the following type library definition for IADDesigoBrowser :

        [
          odl,
          uuid(9E65E974-6E66-4F38-BAFF-30F2B5B80F7E),
          version(1.0),
          oleautomation,
            custom({0F21F359-AB84-41E8-9A78-36D110E6D2F9}, "AlarmDirector.ADmin.COMObjects.DesigoAdminBrowser.IADDesigoBrowser")    
    
        ]
        interface IADDesigoBrowser : IUnknown {
            HRESULT _stdcall Initialize(
                            [in] BSTR serverURL, 
                            [in] BSTR userName, 
                            [in] BSTR password, 
                            [in] BSTR certificateString);
            HRESULT _stdcall GetDesignations([out, retval] BSTR* pRetVal);
            [propget]
            HRESULT _stdcall property_someenum([out, retval] someenum* pRetVal);
            [propput]
            HRESULT _stdcall property_someenum([in] someenum pRetVal);
        };
    

    2.2 The following would be how class ADDesigoBrowser implement the property_someenum property :

        public class ADDesigoBrowser : IADDesigoBrowser
        {
    	...
    	...
    	...
            public someenum m_someenum;
            //------------------------------------------------------    
    
            static ADDesigoBrowser()
            {
            }//constructor
    
            public void Initialize(String serverURL,
                                   String userName,
                                   String password,
                                   String certificateString)
            {
    		...
    		...
    		...
            }// Initialize
    
            //------------------------------------------------------    
    
            public bool Initialised { get { return _initialized; } }
    
            //------------------------------------------------------    
            public String GetDesignations()
            {
                return "test";
            }//
    
            public someenum property_someenum
            {
                get
                {
                    return m_someenum;
                }
    
                set
                {
                    m_someenum = value;
                }
            }
            //------------------------------------------------------    
    
        } // DesigoBrowser
    

    3. I am not familiar with Delphi but as a modern language compiler that supports COM development, it should be able to process Type Libraries which is a starting point of COM client application development.

    3.1 As part of the processing for a type library, Delphi will produce a declaration for an enum type using either : its own equivalent of the IDL enum type or the system integer type.

    - Bio.


    Please visit my blog : http://limbioliong.wordpress.com/

    Friday, October 19, 2012 6:53 AM
  • Hi Henry,

    From your description , I ‘d like to move this post to  the most related forum .

    There has more  experts in this aspect  , so you will get  better support and  may have more luck getting answers .

    Thanks for your understanding .

    Regards ,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us

    Friday, October 19, 2012 8:28 AM