![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Fast Track Visual C++ 6.0 Programming
Creating CounterDLL.dllUsing 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 DLLTo set up the variable weve named data and to make sure its 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.
// 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() ⇐ . . . Theres one more thing we must do to set up our shared section of memory: declare it in the DLL projects .def file. This file specifies the name of the DLL file were creating, as well as the explicit exports if we want to list them, though thats no longer necessary. In this case, we indicate that were 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 ProgramThe 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 projects directory. Next, link CounterDLL.lib into the project using the Project menus Settings item as we did in the previous example. We also include CounterDLL.h in the views .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 ⇐ . . .
Finally, add a new item to the File menu, Increment counter, and call the IncrementCounter() function from that menu items 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. Were sharing memory between these two processes using DLLs. Our shared memory DLL example is a success.
|
![]() |
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. |