Click here to Skip to main content
13,260,019 members (43,507 online)
Click here to Skip to main content
Add your own
alternative version


20 bookmarked
Posted 11 Sep 2004

Calling webService (managed C# webservice) from MFC(unmanaged) Application by Retaining the Session using Cookies Container.

, 11 Sep 2004
Rate this:
Please Sign up or sign in to vote.
This article deals with calling webService (managed C# webservice) from MFC (unmanaged) application by retaining the session using Cookies Container


This article deals with calling webservice (managed C# webservice) from MFC (unmanaged) application by retaining the session using Cookies Container.


This article deals with the following factors:

  • Calling managed webservice from MFC Application.
    Solution: Create MFC Dialog Based application, say FirstWebServiceClientDlg.
    Before adding WebService, you have to change the settings of project.
    Right click on the Solution Explorer, click properties.
    • General - Use Managed Extensions = YES
    • C/C++ - General - Debug Information Format = Program Database (/Zi)
    • C/C++ - General - Compile As Managed = Assembly Support (/clr)
    • C/C++ - Code Generation - Enable Minimal Rebuild = No
    • C/C++ - Code Generation - Basic Runtime Checks = Default

Now you are ready to add WebService. Right click on Solution Explorer -> AddWebService -> in the dialog box, give http://localhost/firstwebservice/firstwebservice.asmx then click Addreference button.
[Note: You have to make Firstwebservice folder websharing that is contained in the sample code.]

VC.NET automatically adds a header file (webservice.h), .cs file and a DLL. In order to call your webservice, you just include webservice.h.

  1. Make managed C++ object as member variable inside unmanaged class.


    //Add in the dialog Class    
    //Check FirstwebServiceClient.h
    gcroot<webserviceobject*>  m_pWebServiceObject;
  2. To use the cookiescontainer objects like in C#


    using namespace System::Net;
    // And Add in the dialog Class   //Check FirstwebServiceClient.h
    gcroot<CookieContainer*> m_pCookies ; 
    //CookiesContainer is used Enable  Session. In the Sample Code

Sample Code Description

There are two folders:

  • FirstWebServiceClient is an MFC Project
  • FirstWebService is a C# project, used to create WebService

webService contains two methods - one for setting values, another for adding values
Set this folder for WebSharing (Name FirstwebService).

Set values, create two session variables to store the values. Add values to add previously stored values.

Client Code

Before SetValue is called in the client side, you have to enable the cookies.

void CFirstWebServiceClientDlg::OnBnClickedButtonSetval()
  m_pWebService->CookieContainer = m_pCookies;
  AfxMessageBox("Now Press Add Button")

Then press Add Button. You will get the correct answer.

Just Comment line
//m_pWebService->CookieContainer = m_pCookies; 

Then press Add Button, and compare the result.

The difference is because, when every time you send a Web request, it is treated as a separate request. In order to retain the previous values, use enable session.


  • 12th September, 2004: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Architect Max Corp.
Singapore Singapore
I am BaijuMax.Now working as Senior Systems Engineer.

Tech. Interests :- C#,Winforms,,,VB,Sqlserver-2000,Analysis services,Matlab,Neural networks etc...

Hobbies :- Watching Sci-Fi Movies.

Dream & Vision:- Intelligent OS , Space Travelling.


My Blog:-

You may also be interested in...


Comments and Discussions

GeneralSDL1031 error message.. Pin
yooyo3d18-Feb-05 13:48
memberyooyo3d18-Feb-05 13:48 
Im new in this and I dl your example I try to compile and run, but I got error message:
<br />
Creating web service proxy file for FirstWebServiceClient ...<br />
.\FirstWebService.wsdl(2,434) : error SDL1005 : unrecognized tag, with<br />
  [<br />
    namespace = ""<br />
    tag = "definitions"<br />
  ]<br />
sproxy : error SDL1031 : failure in processing discomap file: ".\FirstWebService.wsdl".<br />

So whats going on? Can you help me to solve this.

Im using .NET 2003

GeneralRe: SDL1031 error message.. Pin
Anonymous18-Feb-05 22:00
sussAnonymous18-Feb-05 22:00 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 12 Sep 2004
Article Copyright 2004 by BAIJUMAX
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid