ASP interacts with ActiveX controls

Source: Internet
Author: User
Tags error handling interface modify client visual studio
active|activex| Interaction | control
Keywords: Asp,activex controls, digital signatures, security

The ActiveX control runs on the client. We can get some information about the client, such as IC card information, client authentication and so on. However, the ActiveX control also has many disadvantages: The client's deployment is difficult, such as the client can not download correctly, the download can not be correctly executed, download the time required to set IE security level and so on.

More often, I use COM components to extend the functionality of the ASP. Specifically, the ActiveX DLL component that is running on the server side. You can complete a call to a component using Server.CreateObject ("project name. Class Name"). There are few problems with this approach, as long as the components are exposed to public external methods. However, sometimes we have to use ActiveX controls (*. OCX) is embedded in the Web page to get information from the client. This has a lot of problems.

This is the n problem we need to solve (from simple to difficult)

1. How to get data from the server side and pass it to the ActiveX control.

1) using parameter binding when initializing

2 run time to get data

2. The ActiveX control gets information about the client and how it is delivered to the server.

3. Security

1) Digital Signature

2) using the Iobjsafe interface

All right, let's start with the example study.

This feature demonstrates how to make a simple control and embed it in a Web page for execution.

Working environment: WINXP+VB6+IE6

My IE security is set to security level-medium. All ActiveX options are set to Enabled.



Open VB6 To create a new ActiveX control project.

Project name: focx, User control name: UC1

For convenience, we use the ActiveX Control Interface Wizard ..., menu-> add-in-> add-in Manager->vb 6 ActiveX Control Interface Wizard. sure you can.



Open the ActiveX Control Interface Wizard, and next, for the available names and selected names, we default next, create a new custom member, GetInfo type: Properties, next, a common method to select the GetInfo we just added, map to the control selection Txtinfo, the member select text, The next step is done. The code window will generate something we don't need, delete, and the rest of the code is as follows:

Option Explicit

Attention Do not delete or modify the following commented lines!

' Mappinginfo=txtinfo,txtinfo,-1,text

Public Property Get GetInfo () as String

GetInfo = Txtinfo. Text

End Property



Public Property Let GetInfo (ByVal New_getinfo as String)

Txtinfo. Text () = New_getinfo

PropertyChanged "GetInfo"

End Property



' Load the property value from the memory

Private Sub usercontrol_readproperties (propbag as PropertyBag)

Txtinfo. Text = Propbag.readproperty ("GetInfo", "Text1")

End Sub

' Write attribute values to storage

Private Sub usercontrol_writeproperties (propbag as PropertyBag)

Call Propbag.writeproperty ("GetInfo", Txtinfo. Text, "Text1")

End Sub

OK, OK, we compile into Focx.ocx file, then F5 run directly, VB will open a test page. As follows:

E:\Program Files\Microsoft Visual Studio\vb98\uc1.html

At the same time, open your IE browser and see that the controls you make appear on this test page.

You can open the test page and see the CLSID, for example: 890D1028-298B-45CF-9A64-6ED5A5BACBC9

Since the VB compile time has completed registration. It is not possible to simulate a prompt that occurs when the client does not have the control installed.

We continue to

Uninstall using regsvr32 f:\csdn_vb\com+_activex\sample1\focx.ocx–u

Or

Open the registry and find it under HKEY_CLASSES_ROOT\CLSID

890D1028-298B-45CF-9A64-6ED5A5BACBC9 items, delete, of course, this way there are still items exist but not deleted clean.



At this point, browse http://yang/uc1.htm, because ActiveX in IE is set to boot, the control is displayed.

After the download OCX to where to???

Open the directory (may vary) E:\WINDOWS.0\Downloaded program files, see, focx.uc1 files are in this directory.

Principle: After the client downloads the control, it is automatically registered and placed in the directory above. Then we can open the registry to see the CLSID, hehe, or the old one.

We can right-click on the FOCX.UC1 properties and look at the relevant content, which shows all the files that OCX relies on. So, your OCX minimize the use of advanced controls or Third-party controls, otherwise, the download has some problems.

If the option for ActiveX in IE is set to disabled, the browser will appear with the dialog box "Current security setting prevents ActiveX controls from running on the page." Therefore, the page may not be displayed correctly, and this requires other ways to handle it.

The general user defaults to disabling the ActiveX option, so it is not appropriate for the user to set their own settings.
Let's take a look at how to get the data from the server and pass it to the ActiveX control.

1) using parameter binding when initializing

2 run time to get data

Open VB6 To create a new ActiveX control project.

Project name: focx, User control name: UC2

For convenience, we use the ActiveX Control Interface Wizard ..., menu-> add-in-> add-in Manager->vb 6 ActiveX Control Interface Wizard. sure you can.

Open the ActiveX Control Interface Wizard, and next, for the available names and selected names, we default next, create a new custom member, GetInfo type: Properties, next, a common method to select the GetInfo we just added, map to the control selection Txtinfo, the member select text, The next step is done. The code window will generate some we don't need, delete,

Attention Do not delete or modify the following commented lines!

' Mappinginfo=txtinfo,txtinfo,-1,text

Public Property Get GetInfo () as String

GetInfo = Txtinfo. Text

End Property

Public Property Let GetInfo (ByVal New_getinfo as String)

Txtinfo. Text () = New_getinfo

PropertyChanged "GetInfo"

End Property

Private Sub Command1_Click ()

Label2.Caption = GetInfo ()

End Sub

' Load the property value from the memory

Private Sub usercontrol_readproperties (propbag as PropertyBag)

Txtinfo. Text = Propbag.readproperty ("GetInfo", "Text1")

End Sub

' Write attribute values to storage

Private Sub usercontrol_writeproperties (propbag as PropertyBag)

Call Propbag.writeproperty ("GetInfo", Txtinfo. Text, "Text1")

End Sub

Compiled into an OCX control. Run the generated test page. The default in text is Text1

How do I pass the server-side data to OCX?

This tool is easy to find with Microsoft ActiveX Control pad.

Use this tool to open the test page directly.

Menu->edit->edit Activex control, hehe, opens a visual interface.

See, there is a property box that we can set up and set to complete after the following code:

<HTML>

<HEAD>

<title>new page</title>

</HEAD>

<BODY>

<object id= "Getclient" width=507 height=440

Classid= "CLSID:890D1028-298B-45CF-9A64-6ED5A5BACBC9"

codebase= "Http://localhost/xml/fOcx.ocx" >

<param name= "_extentx" value= "13414" >

<param name= "_extenty" value= "11642" >

<param name= "GetInfo" value= "This is OCX the client card reader Information" >

</OBJECT>

</BODY>

</HTML>

Added Id,id is the flag we use to access. And our property GetInfo, we also set the initial value.

Okay, run.

Text in the value of our set "This is OCX the client card reader information."

We change the top page to ASP, look at the code:

<HTML>

<HEAD>

<TITLE> Tornado test Page </TITLE>

</HEAD>

<BODY>

<%

Dim svalue

Svalue= "This is the information from the client card reader of OCX."

%>

<object id= "Getclient" width=507 height=440

Classid= "CLSID:56DFCA88-F5B8-4879-853B-97FE504423FD"

codebase= "Http://localhost/xml/fOcx.ocx" >

<param name= "_extentx" value= "13414" >

<param name= "_extenty" value= "11642" >

<param name= "GetInfo" value= "<%=sValue%>" >

</OBJECT>

</BODY>

</HTML>

OK, run it

Let's look at the second situation.

Get data at run time


<HTML>

<HEAD>

<TITLE> Tornado test Page </TITLE>


<%

Dim svalue

Svalue= "This is the information from the client card reader of OCX."

%>

<script Id=clienteventhandlersjs language=javascript>

<!--

function Button1_onclick ()

{

getclient.getinfo= ' <%=sValue%> ';

}

-->

</SCRIPT>

</HEAD>

<BODY>

<object id= "Getclient" width=507 height=440

Classid= "CLSID:56DFCA88-F5B8-4879-853B-97FE504423FD"

codebase= "Http://localhost/xml/fOcx.ocx" >

<param name= "_extentx" value= "13414" >

<param name= "_extenty" value= "11642" >

</OBJECT>

<input id=button1 Type=button Value=button name=button1 language=javascript >

</BODY>

</HTML>

After running, click the button to see the effect.
Questions raised:

ActiveX controls get information about the client and how to deliver to the server???

One way to do this is to get the value and submit it using the Get,post method, which is probably the most common.

Is there a better way?

Can you make a request to the server directly from the control and get the data?



Open VB6 To create a new ActiveX control project.

Project name: focx, User control name: UC3

Add 1 buttons, 2 text boxes

The code is as follows:

Option Explicit

Private Sub Command1_Click ()

' Note that there are some differences in the wording of strings and numbers

' Use AsyncRead to send your request over HTTP

Usercontrol.asyncread "http://yang/xml/activex.asp?s1=" & Text1.Text & "", Vbasynctypebytearray

Usercontrol.asyncread "http://yang/xml/activex.asp?s1=" & Text1.Text, Vbasynctypebytearray

End Sub

The ' AsyncReadComplete event is used to receive and analyze from an ASP page.

' When the container has just completed an asynchronous read request, the event occurs?

The value in ' Asyncprop specifies an asynchronous data read request that has been completed.

' It matches the data in the previous AsyncRead method call.

' The AsyncReadComplete event procedure should contain error handling code because the error state terminates the download.

' If this happens, an error occurs when accessing the Value property of the AsyncProperty object.

Private Sub Usercontrol_asyncreadcomplete (Asyncprop as AsyncProperty)

On Error GoTo Errhandle

Text2.text = ByteArrayToString (asyncprop.value)

Errhandle:

Err.Raise 601, "error occurred with asynchronous read", Err.Description

End Sub

' Converts a byte array into a string

Public Function bytearraytostring (Bytarray () as Byte) as String

Dim SAns as String

SAns = StrConv (Bytarray, Vbunicode)

ByteArrayToString = SAns

End Function

Let's take a look at http://yang/xml/activex.asp this file.

<%

Dim str

Str=request ("S1")

If str= "name" Then

Response.Write ("Tornado")

ElseIf str= "Age" then

Response.Write ("26")

Else

Response.Write ("No information available to return")

End If

%>

Very simply, use request to accept parameters, response returns.



This example can be further processed by passing the server name to the control in the form of a property.

For example: <param name= "ServerName" value= "192.168.0.1" >

Then you get it in the program, you can use it, such as:

Usercontrol.asyncread "http://192.168.0.1/xml/activex.asp?s1=" & Text1.Text & "", Vbasynctypebytearray

Then you get it in the program and you can handle it flexibly.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.