| Tenouk C & C++ | MFC Home | ActiveX Doc Servers & Internet 3 | ActiveX Doc Servers & Internet 5 | Download | Site Index |


 

 

 

 

 

 

ActiveX Document Servers and the Internet Part 4

 

 

 

 

 

 

Program examples compiled using Visual C++ 6.0 compiler on Windows XP Pro machine with Service Pack 2 and some Figure screen snapshots have been taken on Windows 2000 server. The Internet Information Services version is IIS 4.x/5.x/6.x on Windows 2000 Server SP 4 and Windows XP Pro SP 2. The Internet Explorer is 6.x. Topics and sub topics for this tutorial are listed below. A complete information about IIS installation, configuration and testing a Web site is dumped HERE and how to setup FTP server also included HERE. Both Web and FTP servers were done on Windows 2000 Server SP4. Don’t forget to read Tenouk’s small disclaimer.

  1. MYEX35B From Scratch...Continue

  2. The Coding Part

  3. CMainFrame Class

  4. CInPlaceFrame Class

  5. CMyex35bDoc Class

  6. CMyex35bView Class

 

 

In ResourceView select IDD_ADDRDIALOG and launch ClassWizard. Select Create a new class and click OK.

 

 

ActiveX document and Internet - Figure 75: Adding a new class to project.

 

Figure 75: Adding a new class to project.

 

Fill in as shown below. Click OK.

 

ActiveX document and Internet - Figure 76: Entering IDD_ADDRDIALOG class information.

 

Figure 76: Entering IDD_ADDRDIALOG class information.

 

Add the following member variables to CAddrDialog class.

 

ID

Type

Variable name

IDC_FILE

CString

m_strFile

IDC_SERVERNAME

CString

m_strServerName

 

Table 7.

 

ActiveX document and Internet - Figure 77: Adding member variable to class.

 

Figure 77: Adding member variable to class.

 

ActiveX document and Internet - Figure 78: The added member variables.

 

Figure 78: The added member variables.

 

Add a new class. Click the Add Class button and select New. Add CValidForm class information as shown below.

 

ActiveX document and Internet - Figure 79: Adding CValidForm class information.

 

Figure 79: Adding CValidForm class information.

 

ActiveX document and Internet - Figure 80: Modifying the source and header files for the class.

 

Figure 80: Modifying the source and header files for the class.

 

Add new header and source files, PostThread.h and PostThread.cpp.

 

ActiveX document and Internet - Figure 81: Adding new files to a class.

 

Figure 81: Adding new files to a class.

 

ActiveX document and Internet - Figure 82: Adding new header and source files, PostThread.h and PostThread.cpp.

 

Figure 82: Adding new header and source files, PostThread.h and PostThread.cpp.

 

Copy the following codes to PostThread.h and PostThread.cpp file respectively.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

// PostThread.h

 

#define WM_POSTCOMPLETE WM_USER + 6

 

extern CString g_strFile;

extern CString g_strServerName;

extern CString g_strParameters;

UINT PostThreadProc(LPVOID pParam);

// PostThread.cpp (uses MFC Wininet calls)

#include <stdafx.h>

#include "PostThread.h"

 

#define MAXBUF 50000

 

CString g_strFile = "/scripts/myex34a.dll";

CString g_strServerName = "localhost";

CString g_strParameters;

 

UINT PostThreadProc(LPVOID pParam)

{

       CInternetSession session;

       CHttpConnection* pConnection = NULL;

       CHttpFile* pFile1 = NULL;

       char* buffer = new char[MAXBUF];

       UINT nBytesRead = 0;

       DWORD dwStatusCode;

       BOOL bOkStatus = FALSE;

       try

       {

           pConnection = session.GetHttpConnection(g_strServerName, (INTERNET_PORT) 80);

           pFile1 = pConnection->OpenRequest(0, g_strFile + "?ProcessTimesheet", // POST request

                            NULL, 1, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RELOAD); // no cache

              pFile1->SendRequest(NULL, 0, (LPVOID) (const char*) g_strParameters, g_strParameters.GetLength());

              pFile1->QueryInfoStatusCode(dwStatusCode);

              if(dwStatusCode == 200) { // OK status

              // doesn't matter what came back from server

              // -- we're looking for OK status

                     bOkStatus = TRUE;

                     nBytesRead = pFile1->Read(buffer, MAXBUF - 1);

                     buffer[nBytesRead] = '\0'; // necessary for TRACE

                     TRACE(buffer);

                     TRACE("\n");

              }

       }

       catch(CInternetException* pe) {

              char text[100];

              pe->GetErrorMessage(text, 99);

              TRACE("WinInet exception %s\n", text);

              pe->Delete();

       }

       if(pFile1) delete pFile1; // does the close -- prints a warning

       if(pConnection) delete pConnection; // why does it print a warning?

       delete [ ] buffer;

       ::PostMessage((HWND) pParam, WM_POSTCOMPLETE, (WPARAM) bOkStatus, 0);

       return 0;

}

 

 

Listing 13.

 

The Coding Part

 

Add the following #include directives in StdAfx.h.

#include <afxinet.h>

#include <afxmt.h>

 

Listing 14 - ActiveX document and Internet - C++ and MFC code snippet.

 

Listing 14.

 

CMainFrame Class

 

Add the following codes in MainFrm.cpp.

// TODO: Remove this if you don't want tool tips or a resizable toolbar

m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);

 

Listing 15 - ActiveX document and Internet - C++ and MFC code snippet.

 

Listing 15.

 

Edit the PreCreateWindow() as shown below.

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)

{

      // TODO: Modify the Window class or styles here by modifying

      //  the CREATESTRUCT cs

      return CFrameWnd::PreCreateWindow(cs);

}

 

Listing 16 - ActiveX document and Internet - C++ and MFC code snippet.

 

Listing 16.

 

Add the following code in InitInstance().

AfxEnableControlContainer();

 

Listing 17 - ActiveX document and Internet - C++ and MFC code snippet.

 

Listing 17.

 

And delete the following code at the end of the InitInstance().

// The one and only window has been initialized, so show and update it.

m_pMainWnd->ShowWindow(SW_SHOW);

m_pMainWnd->UpdateWindow();

CInPlaceFrame Class

 

Using ClassView, delete OnCreateControlBars() virtual function and delete the implementation in IpFrame.cpp.

 

ActiveX document and Internet - Figure 83: Deleting a message handler.

 

Figure 83: Deleting a message handler.

 

Delete also the following variable.

CToolBar    m_wndToolBar;

 

Listing 18 - ActiveX document and Internet - C++ and MFC code snippet.

 

Listing 18

 

Edit/change the ValForm.h and ValForm.cpp code as shown below.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

// ValForm.h

 

#ifndef _VALIDFORM

#define _VALIDFORM

 

#define WM_VALIDATE WM_USER + 5

 

class CValidForm : public CFormView

{

DECLARE_DYNAMIC(CValidForm)

private:

              BOOL m_bValidationOn;

public:

       CValidForm(UINT ID);

       // override in derived dlg to perform validation

    virtual void ValidateDlgItem(CDataExchange* pDX, UINT ID);

       //{{AFX_VIRTUAL(CValidForm)

       protected:

       virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam);

       //}}AFX_VIRTUAL

 

       //{{AFX_MSG(CValidForm)

       afx_msg LONG OnValidate(UINT wParam, LONG lParam);

       //}}AFX_MSG

       DECLARE_MESSAGE_MAP()

};

 

#endif // _VALIDFORM

 

// ValForm.cpp : implementation file

//

 

#include "stdafx.h"

#include "myex35b.h"

#include "ValForm.h"

 

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[ ] = __FILE__;

#endif

 

IMPLEMENT_DYNAMIC(CValidForm, CFormView)

 

BEGIN_MESSAGE_MAP(CValidForm, CFormView)

       //{{AFX_MSG_MAP(CValidForm)

       ON_MESSAGE(WM_VALIDATE, OnValidate)

       //}}AFX_MSG_MAP

END_MESSAGE_MAP()

 

CValidForm::CValidForm(UINT ID) : CFormView(ID)

{

       TRACE("CValidForm ctor\n");

    m_bValidationOn = TRUE;   // turn validation on

}

 

void CValidForm::ValidateDlgItem(CDataExchange* pDX, UINT ID)

{

       // return valid unless overridden in the form

       ASSERT(this);

       TRACE("CValidForm::ValidateDlgItem (should be overridden)\n");

       return;

}

 

BOOL CValidForm::OnCommand(WPARAM wParam, LPARAM lParam)

{

       // specific for WIN32 -- wParam/lParam processing different for WIN16

       TRACE("CValidForm::OnCommand, wParam = %x, lParam = %x\n", wParam, lParam);

       TRACE("m_bValidationOn = %d\n", m_bValidationOn);

       if(m_bValidationOn) {   // might be a killfocus

        UINT notificationCode = (UINT) HIWORD( wParam );

        if((notificationCode == EN_KILLFOCUS)  || (notificationCode == LBN_KILLFOCUS) || (notificationCode == CBN_KILLFOCUS) ) {

            CWnd* pFocus = CWnd::GetFocus(); // static function call

            // if we're changing focus to another control in the same form

            if( pFocus && (pFocus->GetParent() == this) ){

              if(pFocus->GetDlgCtrlID() != IDCANCEL) {

                  // and focus not in Cancel button, validate AFTER drawing finished

                  BOOL rtn = PostMessage( WM_VALIDATE, wParam );

                  TRACE("posted message, rtn = %d\n", rtn);

                }

            }

        }

    }

       return CFormView::OnCommand(wParam, lParam); // pass it on

}

 

LONG CValidForm::OnValidate(UINT wParam, LONG lParam)

{

    TRACE("Entering CValidForm::OnValidate\n");

    CDataExchange dx(this, TRUE);

    m_bValidationOn = FALSE;  // temporarily off

    UINT controlID = (UINT) LOWORD( wParam );

    try {

           ValidateDlgItem(&dx, controlID);

       }

       catch(CUserException* pUE) {

              pUE->Delete();

              TRACE("CValidForm caught the exception\n");

              // fall through -- user already alerted via message box

       }

    m_bValidationOn = TRUE;

    return 0;  // goes no further

}

 

 

Listing 19

 

CMyex35bDoc Class

 

Use ClassView or manually add the following public variable.

 

public:

      int  m_nPeriod;

 

ActiveX document and Internet - Figure 84: Adding variable.

 

Figure 84: Adding variable.

 

Listing 20 - ActiveX document and Internet - C++ and MFC code snippet.

 

Listing 20.

 

Initialize the variable as shown below.

CMyex35bDoc::CMyex35bDoc()

{

      // Use OLE compound files

      EnableCompoundFile();

      // TODO: add one-time construction code here

      m_nPeriod = 12; // should initialize this some other way

}

 

Edit Serialize() as shown below.

void CMyex35bDoc::Serialize(CArchive& ar)

{

      if (ar.IsStoring())

      {

            // TODO: add storing code here

            ar << m_nPeriod;

      }

      else

      {

            // TODO: add loading code here

            ar >> m_nPeriod;

      }

      // Calling the base class COleServerDoc enables serialization

      //  of the container document's COleClientItem objects.

      COleServerDoc::Serialize(ar);

}

 

Listing 21 - ActiveX document and Internet - C++ and MFC code snippet.

 

Listing 21.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

CMyex35bView Class

 

Add the #include "ValForm.h" directive and change the CView base class to CValidForm in myex35bView.h.

 

Listing 22 - ActiveX document and Internet - C++ and MFC code snippet.

 

Listing 22.

 

Then, manually add the following virtual function.

 

virtual void ValidateDlgItem(CDataExchange* pDX, UINT ID);

 

Listing 23 - ActiveX document and Internet - C++ and MFC code snippet.

 

Listing 23.

 

Using ClassWizard, delete OnDraw() and add DoDataExchange() and OnPrint() virtual functions. Don’t forget to delete the implementation part of the deleted function.

 

ActiveX document and Internet - Figure 85: Adding and deleting virtual functions.

 

Figure 85: Adding and deleting virtual functions.

 

Listing 24 - ActiveX document and Internet - C++ and MFC code snippet.

 

Listing 24.

 

Next, delete OnDestroy() and add the following message map functions. Don’t forget to delete the implementation part of the deleted function.

 

ID

Message

Function

IDC_CANCEL

BN_CLICK

OnCancel()

IDC_SUBMIT

BN_CLICK

OnSubmit()

ID_SERVER_ADDRESS

COMMAND

OnServerAddress()

OnPostComplete() – Added manually if you cannot found it in ClassWizard crap as shown below.

 

afx_msg LONG OnPostComplete(UINT wParam, LONG lParam);

 

Table 8.

 

ActiveX document and Internet - Figure 86: Adding message handlers.

 

Figure 86: Adding message handlers.

 

Listing 25 - ActiveX document and Internet - C++ and MFC code snippet.

 

Listing 25.

 

Manually, add the following AFX DATA.

public:

      //{{AFX_DATA(CMyex35bView)

      enum { IDD = IDD_MYEX35B_FORM };

      double      m_dHours;

      CString     m_strEmployee;

      int         m_nJob;

      int         m_nPeriod;

      //}}AFX_DATA

 

Listing 26 - ActiveX document and Internet - C++ and MFC code snippet.

 

Listing 26.

 

 

Add the following #include directives to myex35bView.cpp

#include "AddrDialog.h"

#include "PostThread.h"

 

Listing 27 - ActiveX document and Internet - C++ and MFC code snippet.

 

Listing 27.

 

Use Edit Find/Replace menu to replace all the CView base class to CValidForm and add the following at the end of the message map.

 

ON_MESSAGE(WM_POSTCOMPLETE, OnPostComplete)

 

Listing 28 - ActiveX document and Internet - C++ and MFC code snippet.

 

Listing 28.

 

Add the following data initialization in constructor.

 

      //{{AFX_DATA_INIT(CMyex35bView)

      m_dHours = 0.0;

      m_strEmployee = _T("");

      m_nJob = 0;

      m_nPeriod = 0;

      //}}AFX_DATA_INIT

 

Listing 29 - ActiveX document and Internet - C++ and MFC code snippet.

 

Listing 29.

 

 

 

 

 

Further reading and digging:

  1. DCOM at MSDN.

  2. COM+ at MSDN.

  3. COM at MSDN.

  4. Win32 process, thread and synchronization story can be found starting from Module R.

  5. MSDN MFC 7.0 class library online documentation.

  6. MSDN MFC 9.0 class library online documentation - latest version.

  7. MSDN Library

  8. Windows data type.

  9. Win32 programming Tutorial.

  10. The best of C/C++, MFC, Windows and other related books.

  11. Unicode and Multibyte character set: Story and program examples.

 

 


 

| Tenouk C & C++ | MFC Home | ActiveX Doc Servers & Internet 3 | ActiveX Doc Servers & Internet 5 | Download | Site Index |