cppengg
cppengg

Reputation: 11

DLL memory mapping or address space allocation in executable

Correct me if i am wrong. Question is related to C++ on Windows Console application.

I created two applications, abc.exe and def.exe, and a DLL called Funky.dll. I kept the DLL at location C:\Funky.dll.

There is a global string variable in the DLL e.g. "string Value;" And Dlls exposed function is using that variable.

Below is the DLL code:

#include<header files etc>

string Value;

#ifdef _MANAGED
#pragma managed(push, off)
#endif

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{


    return TRUE;
}

#ifdef _MANAGED
#pragma managed(pop)
#endif

extern "C" __declspec(dllexport) string Display(string str, int val)
{

    value=str;
    cout<<"\n************I am in a FunkyDLL************\n";
    cout<<"\n"<<value;
    cout<<"\n"<<val;
    printf("\nAddress Space %u::",&value);



return value;
}

Now I am accessing the same DLL from two different applications (i.e. abc.exe and def.exe) but the location of the DLL is the same, i.e. c:\Funky.dll. I call the Display function in both applications in while loop, e.g. for abc.exe:

while(1)
{
Display("ABC",10);
}

and for def.exe:

while(1)
{
Display("DEF",10);
}

Now the display function printing the different values for different application. But the address of the variable is always the same.

Can someone please explain it or provide the link related to address space of DLL in executables.

Thanks a lot in advance.

Upvotes: 1

Views: 2286

Answers (2)

Andrey
Andrey

Reputation: 4356

Each process using you DLL has its own copy of DLL global and static variables. In order to share data between processes you have to manually apply one of the multiple approaches - most oftenly used are making a shared section in a DLL or via the memory mapped files API.

Read carefully all DLL-related material on MSDN (starting point) and even better - get a copy of Richter's "Programming aplications for Windows"

Upvotes: 1

Glenner003
Glenner003

Reputation: 1552

see wikipedia

Upvotes: 1

Related Questions