locked
Problem in loading/accessing .NET COM in Web RRS feed

  • Question

  • Hi All,
    I have created a .NET COM DLL. I am trying to create an ActiveX kind of control in .NET for my Website. The code snippet is,

    FileUpload.cs

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    namespace FileUploadControl
    {
        [ProgId("FileUploadControl.FileUpload")]
        [Guid("ACA8FA62-AD76-4ee3-A2E0-C9CCC55BB98E")]
        [ClassInterface(ClassInterfaceType.AutoDual)]
        public class FileUpload : IFileUpload
        {
            private string _filter = string.Empty;
            private bool _isMultiSelectionMode = false;
    
            public FileUpload()
            {            
            }
    
            #region IFileUpload Members
    
            public string Filter
            {
                get
                {
                    return _filter;
                }
                set
                {
                    _filter = value;
                }
            }
    
            public bool IsMultiSelectionMode
            {
                get
                {
                    return _isMultiSelectionMode;
                }
                set
                {
                    _isMultiSelectionMode = value;
                }
            }
    
            public void ShowDialog()
            {
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Filter = _filter;
                dialog.Multiselect = _isMultiSelectionMode;
    
                dialog.ShowDialog();
            }
    
            #endregion
        }
    }
    


    IFileUpload.cs


    using System;
    using System.Text;
    using System.Runtime.InteropServices;
    
    namespace FileUploadControl
    {
        [Guid("725F78AC-02CC-4d38-A355-9137CEEB3B10")]
        [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
        public interface IFileUpload
        {
            [DispId(0)]
            string Filter
            {
                get;
                set;
            }
    
            [DispId(1)]
            bool IsMultiSelectionMode
            {
                get;
                set;
            }
    
            [DispId(2)]
            void ShowDialog();
    
        }
    }
    


    The project builds successfully. But the object is not accessing in the Web. I have register the DLL in GAC also (Strong name also created). I am trying to invoke the ShowDialog() method. Can anyone help me to find a solution?
    Castle Rider
    In the jungle the mighty jungle
    The lion sleeps tonight...
    • Edited by Castle Rider Thursday, May 21, 2009 2:03 PM Formatting fix
    Thursday, May 21, 2009 2:00 PM

All replies

  • Hello; 

            You have to mark all the methods with the attribute [ComVisible(true)] which you want to access in the web.
    If you have problem in creating the object of the class, then you have to paste some code of javascript which you have written for creating the object .

    HTH
    Mahesh
    Thursday, May 21, 2009 2:14 PM
  • Mahesh,

    [ComVisible (true )] attribute doesn't helped. Below is the HTML implementation of my ActiveX:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    
    <head>
    
        <title>Untitled Page</title>
    
        <script language="javascript" type="text/javascript">
    
        
    
            function InvokeControl(){
    
                var obj = document.getElementById('fileUpload');
    
                obj.ShowDialog();
    
            }
    
            
    
        </script>
    
    </head>
    
    <body>
    
        <form id="form1">
    
        <div>
    
        <object classid="CLSID:ACA8FA62-AD76-4ee3-A2E0-C9CCC55BB98E" codebase="" id="fileUpload">
    
        </object>
    
        <input type="button" value="Click me" onclick="javascript:InvokeControl()" />
    
        </div>
    
        </form>
    
    </body>
    
    </html>
    
    

    I am calling the ShowDialog() method using the JavaScript. The error occur when I call ShowDialog().

    I have attached the screenshot of the error message I am getting. I have turned off all the security options in the Internet Explorer. Check this out

    Castle Rider
    In the jungle the mighty jungle
    The lion sleeps tonight...
    • Edited by Castle Rider Friday, May 22, 2009 2:43 AM Added the screenshot
    Friday, May 22, 2009 2:30 AM
  • hello;
              Mark your class with com visible as shown below    

    [

    ProgId("FileUploadControl.FileUpload")]
    [
    Guid("ACA8FA62-AD76-4ee3-A2E0-C9CCC55BB98E")]
    [
    ClassInterface(ClassInterfaceType.AutoDual)]
    [
    ComVisible (true)]
    public class FileUpload : IFileUpload
    {
    ...................................
    }

    goto project->properties->Build->Register for COM interop check that box and Build your dll

    Register your dll with regasm FileUpload.dll \tlb \codebase  using .Net command prompt

    Now run your web page..

    HTH
    Mahesh

    • Marked as answer by liurong luo Wednesday, May 27, 2009 3:08 AM
    • Unmarked as answer by Castle Rider Wednesday, May 27, 2009 2:35 PM
    Friday, May 22, 2009 6:08 AM
  • Mahesh,

    Its not working. Did you tried creating an COM using my code? If not, can try and see? I will we very thankful if I get a solution for this.
    Castle Rider
    In the jungle the mighty jungle
    The lion sleeps tonight...
    Friday, May 22, 2009 11:21 AM
  • Hello;

    Yes I had created a COM component using your code and it is working fine.

    If you had follow all the steps written in my post and still the object is not creating; then check the security setting of your browser. If you get that security pop up message mention in your post ; right click it and click on "allow block content" after allowing ActiveX control to run; then click the button in your webpage .

    If your browser security setting is high then browser doesnt allow the creation of ActiveX object.


    HTH

    Mahesh

    Friday, May 22, 2009 1:58 PM
  • If you get that security pop up message mention in your post ; right click it and click on "allow block content" after allowing ActiveX control to run; then click the button in your webpage .

    I love to do that, but it doesn't allow me to create it. May be I will start from the scratch again.


    Castle Rider
    In the jungle the mighty jungle
    The lion sleeps tonight...
    Friday, May 22, 2009 4:39 PM
  • Hello ;

    Try this

    ///////////////////////C# Code////////////////////////////////////////
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    using System.ComponentModel;
    namespace MyFileUploadControl
    {
        [ProgId("MyFileUploadControl.MyFileUpload")]
        [Guid("B8C3F4DB-DDCA-45b1-B431-D31CC2D2D5D6")]
        [ClassInterface(ClassInterfaceType.AutoDual)]
        [ComVisible (true)]
        public class MyFileUpload :IObjectSafety, IFileUpload
        {
            private string _filter = string.Empty;
            private bool _isMultiSelectionMode = false;
    
            public MyFileUpload()
            {            
            }
    
            #region IFileUpload Members
    
            public string Filter
            {
                get
                {
                    return _filter;
                }
                set
                {
                    _filter = value;
                }
            }
    
            public bool IsMultiSelectionMode
            {
                get
                {
                    return _isMultiSelectionMode;
                }
                set
                {
                    _isMultiSelectionMode = value;
                }
            }
    
            public void ShowDialog()
            {
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Filter = _filter;
                dialog.Multiselect = _isMultiSelectionMode;
    
                dialog.ShowDialog();
            }
    
            #endregion
    
            #region [IObjectSafety implementation]
            private ObjectSafetyOptions m_options =
            ObjectSafetyOptions.INTERFACESAFE_FOR_UNTRUSTED_CALLER |
            ObjectSafetyOptions.INTERFACESAFE_FOR_UNTRUSTED_DATA;
            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
        }
    }
    
    
    
    namespace MyFileUploadControl
    {
        [Guid("F35F054A-9A28-4ad2-9483-A0BCF15798C5")]
        [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
        public interface IFileUpload
        {
            [DispId(0)]
            string Filter
            {
                get;
                set;
            }
    
            [DispId(1)]
            bool IsMultiSelectionMode
            {
                get;
                set;
            }
    
            [DispId(2)]
            void ShowDialog();
    
        }
    }
    ////////////////////////////////////////////Make your object safe for browser////////////////////////////
    namespace MyFileUploadControl
    {
        [
            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
        };
    
    
        [
            ComImport(),
            Guid("604ADF9A-0797-4fe4-827A-A5A3A216FDD0"),
            InterfaceType(ComInterfaceType.InterfaceIsDual)
        ]
        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);
        };
    
    
    }
    
    <!--HTML CODE-->
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    
      <head>
    
        <title>Untitled Page</title>
    
        <script language="javascript" type="text/javascript">
    
    
    
          function InvokeControl(){
    
          // var obj= new ActiveXObject("MyFileUploadControl.MyFileUpload");
          var obj = document.getElementById('fileUpload');
          obj.ShowDialog();
    
          }
    
    
    
        </script>
    
      </head>
    
      <body>
    
        <form id="form1">
    
          <div>
    
            <object classid="CLSID:B8C3F4DB-DDCA-45b1-B431-D31CC2D2D5D6" codebase="" id="fileUpload">
    
            </object>
    
            <input type="button" value="Click me" onclick="javascript:InvokeControl()" />
    
          </div>
    
        </form>
    
      </body>
    
    </html>
    














    Delete all the entries asoociated with the class "MyFileUpload" from the registery before build
    Dont forget to sign your assembly before build and check the box "register for com interop"
    Making object safe will allow the browser to create the object.

    I am still monitoring this thread if you have any problem then pls revert back.

    HTH
    Mahesh
    • Marked as answer by liurong luo Wednesday, May 27, 2009 3:07 AM
    • Unmarked as answer by Castle Rider Wednesday, May 27, 2009 2:35 PM
    Saturday, May 23, 2009 7:10 AM
  • Mahesh,
    The code works fine when I tried it locally. I tried hosting it in my IIS. The problem started again. Did you tried hosting it?

    And the code is cool. It would be coolest if it works when it is hosted.


    Castle Rider
    In the jungle the mighty jungle
    The lion sleeps tonight...
    Saturday, May 23, 2009 4:42 PM
  • Hello;
            I dont think there should be a problem while hosting the code from IIS because your activex cntrl is deployed at the client machine and also javascript which uses this cntrl run at client machine. One thing you can check is the security permission assign to u r website by the browser; Make sure your site is not listed in the "Restricted Site" list.

    HTH
    Mahesh
    Monday, May 25, 2009 5:29 AM
  • Hello;
            I dont think there should be a problem while hosting the code from IIS because your activex cntrl is deployed at the client machine and also javascript which uses this cntrl run at client machine. One thing you can check is the security permission assign to u r website by the browser; Make sure your site is not listed in the "Restricted Site" list.

    HTH
    Mahesh

    The ActiveX has to be downloaded before it get deployed in the client machine. I don't think it is downloading. May be you can try with your sample code deployed in a website and let me know.

    I have set all the Internet Security options to Low only and also the my site is in Trusted Site list only.
    Castle Rider
    In the jungle the mighty jungle
    The lion sleeps tonight...
    Monday, May 25, 2009 7:06 AM
  • Hello;
           
            Check this http://www.webreference.com/javascript/961111/part01.html

    HTH

    Mahesh

    Monday, May 25, 2009 11:48 AM
  • This website also showing the same error message, I mentioned above.

    Nothing working...
    Castle Rider
    In the jungle the mighty jungle
    The lion sleeps tonight...
    Wednesday, May 27, 2009 2:36 PM
  • You r getting this error because activex dll is not register in your client machine; You have to register it using some InstallShield application.

    Your C# and javascript code are ok


    http://www.codeproject.com/KB/cs/CreateActiveXDotNet.aspx


    HTH
    Mahesh
    Friday, May 29, 2009 12:20 PM
  • OK... I will give a shot with this then.

    I hope this should work. Mahesh. Thanks for your time. I will update you once I tried it.
    Castle Rider
    In the jungle the mighty jungle
    The lion sleeps tonight...
    Saturday, May 30, 2009 9:59 AM