Introduction to making ASP server controls using vc++6.0

Source: Internet
Author: User
Tags file upload functions implement visual studio
c++| Server | The programmer who writes the Web page with ASP, must be Vbsript statement "Set conn=server.object (' ADODB. Connection ') "is very familiar. The statement uses ASP's server object to create a database control that provides a range of methods and properties that can help us implement database operations conveniently in the ASP. Similarly, in visual InteDev6.0, we can also see some of the ASP's built-in components such as "Scripting.FileSystemObject". However, in some cases, the use of these is not enough or complex, and the ASP's script language is far less powerful than the C + + language (such as the establishment of a linked list, B + tree structure, etc.), which makes us think that we can use a good data structure of the C + + language in the Although CGI,ISAPI can be used as a pioneer in the development of the Web page, but we know that the development cycle of CGI,ISAPI is very long, and not with the Web page, poor visibility, writing, debugging difficulties; In terms of stability, because ISAPI is the way of dynamic linking, so in the event of a problem in execution, will cause the Web server to be paralyzed together. And the ASP just can make up these shortcomings.
This shows that if you can learn to write ASP components can be combined with the strengths of the ASP to write a powerful web page. Unfortunately, the author in the market rarely see a special talk about how to use VC + + to make ASP components of the book. General VC Books only talk about some writing Windows Windows program or plus write ordinary dynamic link library. and ASP books is only talk about ASP built-in components of the use of methods. In fact, if you want to really master the mechanism of writing ASP components, it involves a lot of knowledge of COM technology. And some of the books on COM technology are very esoteric and rarely combine useful examples. To this end, I would like to take this article in the simplest way to introduce the process of writing. The so-called introduction, is the description will not involve the specific details of COM knowledge, only give the implementation method.
I. Establishment of the project
Open vc++6.0, select the Alt COM AppWizard in new projects, and enter a good project name (such as MyStudio), remembering that the project name is part of the ASP component name (which can actually be modified, but cumbersome), and select Server Type on the next page As a DLL, and consider a tick before support MFC (presumably with many people who are programming with MFC). Finish by finish. There are four standard export functions registered DLLs in the generated code, and we don't have to care.

Ii. Creating COM objects (new ATL Active Template Library classes)
Select Insert New class on the menu, and create a new ATL class, such as Cmycomponent, you will find a imycomponent in the interface table, and the methods and properties for creating the interface can be used in ASP. Remove the aggregatable option, we don't need it. Keep the rest unchanged.

Third, add interface method
In ClassView, right-click Imycomponent, and in the menu that appears, press Add method. As shown in the figure, method name can be written as Initmycomp. The parameters of the method are written in parameters such as [In]int number1,[out,retbal]int* Number2. What we want to specify here is the wording of the parameter.

1. For incoming parameters, you must precede the parameters with [in], and then follow such as int n or float f. and must be stated before each incoming parameter.
2. For the return of the calling method, it is no longer the type before the method name (because all of these methods return HRESULT, which indicates whether the call succeeded), so using outgoing arguments, you must add [Out,retval] before the parameter, and then follow the example int* N or float* F, and so on, if the value of an int or float type is returned. And each method is limited to returning only one such parameter. The function body can be written as "*number2=number1;" to indicate that the input parameter is an outgoing parameter.

3. For the most commonly used string parameters here we use BSTR Str instead of the usual char* or CString, so the arguments need to be written [In]bstr str or [out,retval]bstr* str. Special attention needs to be paid to the conversion with CString. As a new function StrConv ([in]bstr bstr1,[out,retval]bstr* bstr2), it can be written in the function body as follows:
CString str;
STR=BSTR1;
Str=str. Left (str. GetLength ()-1);
*bstr2=str. AllocSysString ();
4. For methods that want to take an ASP object as a parameter (such as a Request object), you can use [In]iunkown *piunk, in the body of the function, the following statement: (Please refer to the contents of the relevant classes in MSDN)
irequest* pirequest;
HRESULT hr = Piunk->queryinterface (iid_irequest, reinterpret_cast<lpvoid*> (&pirequest));
Long m_ldatasize;
HRESULT hr = pirequest->get_totalbytes (&m_ldatasize);
if (FAILED (HR))
return HR;
if (m_ldatasize = 0L)
{
return S_OK;
}

COleVariant varbytestoread;
COleSafeArray sarraybytes;
Varbytestoread = m_ldatasize;
Sarraybytes.createonedim (VT_UI1, m_ldatasize);
hr = Pirequest->binaryread (&varbytestoread, &sarraybytes);
if (FAILED (HR))
{
Sarraybytes.clear ();
return HR;
}
...
5. For indefinite number of parameters, SAFEARRAY can be used, but the use of the method is more complex. You can enter the following in the argument [In]safearray array, and by attribute ... button, select vararg in name. Modify the IDL file after confirmation to change [In]safearray array to [In]safearray (VARIANT) array. Then open the MyComponent.h file to change the function's parameter SAFEARRAY array to safearray* array. Then open the MyComponent.cpp file modification parameters, use the COleSafeArray class in the function body to implement the control of the parameter, such as "COleSafeArray Sarray (ARRAY,VT_BSTR)", You can refer to the member functions of the class in MSDN for a specific action.
Iv. Adding Interface Properties
In ClassView, right-click Imycomponent and press Add property in the menu that appears. As shown in the property, you can select an attribute type such as float to write the property name such as Myfloat below. Keep the rest unchanged. What needs to be explained here is the writing in the generated put_myfloat and get_myfloat function bodies.

Let's cite a small example:
1. Add member variables to the Cmycomponent m_myfloat
2. Write in put_myfloat: M_myfloat=newval;
3. Written in Get_myfloat: *pval=m_myfloat
A member variable is also a place that is superior to ISAPI in that it is possible to pass information between Web pages in an ISAPI only through URL parameters or cookies, because each call is a new link, and the DLL cannot be called the same time between pages, so setting a member variable makes little sense, Useful only in one call. Both of these methods can only deliver very limited characters, unless the creation of a server-side temporary file (this method is bad). Furthermore, the session object in ASP itself is a good object for storing information.
So far, we've created a very simple ASP component with no functionality, intended to illustrate how to make it. Below, we write a simple ASP page to see how to use it. The Web page is as follows:
<%@ Language=vbscript%>
<HTML>
<HEAD>
<meta name= "generator" content= "Microsoft Visual Studio 6.0" >
</HEAD>
<BODY>
<%
Set obj=server.createobject ("Mystudio.mycomponent")
J=obj.initmycomp (5)
Response.Write (j)
%>
<p>
<%
obj.myfloat=3.9
J=obj.myfloat
Response.Write (j)
%>
<p>
<%
str1= "ABCD"
Str2=obj.strconv (STR1)
Response.Write (STR2)
%>
</BODY>
</HTML>
Display results as:
5
3.9
Abc
Perhaps, you still can't see the great glamour of writing ASP component now. However, if you are writing search engines, file upload, automatic response, and so on advanced web production, if you can fully utilize VC + + Superior programming means, you do not have to struggle in the Cgi,isapi, you can easily write these functions. For example, users want to search on your Web site on several famous news sites on the current important news, you can use ASP components in the background real-time open the site to get information, filter the match to show the user. For example, when a registered user tells his email address, you can use ASP components to automatically send a confirmation letter to the new registered users, the quickest way for users to confirm and make registration successful ... In fact, you must have a lot of new ideas can be achieved on the Web page, to move quickly.

Http://www.ccw.com.cn/htm/app/aprog/01_2_13_4.asp



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.