C ++ Builder 6 for soap development (1)-A Hello world! Example

Source: Internet
Author: User
Tags wsdl
C ++ Builder 6 bizsnap/soap/WebService (1) -- A Hello world! Example

As a brother of Delphi, C ++ builder is exactly the same as Delphi in many aspects. Of course, it is similar in terms of SOAP/WebService. With regard to the use of Delphi for soap/WebService application development, many articles have been introduced on this site. For this reason, prawn Li Wei has published a monograph, so I don't want to talk about it any more. However, a few days ago, I saw someone discussing Li Wei's book and using C ++ builder for soap/WebService applications, so I also wrote several C ++ builder articles based on my previous articles about Delphi's soap/WebService.

Platforms used: Borland C ++ Builder 6 + update1, Windows 2000 Server, iis5

Server:
1. New | WebServices | SOAP server application. For example, the icons in the upper left corner are identical to those in Delphi 6 + Update 2:

Select the web app debugger executeable type. The coclass name is wadsoapdemo1, for example:

After confirming, the system will automatically prompt whether to create an interface. For example, if you are sure to open the new interface wizard, if you want to add an interface later, you can select the SOAP server interface in new | WebServices to open the new interface Wizard:

2. In the new interface wizard, enter "hello" to generate a SOAP server interface:

If you select generate sample methods, you can generate a series of methods in the generated interface at the same time. It comprehensively describes how to define methods in the interface. With this option, you can easily learn the soap development method of C ++ Builder 6. Service Activation Model can specify the activation method of the server: per request (activated every time a service is requested)/Global (global ). The default value is per request. In this mode, the service is established upon each service request of the client, and the service is terminated upon the completion of the response. In the global mode, the server has only one service instance, all Client Service requests are processed by this instance, specifically through a Global static function named xxxfactory;
3. saveall, unit1 is named mainwm, project1 is named demo1, and hello is not renamed;
4. Add a method -- gethello to the header file (hello. h) of the interface unit, as shown below:

_ Interface interface_uuid ("{3c8c8426-1cf2-423d-b09e-5feb5716b3c4 }")
Ihello: Public iinvokable
{
Public:
Virtual ansistring gethello (INT aid) = 0; // New Method
};
Typedef delphiinterface _ Di_ihello;

The _ interface is not a New Keyword (Borland does not dare to modify C ++'s standard ^_^). It is just a macro, which is actually a class, it is clear that this is an interface. The so-called interface requires that the class defined with _ interface must be a full abstract class (all member functions must be pure virtual functions ); therefore, the newly added method gethello is a pure virtual function.
5. Implement the gethello function. The following is the class definition generated in the interface unit file (hello. cpp), the method we add, and its implementation:

Class thelloimpl: Public tinvokableclass, public ihello
{
Public:

Ansistring gethello (INT aid); // This is the definition of the new method


/* Iunknown */
Hresult stdmethodcalltype QueryInterface (const guid & IID, void ** OBJ)
{Return getinterface (IID, OBJ )? S_ OK: e_nointerface ;}
Ulong stdmethodcalltype addref () {return tinterfacedobject: _ addref ();}
Ulong stdmethodcalltype release () {return tinterfacedobject: _ release ();}

/* Ensures that the class is not abstract */
Void checkvalid () {delete new thelloimpl ();}
};

Ansistring thelloimpl: gethello (INT aid)
{
If (Aid = 1)
Return "hello ";
Else
Return "world ";
}

As described in the annotations, The iunknown interface is also implemented in this class, And a checkvalid () function is used to ensure that this class implements all pure virtual functions defined in the interface. Gethello is the newly added interface method, including a very simple implementation.
6. the following automatically generated code is used to register interfaces and their implementation classes. Because c ++ does not have initialization like Delphi, it uses the compilation command # pragma startup to implement it:

static void RegTypes()
{
InvRegistry()->RegisterInterface(__interfaceTypeinfo(IHello));
InvRegistry()->RegisterInvokableClass(__classid(THelloImpl));
}
#pragma startup RegTypes 32

7. compile and generate: demo1.exe;

Run demo1.exe once to complete registration and then start the web app debugger. Open your browser and enter http: // localhost: 1024/demo1.wadsoapdemo1 to view a standard soap application description page. Click the corresponding link to view the relevant WSDL.

Client Program:
1. New | application creates a general VCL application;
2. saveall, unit1 is named clnmain, and project1 is named client;
3. New | Web Services Importer:
Enter http: // localhost: 1024/demo1.wadsoapdemo1/WSDL/ihello in the URL,

If you can see the correct XML document in the browser, select "Next" to generate the import result, for example:

Here are the interface ihello defined on the server and its method gethello. After the interface is selected, the interface unit is generated;
4. saveall: The ihello unit is not renamed and saved, and then # include ihello. h In clnmain;
5. Put a labelededit and a button on the form, for example:

6. Double-click button1 and enter the following program:

void __fastcall TForm2::Button1Click(TObject *Sender)
{
LabeledEdit1->EditLabel->Caption
= GetIHello()->GetHello( StrToInt( LabeledEdit1->Text ) );
}

7. Compile and run the program. Enter "1" in edit and press "button1". "Hello" will be displayed in the label, and "world" will be displayed when other numbers are entered ";

In this way, a soap application implemented using C ++ builder is completed, isn't it different from Delphi?

[Mental Studio] apr.30-02

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.