Temperature Convert:ATL Server Webservice and Client

    技术2022-05-11  3

    Temperature Convert:An XML Web service Using ATL Server and MFC Client 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: 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); } Troubleshooting: 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.

    Also discuss in Codeproject.com:

    http://www.codeproject.com/useritems/TemperatureConvertService.asp

     

    最新回复(0)