Click here to Skip to main content
Click here to Skip to main content

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

, 6 Mar 2007
Rate this:
Please Sign up or sign in to vote.
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

<formulas /></formulas />

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: 4905C832B30AE5FDFF25C4CDA64D6762)

TempConvert_Binary.zip ( MD5: FA5F7ECD19E73BC2D12F7CD93A38EC9A)

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

Share

About the Author

James, Lu Zuheng
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++ Pinmemberinfosarang2-Jun-08 0:08 
QuestionWeb Service Deployment PinmemberSadek8431-Oct-07 21:45 
Hi,
 
I have developed an xml web service using ATL. It worked locally fine, i.e. on the machine where service was built.
When i tried to deploy that service on other machine; either manually or through setup project; i failed to discover
that service. Although i have changed the name of the machine in the URLs that are in the discovery file, but still
the browser of the add web reference wizard tries to reach the URLs of the old machine.
 
This means that there is still a file that saves those URLs, but i couldn't find it out.
 
Could you please help me deploy the web service dynamically, i.e. it gets the name of the deploying server during
deployment?
 
Thanks in advance.
 
Ahmed Sadek
GeneralConvert Service Failed Pinmembermurtaza dhari1-Aug-07 3:26 
AnswerRe: Convert Service Failed PinmemberJames, Lu Zuheng5-Aug-07 17:38 
GeneralMethod to use the Windows Script Host to call a webservice [modified] PinmemberJames, Lu Zuheng11-Mar-07 0:17 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 6 Mar 2007
Article Copyright 2007 by James, Lu Zuheng
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid