Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Fast Track Visual C++ 6.0 Programming
(Publisher: John Wiley & Sons, Inc.)
Author(s): Steve Holzner
ISBN: 0471312908
Publication Date: 09/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Creating CounterDLL.dll

Using AppWizard(dll), create a new DLL project (dynamically linked to MFC) named CounterDLL and add the prototype of the IncrementCounter() function to the CounterDLL.h header. This function takes no parameters and returns none, because we display the new value of the counter with a message box.

// CounterDLL.h : main header file for the COUNTERDLL DLL
//

#if
!defined(AFX_COUNTERDLL_H__A45E4A6E_A79A_11D1_887F_D42B07C10710__INCLUDED_)
#define AFX_COUNTERDLL_H__A45E4A6E_A79A_11D1_887F_D42B07C10710__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#ifndef __AFXWIN_H__
    #error include ‘stdafx.h’ before including this file for PCH
#endif

#include “resource.h”        // main symbols

#define DLLexport    __declspec( dllexport )                        ⇐

DLLexport void WINAPI IncrementCounter();                           ⇐
Next, add the IncrementCounter() function to the end of CounterDLL.cpp:
/////////////////////////////////////////////////////////////////////////////
// The one and only CCounterDLLApp object

CCounterDLLApp theApp;

DLLexport void WINAPI IncrementCounter()                            ⇐
{                                                                   ⇐

}                                                                   ⇐

In this function, we simply increment a variable named data and display its new value as follows:

/////////////////////////////////////////////////////////////////////////////
// The one and only CCounterDLLApp object

CCounterDLLApp theApp;

DLLexport void WINAPI IncrementCounter()
{
    char text[80];                                                  ⇐
    sprintf(text, “New counter value of %d.”, ++data);            ⇐
    AfxMessageBox(text);                                            ⇐

}

The main point here is how we set up the storage for the variable named data. We do that now, making sure that that variable is shared between all instances of our DLL.

Setting Up Shared Memory in a DLL

To set up the variable we’ve named data and to make sure it’s shared among all instances of this DLL, we use the Visual C++ pragma data_seg(). This pragma lets us set up the data in the data segment of our program; we set up the variable named data, initialize it to 0, and call the shared section of memory SharedMemory.


Shared Memory Must Be Initialized

Note that we initialize the variable named data to 0, and that’s important. If we don’t initialize the variable, it is stored in the uninitialized data section of the data segment and sharing it among DLLs would be impossible.


// CounterDLL.cpp : Defines the initialization routines for the DLL.
//

#include “stdafx.h”
#include “CounterDLL.h”

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#pragma data_seg(“SharedMemory”)                                   ⇐
   int data = 0;                                                   ⇐
#pragma data_seg()                                                 ⇐
    .
    .
    .

There’s one more thing we must do to set up our shared section of memory: declare it in the DLL project’s .def file. This file specifies the name of the DLL file we’re creating, as well as the explicit exports if we want to list them, though that’s no longer necessary. In this case, we indicate that we’re setting up a shared read/write memory section named SharedMemory.

; CounterDLL.def : Declares the module parameters for the DLL.

LIBRARY      “CounterDLL”
DESCRIPTION  ‘CounterDLL Windows Dynamic Link Library’

EXPORTS
    ; Explicit exports can go here

SECTIONS                                                            ⇐

    SharedMemory    READ WRITE SHARED                               ⇐

Create CounterDLL.dll and copy it to the c:\windows\system directory.

Creating the Counter Program

The next step is to create the Counter program that calls our new IncrementCounter() function. Create the new MDI Counter project now and copy CounterDLL.lib to the Counter project’s directory. Next, link CounterDLL.lib into the project using the Project menu’s Settings item as we did in the previous example.

We also include CounterDLL.h in the view’s .cpp file so the Counter project knows about the incrementCounter() function.

// CounterView.cpp : implementation of the CCounterView class
//

#include “stdafx.h”
#include “Counter.h”

#include “CounterDoc.h”
#include “CounterView.h”
#include “CounterDLL.h”                                           ⇐
     .
     .
     .


Figure 10.5  Using calls to a custom DLL.


Figure 10.6  Using calls to a custom DLL.

Finally, add a new item to the File menu, “Increment counter,” and call the IncrementCounter() function from that menu item’s handler function.

void CCounterView::OnFileIncrementcounter()
 {
    IncrementCounter();                                            ⇐

}

With this, we are able to run two copies of Counter, as shown in Figure 10.5.

When we select the Increment counter menu item in the first copy of Counter, the counter is incremented to 1, as shown in Figure 10.5. Next, in the second copy of Counter, again select the Increment counter item. As you can see in Figure 10.6, the counter is incremented to 2. We’re sharing memory between these two processes using DLLs. Our shared memory DLL example is a success.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.