Click here to Skip to main content
16,016,535 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: CBitmap and rotating pictures ... Pin
Mazdak5-May-02 6:58
Mazdak5-May-02 6:58 
GeneralRe: CBitmap and rotating pictures ... Pin
Hadi Rezaee5-May-02 7:11
Hadi Rezaee5-May-02 7:11 
GeneralRe: CBitmap and rotating pictures ... Pin
PJ Arends5-May-02 7:59
professionalPJ Arends5-May-02 7:59 
GeneralTrapping output from a DOS program Pin
Paul Barrass5-May-02 4:35
Paul Barrass5-May-02 4:35 
Generaldll global variables Pin
Aviv Halperin5-May-02 4:27
Aviv Halperin5-May-02 4:27 
GeneralRe: dll global variables Pin
Tom Archer5-May-02 4:35
Tom Archer5-May-02 4:35 
GeneralRe: dll global variables Pin
Aviv Halperin5-May-02 5:13
Aviv Halperin5-May-02 5:13 
GeneralRe: dll global variables Pin
Tom Archer5-May-02 6:19
Tom Archer5-May-02 6:19 
I'm not sure I follow your question.

Here are the steps to share data across multiple invocations of a DLL. In this example, I'm keeping track of the number of connected users to this DLL.

Server side (Dll)



  1. Create a Win32 DLL (I called mine DllSharedDataServer)
  2. Create a header file for the DLL (DllSharedDataServer.h)
  3. Add the following code to define the shared data. Note that I would normally just define this in the DLL's main CPP file, but I'm guessing from your question that your DLL has multiple CPP files, all of which need access to this variable.
    #pragma once
    
    #pragma data_seg(".SHARDAT")
    static int connections = 0;
    #pragma data_seg()

  4. Add the following function to the DllSharedDataServer.h header file so that clients can query as to the number of connected users.
    int GetNumberOfConnections();

  5. Add an include directive to the DllSharedDataServer.cpp file
    #include "DllSharedDataServer.h"

  6. Update the DllMain function to increment the number of connected users when a client attaches and decrement the counter when a client detaches:
    BOOL APIENTRY DllMain( HANDLE hModule, 
                           DWORD  ul_reason_for_call, 
                           LPVOID lpReserved
    					 )
    {
      switch (ul_reason_for_call)
      {
        case DLL_PROCESS_ATTACH:
          connections++;
          break;
        case DLL_PROCESS_DETACH:
          connections--;
          break;
        default:
          break;
      }
      return TRUE;
    }

  7. Add the GetNumberOfConnections function to return the counter's value
    int GetNumberOfConnections()
    {
      return connections;
    }

  8. Add a DEF file to the DLL and update it as follows so that the GetNumberOfConnections function is exported and the shared data segment is properly defined:
    LIBRARY	DllSharedDataServer
    
    SECTIONS
    .SHARDAT READ WRITE SHARED
    
    EXPORTS
      GetNumberOfConnections

  9. Build the DLL

Client side



  1. Create an MFC dialog-based app to test with (DllSharedDataClient). In cases like this where the two projects are meant to work together I usually specify the Add to solution option so that it's easier to work on both projects at the same time.
  2. Include the DllSharedDataServer.h file in the DllSharedDataClientDlg.cpp file.
  3. In the dialog's OnOK handler place the following call to the GetNumberOfConnections function
    void CDllSharedDataClient1Dlg::OnBnClickedOk()
    {
      int i = GetNumberOfConnections();
      CString str;
      str.Format("There are currently %ld connections to the DLL", i);
      AfxMessageBox(str);
    }

  4. Add the DllSharedDataServer.lib to the DllSharedDataClient project
  5. Build the client
  6. Finally, copy the DLL to a folder where it can be found when running the client.

    Testing the DLL


    Now simply run a few instances of the client and you will see that each time you click the OK button the correct number of connections will be displayed; thereby proving that your data is being shared among multiple connections to the DLL.


    Cheers,
    Tom Archer
    Author, Inside C#
    Author, Visual C++.NET Bible

    A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the af

GeneralRe: dll global variables Pin
Joel Lucsy6-May-02 5:42
Joel Lucsy6-May-02 5:42 
GeneralRe: dll global variables Pin
Tom Archer6-May-02 14:06
Tom Archer6-May-02 14:06 
GeneralRe: dll global variables Pin
Joel Lucsy6-May-02 14:39
Joel Lucsy6-May-02 14:39 
GeneralRe: dll global variables Pin
Tom Archer6-May-02 14:49
Tom Archer6-May-02 14:49 
GeneralProgramatically simulating menu selection Pin
5-May-02 3:19
suss5-May-02 3:19 
GeneralRe: Programatically simulating menu selection Pin
l a u r e n5-May-02 4:04
l a u r e n5-May-02 4:04 
GeneralRe: Programatically simulating menu selection Pin
5-May-02 4:21
suss5-May-02 4:21 
GeneralCleaning up _Restore directory Pin
Hyien5-May-02 3:14
Hyien5-May-02 3:14 
GeneralRe: Cleaning up _Restore directory Pin
Michael Dunn5-May-02 8:00
sitebuilderMichael Dunn5-May-02 8:00 
GeneralRe: Cleaning up _Restore directory Pin
Hyien6-May-02 3:15
Hyien6-May-02 3:15 
GeneralStop Multiple Instances Pin
Peter Liddle5-May-02 2:45
Peter Liddle5-May-02 2:45 
GeneralRe: Stop Multiple Instances Pin
User 66585-May-02 3:25
User 66585-May-02 3:25 
GeneralRe: Stop Multiple Instances Pin
Mazdak5-May-02 3:54
Mazdak5-May-02 3:54 
GeneralRe: Stop Multiple Instances Pin
Nish Nishant5-May-02 4:46
sitebuilderNish Nishant5-May-02 4:46 
GeneralRe: Stop Multiple Instances Pin
Ravi Bhavnani6-May-02 14:25
professionalRavi Bhavnani6-May-02 14:25 
Generalhelp me please. Pin
Angel Kid5-May-02 2:19
Angel Kid5-May-02 2:19 
GeneralRe: help me please. Pin
Anders Molin5-May-02 3:28
professionalAnders Molin5-May-02 3:28 

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.