Creating an ActiveX Control in Visual Studio 2005

Published Wed, Aug 16 2006 6:26 | Paul June

There have been misconceptions on technologies upon the arrival of .Net. Many programmers believe that the arrival of .Net means the scrapping of all previous technologies. Or in other word the COM technology.

 

COM or ActiveX technology has been around us for very long. So it’s not quite easy to scrap a long-time proven technology. Some also believed that the .Net technology replaces the ActiveX in all applications. Well, sadly this is not true. I was awakened by this reality when I handled a code conversion project. It was a conversion from Visual Basic 6 to Visual Basic 8 (VB2005). The Visual Basic 6 project was a dll-based project with an ActiveX Document as its user interface. As we all know, user documents are controls that is embedded to a web page.

 

That’s when my problem began. What is ActiveX Document in .Net? What is its counterpart? Well, upon goog-ling a couple of hours to find the answer, in vain as I may add, it suddenly struck me. Instead of finding a counterpart, why not just re-use an old technology (ActiveX)? I must find a way to make my .Net object visible to COM applications or other ActiveX objects.

 

This may sound very complex, but the solution that I found was pretty easy. I’ll do this step by step.

 

Step 1: Create a Class Library Project

 

After creating the project, delete Class1.cs coz we don’t need the file. What we need though is a User Control to contain our controls.

 

Step 2: Add a User Control

 

This user control would host the UI that we would be placing in our Web Page. After adding, lets add a TextBox control in our user control.

 

Step 3: Create an Interface

 

Since we are talking to a COM application, we must expose certain properties that the browser could use to modify parts of our component. This is exposed through our interface.

 

public interface IComWrapper {

     string TextInTextBox { get; set; }

}

 

In our case, lets create a TextInTextBox string property. After doing that, implement the interface in our User Control.

 

public partial class myControl : UserControl, IComWrapper  {

    private string _userText = "";

      

 

    public myControl() {

        InitializeComponent();

    }

 

    #region IComWrapper Members

 

    public string TextInTextBox {

        get {

            return _userText;

        }

        set {

            _userText = value;

            textBox1.Text = value;

        }

    }

    #endregion

}

 

Step 4: Embed User Control in a browser

 

Like its ActiveX counterpart, embedding a UserControl in a webpage is done through the usage of the object tag:

 

<object id="myControl1" name="myControl1" classid="ActiveXdotNet.dll#ActiveXdotNet.CustomerMaster"

    style="width:100%; height:100%; background-color:ButtonFace; font-family:Arial; font-size:x-small">

</object>

 

As you may have noticed, the difference between using ActiveX and UserControl is its classid parameter. Instead of containing a GUID for our ActiveX, it contains the dll name followed by a # and the name of the namespace and the class.

 

Step 5: Access Browser

 

By this time, you have successfully embedded the UserControl in a webpage. But in order to see the effect you must place in a single folder the dll and your webpage and access it through your IIS. Just a reminder, don’t access your webpage by double-clicking it in your Windows Explorer for it will not show your UserControl. Access it with its full path in IIS (http://localhost/ActiveXdotNet/Default.htm).

 

Tricks

 

You can call your exposed properties by using a plain javascript. Use the Id of the object in your object tag as if it were the object itself. So when you do this in your web page:

 

myControl1.TextInTextBox = “testing”;

 

The textbox in your user control would change.

 

Also, sometimes we would want to manipulate the webpage inside our UserControl. To do this we must expose a Window object property. This would serve as the container of the window object in javascript. After that let’s cast the value to IHTMLWindow2 so that we can access the browser’s functionalities.

 

There you are! You have successfully built an Active-like control in your web paeg.

Comments

# cuongdv said on August 29, 2006 9:39 PM:

that's so cool
thanks for your article

# sharpfun23 said on September 1, 2006 2:10 AM:

Would you please post your source code
thanks.

# marco said on September 16, 2006 5:28 AM:

I'm trying to find a vb2005 source code about creating active x and use it in html page, but I haven't success

# marco said on September 16, 2006 5:29 AM:

http://www.c-sharpcorner.com/UploadFile/dsandor/ActiveXInNet11102005040748AM/ActiveXInNet.aspx?ArticleID=99d9681d-84de-4d64-9c85-9ae9c15a4ee7

# sanga said on September 21, 2006 6:52 PM:

Right now, my problem is that the activeX can only be seen locally (at the PC that develop the activeX).

Do you also happen to know how to create the .cab file to install the activeX?

# sanga said on September 23, 2006 12:31 AM:

Sorry 'bout my previous post. It was silly of me to include non-native objects inside my control. Once they're out, the object works just fine.

I wonder why, or how to include those objects, though.

# Joe said on October 26, 2006 1:25 AM:

this is weird. I did this but it doesn't show up on the page. Only shows where it should be.

# Blu_Fire said on November 13, 2006 9:04 PM:

I had the same problem. I then copied the page to a different web server and it worked. Must be some setting I'm not seeing. Both the server and client had the .net framework so that wasn't it.

Anyway I would really like to see an elaboration of the control accessing the webpage object so I can call a form.Submit() and set field values from within the control.

# Boney said on November 22, 2006 2:49 AM:

Hi,

How  can  i transfer data ....... Whenever i am trying to save some information to DB i am getting following error.....

System.Security.SecurityException: Request for the permission of type 'System.Data.OleDb.OleDbPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

  at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)

  at System.Security.PermissionSet.Demand()

  at System.Data.Common.DbConnectionOptions.DemandPermission()

  at System.Data.OleDb.OleDbConnection.PermissionDemand()

  at System.Data.OleDb.OleDbConnectionFactory.PermissionDemand(DbConnection outerConnection)

  at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)

  at System.Data.OleDb.OleDbConnection.Open()

  at RDSActiveX_BL.GridControl.OpenDataBaseConnection()

  at RDSActiveX_BL.GridControl.ExecuteNonQueries(String sSql)

  at RDSActiveX_BL.GridControl.btnSave_Click(Object sender, EventArgs e)

  at System.Windows.Forms.Control.OnClick(EventArgs e)

  at System.Windows.Forms.Button.OnClick(EventArgs e)

  at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)

  at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)

  at System.Windows.Forms.Control.WndProc(Message& m)

  at System.Windows.Forms.ButtonBase.WndProc(Message& m)

  at System.Windows.Forms.Button.WndProc(Message& m)

  at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)

  at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)

  at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

The action that failed was:

Demand

The type of the first permission that failed was:

System.Data.OleDb.OleDbPermission

The Zone of the assembly that failed was:

Intranet

************** Loaded Assemblies **************

mscorlib

   Assembly Version: 2.0.0.0

   Win32 Version: 2.0.50727.42 (RTM.050727-4200)

   CodeBase: file:///C:/WINNT/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll

----------------------------------------

System

   Assembly Version: 2.0.0.0

   Win32 Version: 2.0.50727.42 (RTM.050727-4200)

   CodeBase: file:///C:/WINNT/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll

----------------------------------------

RDSActiveX_BL

   Assembly Version: 1.0.0.0

   Win32 Version: n/a

   CodeBase: http://localhost:1351/WebSample/RDSActiveX_BL.dll

----------------------------------------

System.Windows.Forms

   Assembly Version: 2.0.0.0

   Win32 Version: 2.0.50727.42 (RTM.050727-4200)

   CodeBase: file:///C:/WINNT/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll

----------------------------------------

System.Drawing

   Assembly Version: 2.0.0.0

   Win32 Version: 2.0.50727.42 (RTM.050727-4200)

   CodeBase: file:///C:/WINNT/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll

----------------------------------------

System.Data

   Assembly Version: 2.0.0.0

   Win32 Version: 2.0.50727.42 (RTM.050727-4200)

   CodeBase: file:///C:/WINNT/assembly/GAC_32/System.Data/2.0.0.0__b77a5c561934e089/System.Data.dll

----------------------------------------

System.Xml

   Assembly Version: 2.0.0.0

   Win32 Version: 2.0.50727.42 (RTM.050727-4200)

   CodeBase: file:///C:/WINNT/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll

----------------------------------------

Accessibility

   Assembly Version: 2.0.0.0

   Win32 Version: 2.0.50727.42 (RTM.050727-4200)

   CodeBase: file:///C:/WINNT/assembly/GAC_MSIL/Accessibility/2.0.0.0__b03f5f7f11d50a3a/Accessibility.dll

----------------------------------------

Microsoft.mshtml

   Assembly Version: 7.0.3300.0

   Win32 Version: 7.0.3300.0

   CodeBase: file:///C:/WINNT/assembly/GAC/Microsoft.mshtml/7.0.3300.0__b03f5f7f11d50a3a/Microsoft.mshtml.dll

----------------------------------------

System.Transactions

   Assembly Version: 2.0.0.0

   Win32 Version: 2.0.50727.42 (RTM.050727-4200)

   CodeBase: file:///C:/WINNT/assembly/GAC_32/System.Transactions/2.0.0.0__b77a5c561934e089/System.Transactions.dll

----------------------------------------

************** JIT Debugging **************

To enable just-in-time (JIT) debugging, the .config file for this

application or computer (machine.config) must have the

jitDebugging value set in the system.windows.forms section.

The application must also be compiled with debugging

enabled.

For example:

<configuration>

   <system.windows.forms jitDebugging="true" />

</configuration>

When JIT debugging is enabled, any unhandled exception

will be sent to the JIT debugger registered on the computer

rather than be handled by this dialog box.

can anyone help me......

Tks in Advance...........

# Edward said on December 10, 2006 11:16 PM:

I can't get javascript and activeX to interoperate...?

I'm using IE7 with .NET 2.0

# Paulo Bucho said on December 19, 2006 12:39 PM:

Hi!

I did all the code and noting happen.

Can you send me the sample source?

# Paul June said on January 11, 2007 12:59 PM:

HI,

Sorry for the super delay of my reply. I'm not monitoring this blog anymore. For further inquiries, I suggest you visit my new blog,

http://webmag.mvps.org

and post your questions there so that I can attend to it.

Thanks,

Paul June A. Domag

# ... said on March 1, 2007 12:50 AM:

Very nice site! Good work.

# anton said on August 23, 2007 7:07 AM:

the web page must also be registered URL in the frameworks runtime security policy Machine

# niranjan said on November 9, 2007 1:16 AM:

Thanks for sharing this article. Iam trying to get the value from the Control. How to do it. Any idea will be greatly usefull....thanks a tons

# elango n said on December 18, 2007 6:48 AM:

I am trying to get the value displayed in the User Control but not.The Control is visible but after giving the value to the Textbox in the page it is not transferred to the Control.Note :- Working in VS 2005 using C#.Thanks in Advance...

# Sara said on January 8, 2008 4:59 PM:

I got the same problem as elango n. Don't know why.

# Djbmcccw said on July 14, 2009 4:54 PM:

USr3ty

# Venture Digital said on July 22, 2009 11:17 AM:

Thanks for this article!

# Ray said on July 28, 2009 12:55 PM:

The Active X built by Visual Studio 2005 can successfully render on the web page, but I am NOT able to update text inside textBox1 from Javascript.  However, if I built the project using Visual Studio 2003, I am able to do so.

Do they have any security configuration I need to set in Visual Studio 2005 while compiling the Active X project?

# Nismoto said on August 11, 2009 11:29 AM:

Interesting. Yours looks exactly like this one that precedes yours by 3 years.

www.c-sharpcorner.com/.../ActiveXInNet.aspx

Leave a Comment

Name:  
Website: