Link to home
Start Free TrialLog in
Avatar of manas_ghosal
manas_ghosal

asked on

Dll tricks

I have more than one different process which are using the same Dll, I want to reflect certain states of some variable, declared locally to the Dll, to all the processes using the Dll. I don't want to use Global Atom table or Local Atom table.
  Is it possible? Please help me with some idea.
Avatar of kosolobov
kosolobov

As i understand you writing windows application.
In this case the best solution is to use Named file mapping.
Refer MSDN about this:
==========8<===========
Sharing Files and Memory
File mapping can be used to share a file or memory between two or more processes. To share a file or memory, all of the processes must use the name or the handle of the same file-mapping object.

To share a file, the first process creates or opens a file by using the CreateFile function. Next, it creates a file-mapping object by using the CreateFileMapping function, specifying the file handle and a name for the file-mapping object. The names of event, semaphore, mutex, and file-mapping objects share the same name space. Therefore, the CreateFileMapping and OpenFileMapping functions fail if they specify a name that is in use by an object of another type.

To share memory that is not associated with a file, a process must use the CreateFileMapping function and specify (HANDLE)0xFFFFFFFF as the hfile parameter instead of an existing file handle. The corresponding file-mapping object accesses memory backed by the system paging file. You must specify a size greater than zero when you specify an hfile of (HANDLE)0xFFFFFFFF in a call to CreateFileMapping.

The easiest way for other processes to obtain a handle of the file-mapping object created by the first process is to use the OpenFileMapping function and specify the object's name. This is referred to as named shared memory. If the file-mapping object does not have a name, the process must obtain a handle to it through inheritance or duplication. For more information on inheritance and duplication, see Inheritance.

Processes that share files or memory must create file views by using the MapViewOfFile or MapViewOfFileEx function. They must coordinate their access using semaphores, mutexes, events, or some other mutual exclusion technique. For more information, see Synchronization.

A shared file-mapping object will not be destroyed until all processes that use it close their handles to it by using the CloseHandle function.
==========8<===========
You might also consider

#pragma comment(linker,"/SECTION:.SharedData,RWS")
#pragma data_seg (".SharedData")    
int g_iMySharedStuff = 0;
#pragma data_seg ()

but you should control access to it using a Semaphore.

Avatar of manas_ghosal

ASKER

I accept GGRUNDY's technique but how do I share any Object of a class in the same manner
Only static object can be shared in such way.
kosolobov can you give me a small example please
kosolobov can you give me a small example please
My comment was about GGRUNDY's variant and it will look like before

#pragma comment(linker,"/SECTION:.SharedData,RWS")
#pragma data_seg (".SharedData")    

CMyClass object;

#pragma data_seg ()

the only limitation - class must not allocate memory inside constructor.
You still have questions open dating back to the year 2000.  ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if they are still open in 14 days.  Experts, please post closing recommendations before that time.

Below are your open questions as of today.  Questions which have been inactive for 21 days or longer are considered to be abandoned and for those, your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.
https://www.experts-exchange.com/questions/Q.11576379.html
https://www.experts-exchange.com/questions/Q.11771660.html
https://www.experts-exchange.com/questions/Q.20021632.html
https://www.experts-exchange.com/questions/Q.20021633.html
https://www.experts-exchange.com/questions/Q.20073130.html
https://www.experts-exchange.com/questions/Q.20074958.html
https://www.experts-exchange.com/questions/Q.20093305.html
https://www.experts-exchange.com/questions/Q.20174832.html
https://www.experts-exchange.com/questions/Q.20188009.html
https://www.experts-exchange.com/questions/Q.20257016.html
https://www.experts-exchange.com/questions/Q.20264169.html
https://www.experts-exchange.com/questions/Q.20272528.html
https://www.experts-exchange.com/questions/Q.20279237.html
https://www.experts-exchange.com/questions/Q.20283179.html
https://www.experts-exchange.com/questions/Q.20285545.html
https://www.experts-exchange.com/questions/Q.20287951.html
https://www.experts-exchange.com/questions/Q.20291697.html
https://www.experts-exchange.com/questions/Q.20293847.html



*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations.
If you are interested in the cleanup effort, please click this link
https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed in the link below
https://www.experts-exchange.com/commspt/Q.20277028.html
 
Moderators will finalize this question if in @14 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
 
Thanks everyone.
Moondancer
Moderator @ Experts Exchange
I am doing the same way as said by kosolobov and ggrundy, but it is still not working, I am giving my code below. Plz find any mistake I made
// proctestdll.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "stdio.h"

#ifdef __cplusplus
#define DE extern "C" _declspec(dllexport)
#else
#define DE _declspec(dllexport)
#endif
class  A{
public:
          int z;

     A(){
          z=0;
     }
     void setz(int v){
          z+=v;
     }
     int getz(){
          return z;
     }
};



BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                          )
{
      int i=10;
          if(ul_reason_for_call==DLL_PROCESS_ATTACH){
          ::MessageBox(NULL,"PROCESS_ATTACH","MSG",16);
          printf("i= %d\n",i);
          i++;
          }

     else if(ul_reason_for_call==DLL_THREAD_ATTACH)
               ::MessageBox(NULL,"THREAD_ATTACH","MSG",16);

     else if(ul_reason_for_call==DLL_THREAD_DETACH)
               ::MessageBox(NULL,"THREAD_DETACH","MSG",16);

     else if(ul_reason_for_call==DLL_PROCESS_DETACH)
               ::MessageBox(NULL,"PROCESS_DETACH","MSG",16);

    return TRUE;
}
#pragma data_seg("SHARED")
static A a;
int i=0;
#pragma data_seg()
#pragma comment(linker, "/section:SHARED,RWS")



DE void func(int* p){
     
     printf("address of p %u  value %d\n",p,*p);
     printf("address of i in func %u : %d\n",&i,i);
     ::MessageBox(NULL,"msg","MSG",16);
     i+=20;
     printf("address of i in func %u : %d\n",&i,i);

     ::MessageBox(NULL,"msg","MSG",16);
}

DE void setfunc(int k){
     a.setz (k);
}

DE int getfunc(){
     return a.getz();
}

Hmm from memory....
a class in global shared memory can't have a constructor.
Try taking the constructor out and see what happens.
(Don't worry the global memory is initialized to zero anyway).
In the code above I am calling setfunc and getfunc from processes but no where in any process I am getting the updated(as set by any other process) value of z.I have also removed the constructor
ASKER CERTIFIED SOLUTION
Avatar of GGRUNDY
GGRUNDY

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You logged in as recently as May 13, 2002 and we have asked that you finalize your open questions in April of this year.  Some of your open questions date back to the year 2000, and must be finalized.  ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101, Netminder or Mindphaser will return to finalize these if they are still open in 7 days.  Experts, please post closing recommendations before that time.

Below are your open questions as of today.  Questions which have been inactive for 21 days or longer are considered to be abandoned and for those, your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.
https://www.experts-exchange.com/questions/Q.11576379.html
https://www.experts-exchange.com/questions/Q.11771660.html
https://www.experts-exchange.com/questions/Q.20073130.html
https://www.experts-exchange.com/questions/Q.20074958.html
https://www.experts-exchange.com/questions/Q.20093305.html
https://www.experts-exchange.com/questions/Q.20174832.html
https://www.experts-exchange.com/questions/Q.20188009.html
https://www.experts-exchange.com/questions/Q.20257016.html
https://www.experts-exchange.com/questions/Q.20264169.html
https://www.experts-exchange.com/questions/Q.20272528.html
https://www.experts-exchange.com/questions/Q.20279237.html
https://www.experts-exchange.com/questions/Q.20287951.html
https://www.experts-exchange.com/questions/Q.20291697.html
https://www.experts-exchange.com/questions/Q.20293847.html
https://www.experts-exchange.com/questions/Q.20021632.html
https://www.experts-exchange.com/questions/Q.20021633.html
https://www.experts-exchange.com/questions/Q.20296939.html
https://www.experts-exchange.com/questions/Q.20297840.html

To view your locked questions, please click the following link(s) and evaluate the proposed answer.
https://www.experts-exchange.com/questions/Q.20299864.html

*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations.
If you are interested in the cleanup effort, please click this link
https://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed in the link below
https://www.experts-exchange.com/commspt/Q.20277028.html
 
Moderators will finalize this question if in @7 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
 
Thanks everyone.
Moondancer
Moderator @ Experts Exchange
Finalized today.