ASP advanced: The use of modules, classes, ActiveX and APIs in VB

Source: Internet
Author: User
Tags include net reference vbscript class
Active|activex these days made a point C/s structure of the program design, because the ASP used more skilled, so VB6.0 naturally become my preferred development tool. In the learning process, I combined my experience to summarize some of the advanced application of VB, and realize the benefits of this application to us.

Modules (module)

In VB, the module will give priority to implementation, in fact, the module is like a global process and function calls. This is a preliminary way to improve the reusability of the code. I would like to have experienced ASP (VBScript, in the future only VBScript to write ASP program) programmers have used <!--#Include file= ""--> this statement, which is more convenient to improve the reusability of the Code, In VB, the module completes this function. It is worth noting that the Sub Main () procedure can be defined in a module and that the program can be entered from the main () process in the startup. This is a bit like the main () function in c,c++. However, the code reusability level of the module is still on the procedure, function call.

Second, class module

In VB, class modules can be built. class modules are similar to those in ASP. I think experienced ASP programmers should have their own class library. How do we refer to our class library in ASP? Yes, or include, as long as the class file is included in the line. As long as the Set xxx= new ClassName on the call. This is the same with the ASP. What are the benefits of using class modules? Because classes allow you to define common methods and procedures, you can achieve a higher level of reusable lines of code.

Third, the ActiveX technology

Perhaps you will think that, whether using modules or class modules, many functions may be used in the next project. If this time we need those features, you can directly to the class module and module code into the VB project. In ASP, we simply make the class code into a file, And then in the other application include in the line. But there is one problem, which is the encapsulation line of the code. Because whether it's a class module or a module, or a class of. asp file written in ASP, the code can be viewed and modified. Is there any way to make the code invisible? The answer is yes, This is the ActiveX control technology. The advantage of ActiveX is that you can write ActiveX controls in different languages, as long as it conforms to the agreed specifications, it can then be invoked by other languages. And the code is compiled. This will improve the speed of the ASP. Okay, cut the crap, let's take a look at a practical example:

1. Create an ActiveX DLL project first, then write the name Testdll. The code in the class module is:

Option Explicit
Private Mdbl_augend as Double
Private Mdbl_addend as Double

Public Property Get Augend () as Double
Augend = Mdbl_augend
End Property

Public Property Let Augend (ByVal Vnewvalue as Double)
Mdbl_augend = Vnewvalue
End Property

Public Property Get Addend () as Double
Addend = Mdbl_addend

End Property

Public Property Let Addend (ByVal Vnewvalue as Double)
Mdbl_addend = Vnewvalue
End Property

Public Function Plus ()
Plus = Augend + addend
End Function

2. File-> generate TestDll.dll. So we get a file called TestDll.dll, and this is an ActiveX control. This control has 2 properties, a function. You can calculate the and 2 properties.

3.VB call TestDll.dll. Create a new EXE project. Project-> Reference-> Browse, select TestDll.dll file, and then drag a button, write the following code in the button:

Private Sub Command1_Click ()
Set objsum = New Sum
Objsum.augend = 10.52
Objsum.addend = 382.41
result = Objsum.plus
MsgBox result
Set objsum = Nothing
End Sub
The code doesn't have to be explained. You can see a pop-up dialog box showing 2 properties of the and.

3.ASP invokes the TestDll.dll file. ASP calls the DLL file and VB is similar. When VB calls, we want to add a reference to locate the DLL file. The ASP needs to register before it can be invoked. So first create a Setup.bat file, which writes the following code: Regsvr32.exe TestDll.dll If this bat file is not under TestDll.dll's sibling directory, you need to add the TestDll.dll absolute path. Or knock Regsvr32.exe directly in the running E:\website\work\dll\ TestDll.dll. So TestDll.dll is registered, the following we write a file to test:

<%
Dim Objsum
Set objsum = Server.CreateObject ("Testdll.sum")
Objsum.augend = 10.52
Objsum.addend = 382.41
result = Objsum.plus
Response.Write Result
Set objsum = Nothing
%>

What about the Web page that shows 392.93? So the ASP will call this TestDll.dll file.

In fact, there are many advantages of invoking ActiveX control in ASP, if it is a large B/S software project, this can encapsulate business logic inside ActiveX, And the ASP as long as the call on the line. The only drawback is that you need to register the component. However, we have a workaround, we can support the class above ASP5.0, we could use VBScript class to replace the component, so we can implement encapsulation, but only slow in performance. But now. NET technology solves this problem. Because ASP.net is based on the. NET Framework, DLL files written in. Net do not need to be registered. Specific later can be out of the. NET MVC pattern of the article with you discuss.

Four, API call.

VB is the powerful place can call WINAPI, so the original VB can not complete the function through the API to complete. To invoke the API, you need to refer to it first as well. VB provides an API show gadget for us to use. This saves a lot of trouble with invoking the API. To invoke the API, first build the module, declare the API in the module, and I'll write a little program, is to have the window always appear on top. The calling API code is as follows:

Declare Function setwindowpos Lib "user32" (ByVal hwnd as Long, ByVal hwndinsertafter as Long, ByVal x as Long, ByVal y as Long, ByVal CX as Long, ByVal Cy as Long, ByVal wflags as long) as long


' Constant declaration
Global Const Swp_hidewindow = &h80
Global Const swp_noactivate = &h10
Global Const swp_nocopybits = &h100
Global Const swp_nomove = &h2
Global Const Swp_noownerzorder = &h200
Global Const Swp_noredraw = &h8
Global Const swp_noreposition = Swp_noownerzorder
Global Const swp_nosize = &h1
Global Const Swp_nozorder = &h4
Global Const Swp_showwindow = &h40
Global Const Hwnd_bottom = 1
Global Const hwnd_broadcast = &HFFFF&
Global Const hwnd_desktop = 0
Global Const hwnd_notopmost =-2
Global Const hwnd_topmost =-1
Global Const hwnd_top = 0
Global Const Flags = swp_nomove Or swp_nosize

Then we add the following code to the form:

Private Sub Form_Load ()
Dim Success as Long
Success = SetWindowPos (Me.hwnd, hwnd_topmost, 0, 0, 0, Flags)
End Sub

Run down to see if the form is always at the front?

After the study of VB found that, in fact, VB still provides some packaging technology. For example, modules, class modules, and ActiveX, and VB can invoke the API to make up for its own deficiencies. The main thing is that VB is easy to learn, but also provides a lot of database operations control.

Because I use VB less, there are errors in the place also please point out. Http://www.ncycglass.com/blog interested to see my blog, together to explore.



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.