Click here to Skip to main content
15,860,972 members
Articles / Desktop Programming / ATL
Article

Temperature Convert:An XML Web service Using ATL Server and MFC Client

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
6 Mar 20073 min read 53.3K   707   20   5
An XML Web Service using ATL Server and Called by MFC Client

Introduction

If you are a C++ programmer tired DCOM of RPC boring implement debug, you must want to find a new way and keep free to manage your codes. But you don't want to miss the chance to approach XML Web Service, perhaps you can choose .Net (C#) to create one. But higher performance you want, more lead you to choose ATL Server Web Service. This article is basic on MSDN (Walkthrough: Creating an XML Web Service Using ATL Server), it demonstrate creating an XML Web service that converts temperatures measured in Fahrenheit to Celsius using C++ and ATL Server. I have enhanced the function that can convert temperatures between Fahrenheit and Celsius in the ATL Server Web Service function and using a MFC client to use this Web Service instead of Console application. The codes are unmanaged C++ more than managed.

You will accomplish the following activities:

During the course of this walkthrough, you will accomplish the following activities:

  • Create an XML Web service using the ATL Server Web Service project template named TempConvert .
  • Implement the XML Web service.
  • Deploy the XML Web service.
  • Create a MFC Application to call XML Web Service.

Creating the XML Web Service Project

First you should create a blank solution TempConvert. And then you can use the wizard to add a new XML Web Service project named TempConvert to this solution.

Implementing the XML Web Service

The theory of the ATL Server using a ISAPI extension DLL to call a defined function handler in the Web Service application DLL, response to your XML Web service request.

1, In the interface ITempConvertService, replace the sample helloWorld method with the following codes in TempConvert.h:

__interface ITempConvertService 

{ 

// declare a web service method and its in-parameters 

//and out-parameters 

//Convert Fahrenheit to Celsius 

[id(1)] HRESULT ConTempFah2Cel( 

[in] double dFahrenheit, 

[out, retval] double* pdCelsius); 

//Convert Celsius to Fahrenheit 

[id(2)] HRESULT ConTempCel2Fah( 

[in] double dCelsius, 

[out, retval] double* pdFahrenheit); 

};

2, The CTempConvertService class provides the implementation of the XML Web service. Add the codes to TempConvert.h file.

class CTempConvertService :

public ITempConvertService

{

public:

// This is a sample web service method that shows how to use the 

// soap_method attribute to expose a method as a web method

[ soap_method ] 
HRESULT ConTempFah2Cel(

/* [in]*/ double dFahrenheit,

/* [out, retval] */ double* pdCelsius)

{

if(!pdCelsius)

return E_INVALIDARG; 

*pdCelsius=((dFahrenheit-32)*5)/9; 

return S_OK;

}

[ soap_method ] 
HRESULT ConTempCel2Fah(

/* [in]*/ double dCelsius,

/* [out, retval] */ double* pdFahrenheit)

{

if(!pdFahrenheit)

return E_INVALIDARG;

*pdFahrenheit=dCelsius*9/5+32;

return S_OK;

}

}; // class CTempConvertService

Deploying the XML Web Service

If all is Ok, Compile the solution and deploy it your machine, here it to the local site localhost, you can view it in the IE: http://localhost/TempConvert/TempConvert.dll?Handler=GenTempConvertWSDL

It only show in XML. You will call the XML Web Service in the Following using a MFC Client.

Create a MFC Application to call XML Web Service

1, Create a Dialog-based MFC Application TempConvertClient

2, Add a web reference using URL: http://localhost/TempConvert/TempConvert.dll?Handler=GenTempConvertWSDL , an available alternate URL is: http://ws.bomege.com/bin/TempConvert.dll?Handler=GenTempConvertWSDL (The Source apply this)

and rename it to TempConvert;

3, Add a new controls to the Dialog:

Screenshot - TempConvertDialog.gif

<shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"><stroke joinstyle="miter"><formulas /><f eqn="if lineDrawn pixelLineWidth 0"><f eqn="sum @0 1 0"><f eqn="sum 0 0 @1"><f eqn="prod @2 1 2"><f eqn="prod @3 21600 pixelWidth"><f eqn="prod @3 21600 pixelHeight"><f eqn="sum @0 0 1"><f eqn="prod @6 1 2"><f eqn="prod @7 21600 pixelWidth"><f eqn="sum @8 21600 0"><f eqn="prod @7 21600 pixelHeight"><f eqn="sum @10 21600 0"></formulas /><path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"><lock v:ext="edit" aspectratio="t"><shape id="_x0000_i1025" style="WIDTH: 264pt; HEIGHT: 138.75pt" type="#_x0000_t75"><imagedata src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~2\TEMP\msohtml1\03\clip_image001.gif" o:title="tempConvertDialog">

4, Open the file TempConvertClientDlg.h , Add variable and method in the CTempConvertClientDlg Class :

class CTempConvertClientDlg : public CDialog 

{ 

// Construction 

public: 

CTempConvertClientDlg(CWnd* pParent = NULL); // standard constructor 

// Dialog Data 

enum { IDD = IDD_TEMPCONVERTCLIENT_DIALOG }; 

protected: 

virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support 

afx_msg void OnBnClickedC2F(); 

afx_msg void OnBnClickedF2C(); 

// Implementation 

protected: 

HICON m_hIcon; 

double m_dTempF; 

double m_dTempC; 

// Generated message map functions 

virtual BOOL OnInitDialog(); 

afx_msg void OnSysCommand(UINT nID, LPARAM lParam); 

afx_msg void OnPaint(); 

afx_msg HCURSOR OnQueryDragIcon(); 

afx_msg void OnConvert(); 

DECLARE_MESSAGE_MAP() 

}; 

5, In file TempConvertClientDlg.cpp, add implement method and event handler the Convert Button:

void CTempConvertClientDlg::OnConvert() 

{ 

UpdateData(TRUE); 

CoInitialize(NULL); 

HRESULT hr = S_OK; 

//Get Radio state 

UINT m_iRadio=GetCheckedRadioButton(IDC_RADIOF2C,IDC_RADIOC2F); 

GetDlgItem(IDC_CONVERT)->EnableWindow(FALSE); 

CTempConvertService *pTempCon=new CTempConvertService; 

switch(m_iRadio){ 

case IDC_RADIOC2F: 

double dFah; 

hr=pTempCon->ConTempCel2Fah(m_dTempC,&dFah); 

if(SUCCEEDED(hr)){ 

m_dTempF=dFah; 

GetDlgItem(IDC_CONVERT)->EnableWindow(); 

UpdateData(FALSE); 

}else{ 

MessageBox(_T("Convert Service Failed!"),_T("Service Failed"),MB_OK | MB_ICONHAND); 

} 

case IDC_RADIOF2C: 

double dCel; 

hr=pTempCon->ConTempFah2Cel(m_dTempF,&dCel); 

if(SUCCEEDED(hr)){ 

m_dTempC=dCel; 

GetDlgItem(IDC_CONVERT)->EnableWindow(); 

UpdateData(FALSE); 

}else{ 

MessageBox(_T("Convert Service Failed!"),_T("Service Failed"),MB_OK | MB_ICONHAND); 

} 

} 

delete pTempCon; 

CoUninitialize(); 

} 

6, When click the radio control will send message BM_CLICK to the Button named Convert:

void CTempConvertClientDlg::OnBnClickedC2F() 

{ 

//Send Message to call Button IDC_CONVERT 

GetDlgItem(IDC_CONVERT)->SendMessage(BM_CLICK,0,0); 

} 

void CTempConvertClientDlg::OnBnClickedF2C() 

{ 

//Send Message to call Button IDC_CONVERT 

GetDlgItem(IDC_CONVERT)->SendMessage(BM_CLICK,0,0); 

} 

Trouble spot:

1, The Web Service project built succeeded but it can't run? You need to add a "dll" parse to the ISAPI DLL in the IIS Configuration, it will handle the application dll .

2, The source code is using VS.NET C++ 2005, if you use the early VS C++ version, just create a new blank solution project and add the *.cpp, *.h etc. the project.

Source Files: TempConvert.zip ( MD5: <chmetcnv unitname="C" sourcevalue="4905" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">4905C832B30AE5FDFF<chmetcnv unitname="C" sourcevalue="25" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">25C4CDA64D6762)

TempConvert_Binary.zip ( MD5: FA<chmetcnv unitname="F" sourcevalue="5" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">5F7ECD19E73BC2D<chmetcnv unitname="F" sourcevalue="12" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">12F7CD<chmetcnv unitname="a" sourcevalue="93" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">93A38EC<chmetcnv unitname="a" sourcevalue="9" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">9A)

Compile & Run Environment: Windows Server 2003+IIS6, VS C++ 2005

Finally, give me comments to improve it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
China China
I am favor to use C\C++,MFC,ATL,COM and JavaScript to program, and interesting in network security as well.In my spare time,I like playingbasketball,Climbing.I now live in Beijing,China.It is nice to go along the red walls around the Forbitten City.

Comments and Discussions

 
QuestionHow to load DLL in ATL Server Project using vc++ Pin
infosarang2-Jun-08 0:08
infosarang2-Jun-08 0:08 
QuestionWeb Service Deployment Pin
Sadek8431-Oct-07 21:45
Sadek8431-Oct-07 21:45 
GeneralConvert Service Failed Pin
murtaza dhari1-Aug-07 3:26
murtaza dhari1-Aug-07 3:26 
AnswerRe: Convert Service Failed Pin
James, Lu Zuheng5-Aug-07 17:38
James, Lu Zuheng5-Aug-07 17:38 
GeneralMethod to use the Windows Script Host to call a webservice [modified] Pin
James, Lu Zuheng11-Mar-07 0:17
James, Lu Zuheng11-Mar-07 0:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.