I am using a #pragma data_seg(".shared") in my application which is a
.exe.I am having a shared variable of type bool in the shared memory.I
have set it to FALSE initially.I am launching a second exe from this
application.I want to set it to TRUE in the InitInstance of my second
application.Can i do this?If yes then how?
Thanks in advance
Arun
Yes, I use this type of shared memory for things like this, and it works
very well. Keep in mind that as with all things shared between processes
(and threads), you need to avoid race conditions by using mutex or critical
section. I manage to avoid these since only one thread/process writes the
variables, and the other threads/processes, just compare the variables to
empty or FALSE. I also use things like the InterlockedExchange() functions
when setting these variables, to ensure that any reader process will read
the variables intact.
When using #pragma data_seg, you need to make sure to mark the data segment
as "shared" or else your variables won't really be shared. Here's how:
#pragma data_seg(".sdata")
BOOL sh_bIsRunning = FALSE; // NOTE: all variables must be initialized
in the declaration, or else they won't be shared
#pragma data_seg()
// This comment makes the above data segment shareable
// It replaces having to add a linker option to the project
#pragma comment(linker, "/SECTION:.sdata,rws")
-- David
Yes, I use this type of shared memory for things like this, and it works
What do you mean you can't access it? Use it just like any normal variable!
How do you know you can access it in the first instance but not in the
second?
-- David
Thanks in advance
Arun
#pragma data_seg("MyShared")
volatile BOOL bIsRun = FALSE;
#pragma data_seg()
#pragma comment(linker, "/Section:MyShared,RWS")
Now in the InitInstance() of the first application i want to check as
follows:
BOOL Application1::InitInstance()
{
if (bIsRun)
return FALSE;
}
Now my problem is,in the InitInstance of the second application ie
Application2 i want to do something like this
BOOL Application2::InitInstance()
{
bIsRun = TRUE;
}
But this is not possible since the bIsRun is not declared in the second
application..Am i missing some minor thing or what could be the
problem?
Waiting for ur reply
Thanks
Arun
Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Note that in a #pragma data_seg that every variable *must* have an initializer clause,
that is
#pragma data_seg(".shared")
BOOL somevalue = TRUE;
BOOL othervalue;
#pragma data_seg()
that 'othervalue' is not in the data segment because it lacks an initializer.
In addition, you had better have a line of the form
#pragma comment(linker, "/Section:.shared,rws")
or appropriate declarations in a .def file or put that option on your linker command line;
otherwise, you don't really have a shared data segment. You have a private data segment
with the name ".shared", but it isn't *actually* shared!
joe
To share variables between different applications, you have to put them in a DLL. You
access the variables in the DLL. The DLL is now running multiple instances, and you can
share its data across its instances. The fact that those instances are running in
processes that were instantiated from separate applications is now irrelevant. Data
segments can only be shared between instances of the *same* executable image (and I mean
*same*; if you have two different copies of the DLL, they are two different DLLs from the
viewpoint of sharing; both applications must reference the exact same DLL file when they
load)
joe
> BOOL Application1::InitInstance()
> {
> if (bIsRun)
> return FALSE;
> }
>
> Now my problem is,in the InitInstance of the second application ie
> Application2 i want to do something like this
> BOOL Application2::InitInstance()
> {
> bIsRun = TRUE;
> }
> But this is not possible since the bIsRun is not declared in the second
> application..Am i missing some minor thing or what could be the
> problem?
Hi Arun,
OK, now it's clear. You can only use #pragma data_seg to share memory
between instances OF THE SAME APPLICATION. For example, if you create a
program called myapp.exe, then when you start myapp.exe the first time, you
can set the variable, and when you start myapp.exe the second time, it can
read the variable and will see the value set by the first instance.
If you want to share memory between two separate processes (different .exe
names) then you need to use conventional shared memory created with
CreateFileMapping and MapViewOfFile().
-- David
Regards,
Arun