Thursday, May 29, 2014

Getting the System Local IP address from the ASP.Net Using the ActiveX Control

Getting the System Local IP address from the ASP.Net Using the ActiveX Control

Step 1:- Creating the one empty windows project c#
Open visual studio à File àNew àProject à In installed templates select the Windows à
And select Class Library àName it as “SampleActiveXControl “ and click ok à

Step 2:-Go to solution explorer à Right click the sampleActiveXControl  project àAdd à
New Item àSelect Windows Forms in the installed template àand select User Control à
And name it as “ActiveXUserControl .cs“   and click add 
àFor Identification change the color of the user control background to blue

Step 3:- Creating the SNK (Strong Name Key) file
For Reference go through this link

Step 4:- Click Start àAll programs àMicrosoft visual studio 2010 à Visual studio Tools à
Right click the Visual studio Command prompt àRun as Administrator à

Type                      cd<Space>c:\<Enter>
                                sn<Space>-k<Space> ActiveXUserControl.snk<Enter>

SNK file is created with the name of “ActiveXUserControl.snk” in the c: folder


SNK file is look like



Step 5:- Cut that ActiveXUserControl.snk file and past it in to SampleActiveXControl project

Step 6:- Go to ActiveXUserControl.cs user control in the code file write this code for getting the local IP address

using System.Net;


        public string GetLocalIpAddress()
        {
            try
            {
                string strHostName = "";
                strHostName = System.Net.Dns.GetHostName();

                IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);

                IPAddress[] addr = ipEntry.AddressList;

                return addr[addr.Length - 1].ToString();
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
                //MessageBox.Show("Error in IP");
            }

        }

Step 7:- Go to solution explorer àRight click the project à Properties à

In the Application Tab àClick Assembly Information à Assembly Information window

will open àCheck the Make assembly COM-Visible check box à OK








Step 8:- Go to Build tab àIn output Section à

Check the Register for COM interop checkbox





Step 9:-Go to Signing Tab àCheck the Sign the assembly checkbox

And choose a strong name key file



Step 10:-Creating the Windows setup for this project

Right click the solution explorer àAdd àNew project à In the installed templates à

Other project Types àSetup and deployment àVisual studio Installer à

Setup Project Template àName it as à EXESampleActiveX

Go to solution explorer àRight click the EXESampleActiveX project àAdd àProject
OutputàSelect Primary output and click ok àBuild the EXESampleActiveX project


Step 10:- For accessing this ActiveX Control from the we we have to do these steps

Add the Web project to accessing this ActiveX control

Right click the Solution Explorer àAdd à New Project àSelect Asp.net Web Application

Name it as WebApplication1 àOK

Step 11:- Install the project EXESampleActiveX

Step 12:- Open the WebApplication1 project

àGo to Default Master Page

Create the id for the <Form> Tag

<form runat="server" id="SampleForm">

Go to Default.Aspx Page and write the below Code

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <script type="text/javascript">
        function GetIPAdd() {
            var IP = SampleForm.ObjectIP.GetLocalIpAddress();
            alert(IP);
        }
        //HKEY_CLASSES_ROOT\CLSID\{39428ECB-0122-3499-8663-96171159E2C3}
        //HKEY_CLASSES_ROOT\CLSID\{C16564FA-33CF-345D-A12A-E91BAF434A78}
        //HKEY_CLASSES_ROOT\TypeLib\{7112EEA6-FA5B-4AB9-AFFF-DC1F6C21CE0E}
        //HKEY_CURRENT_USER\Software\Classes\CLSID\{39428ECB-0122-3499-8663-96171159E2C3}
        //HKEY_CURRENT_USER\Software\Classes\CLSID\{C16564FA-33CF-345D-A12A-E91BAF434A78}
        //HKEY_CURRENT_USER\Software\Microsoft\Installer\Products\DD22616C4AA9ECA4FA90F970251F6458
    </script>
    <h2>
        Welcome to ASP.NET ActiveX Control Accessing</h2>
    <h3>
        Getting Local Ip Address
    </h3>
    &nbsp; &nbsp;
    <asp:Button ID="Button1" runat="server" Text="Get Local IP" OnClientClick="GetIPAdd();" />
    &nbsp;
    <object id="ObjectIP" type="application/x-oleobject" classid="CLSID:C16564FA-33CF-345D-A12A-E91BAF434A78">
    </object>
</asp:Content>





Step 13:- Find the CLSID for that user control is

classid="CLSID:C16564FA-33CF-345D-A12A-E91BAF434A78">

Step 14:- Steps to find the CLSID in the system

After Install the Setup project

StartàRunà Type àregeditàEnter à Copy the ActiveX control project name “SampleActiveXControl”  find in the registry Edit




Click Find Next à For go to next find Click F3 button

Like that fin all the keys in the registry

and make track like


//HKEY_CLASSES_ROOT\CLSID\{39428ECB-0122-3499-8663-96171159E2C3}
        //HKEY_CLASSES_ROOT\CLSID\{C16564FA-33CF-345D-A12A-E91BAF434A78}
        //HKEY_CLASSES_ROOT\TypeLib\{7112EEA6-FA5B-4AB9-AFFF-DC1F6C21CE0E}
        //HKEY_CURRENT_USER\Software\Classes\CLSID\{39428ECB-0122-3499-8663-96171159E2C3}
        //HKEY_CURRENT_USER\Software\Classes\CLSID\{C16564FA-33CF-345D-A12A-E91BAF434A78}
        //HKEY_CURRENT_USER\Software\Microsoft\Installer\Products\DD22616C4AA9ECA4FA90F970251F6458


And Copy one CLSID and past in the project and  check

1.  <object id="ObjectIP" type="application/x-oleobject" classid="39428ECB-0122-3499-8663-96171159E2C3">
</object>
 Error:-- Out put fails
While running this project u getting error like



Correct Output is


I tried with another CLSID  C16564FA-33CF-345D-A12A-E91BAF434A78

<object id="ObjectIP" type="application/x-oleobject" classid="CLSID:C16564FA-33CF-345D-A12A-E91BAF434A78">
    </object>



Click Local ip button and Check




Actually the code will work from installed in the system but this is the web application loading from the servers

By using this we can access all the local system controls from the ASP.net c#




Important :-

One more important steps

This ActiveX Controls will work only in the IE will not Work in the other browsers

For Firing this activex control we have to change some options in the Internet Explorer(IE)

Go to internet Options



In Security Tab àClick Trusted Site àClick Sites buttons



Type the your URL ip address

My ip is localhost for the reason I  adding httP://localhost

Close

Click Custom Levels




Go to ActiveX Controls and Plug-ins and enable all the radio buttons



Ok


All are working fine any queries please comment here



For any clarifications or queries please don’t hesitate to call or Email 

Phone :- + 91-9492179390


Email :- lannam@technobrainltd.com (or) lokeshtec@gmail.com 


No comments:

Post a Comment