How to compile ASP into DLL this article mainly turns ASP code into a component

Source: Internet
Author: User

This article describes how to compile ASP into DLL by turning ASP code into components. Developers not only speed up ASP, but also protect their own code.
Next, we will compile a very simple component, focusing on how to develop the DLL component, rather than its complex code! All of this depends on your future efforts.

Server Components

First, the server components should be different from the client components. the client components are transmitted over the network and work with HTML. and can only be used on IE. however, the server component runs on the server, and it performs various operations on the server. therefore, all browsers can enjoy it, relying on servers rather than browsers.

When IIS is requested to execute an ASP program, it first finds the code between the <%> labels in the ASP file, and execute it (it can also be code between <scriptrunat = server> </script> ). if this ASP program was previously called, it will return HTML code to the user using the compiled program in the memory. If not, it will re-compile. here, ASP is a little more speed advantage than CGI, Because CGI uses a thread for every request. in this way, the server resources are greatly consumed.

I don't want the program you write to run on IIS !?! Now you can do it! When using VB5 (of course it is VB6 now), you can create a dynamic1_libraries (DLL file), which can be run directly on IIS (if there is an asp file for request ).

System and software requirements

You need a 32-bit operating system to run ASP. Of course, you also need to install IIS or PWS. Our program is developed in the Windows 95 + PWS + VB5 environment.

Let's get started.

Start your VB and select the ActiveX icon. This icon can be found in the new project! VB provides a default project name (project1) and Class Name (class1 ). we will change both names. before renaming, make sure that we have the MicrosoftActiveServerPagesObjectLibrary, which is very useful in our program. select "project" from the menu, and then select "Reference". The "Reference" window appears.
Select MicrosoftActiveServerPagesObjectLibrary.

Name projects and Classes

Now let's name project1 and class1 based on our hobbies! Naming them is also very important. We will use this project name and class name to create an instance for this component in the future! The following is a detailed description.

How can I change my name!
Change the project name to Exmaple, and the class name is Helloword.

How to use projects and Classes

Now we have our own project (Example1) and Class Name (HelloWorld ). in the future, we will use their names in ASP code to reference this component. in ASP, We reference it as follows:

SetObjReference = Server. CreateObject ("ProjectName. ClassName ")

References to our project are:
SetObjReference = Server. CreateObject ("Example1.HelloWorld ")
Now we can use ObjReference to call the function we created in the component. We will write a SayHello subroutine. The code we run is as follows:

<%
SetObjReference = Server. CreateObject ("Example1.HelloWorld ")
ObjReference. SayHello
%>

To use ASP in the Helloword class, you must write an OnStartPage
The sub-function is as follows:

PublicSubOnStartPage (PassedScriptingContextAsScriptingContext)
SetMyScriptingContext = PassedScriptingContext
EndSub

 

Now, no matter when the user accesses an ASP file with this component, IIS will send ScriptingContext to our object for use. this ScriptingContext includes all ASP methods and attributes. this allows us to access all ASP objects. see the following code:

PublicSubOnStartPage (PassedScriptingContextAsScriptingContext)
SetMyScriptingContext = PassedScriptingContext
SetMyApplication = MyScriptingContext. Application
SetMyRequest = MyScriptingContext. Request
SetMyResponse = MyScriptingContext. Response
SetMyServer = MyScriptingContext. Server
SetMySession = MyScriptingContext. Session
EndSub

In the future, we can use MyApplication in VB to replace the Application in ASP. Likewise, we can replace the Request, Server..., but we want to declare these variables before OnStartPage:

PrivateMyScriptingContextAsScriptingContext
PrivateMyApplicationAsApplication
PrivateMyRequestAsRequest
PrivateMyResponseAsResponse
PrivateMyServerAsServer
PrivateMySessionAsSession

 

Use ASP objects

Our variables can now be used like standard ASP objects! For example, we often use Request. form () in ASP to collect the data for submitting forms. Now we implement this function in our VB. The Code is as follows:

Implemented in ASP:
<%
MyTempVariable = Request. Form ("userName ")
Response. Write ("youentered" & MyTempVariable & "asyourusername ")
%>

Implemented in VB:

MyTempVariable = MyRequest. Form ("userName ")
MyResponse. Write ("youentered" & MyTempVariable & "asyourusername ")

 

By using MyResponse instead of Response, we can use all Response methods. Of course, the name of MyResponse can be obtained at will, and you can even get Response.
Another thing we should note is that we have to write the OnEndPage subfunction in the class we created. This OnStartPage is the opposite! OnStartPage is the object to be created, and OnEndPage is the object to be destroyed.

 

PublicSubOnEndPage ()
SetMyScriptingContext = Nothing
SetMyApplication = Nothing
SetMyRequest = Nothing
SetMyResponse = Nothing
SetMyServer = Nothing
SetMySession = Nothing
EndSub

 

SayHello Method

Let's create a sub-function to display "HolleWorld". This SayHello method is only a sub-function in the HelloWorld class. We will use the following method in ASP later.

<%
SetObjReference = Server. CreateObject ("Example1.HelloWorld ")
ObjReference. SayHello
%>

 

The SayHello program is very simple!

PublicSubSayHello ()
MyResponse. Write ("HelloWorld ")
EndSub

 

Now the compilation of a small component is complete, and the rest of the work is to compile this component. Save it in the "project" menu and get any name. Let's use Exmaple1.vbp! Then select "makeexmaple1.dll" in the menu and compile it into a DLL file. A component is actually complete!

Note: after this component is compiled, you must first turn off your PWS and then compile this component. Otherwise, VB will tell you that some components are in use.

Use our own components in ASP.

When you have corrected the compilation error and successfully compiled the example1 project, now you have to come up with your favorite HTML editor to write down the following statement and save it as an ASP file.

<HTML>
<HEAD>
<TITLE> Example1 </TITLE>
</HEAD>

<BODY>

<%
SetObjReference = Server. CreateObject ("Example1.HelloWorld ")
ObjReference. SayHello
%>

</BODY>
</HTML>

The result is displayed after running:

HelloWorld

Register Components

Register to register the component. After registration, your component will appear in the windows/system directory of Win95/Win98. The following is an example of registration:

Regsvr32.exeC:/wwwroot/Example1/Example1.dll

In your system, vbwill automatically give you registration, so you rarely use regsvr32.exe

Here, we only wrote a very small component. You can write your own larger component, and you can also use many widgets in VB.

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.