Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

#pragma data_seg

94 views
Skip to first unread message

sabara...@hotmail.com

unread,
Jul 12, 2006, 5:27:42 AM7/12/06
to
Hi All,

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

David Ching

unread,
Jul 12, 2006, 5:54:37 AM7/12/06
to
<sabara...@hotmail.com> wrote in message
news:1152696462.1...@s13g2000cwa.googlegroups.com...

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


David Ching

unread,
Jul 12, 2006, 5:54:38 AM7/12/06
to
<sabara...@hotmail.com> wrote in message
news:1152696462.1...@s13g2000cwa.googlegroups.com...

Yes, I use this type of shared memory for things like this, and it works

sabara...@hotmail.com

unread,
Jul 12, 2006, 6:51:48 AM7/12/06
to
Thanks David...But how will i use this shared variable to set it to
TRUE in my second exe...

sabara...@hotmail.com

unread,
Jul 12, 2006, 6:54:33 AM7/12/06
to
I am not able to access the shared variable in the InitInstance of my
second exe..What can i do for this?

David Ching

unread,
Jul 12, 2006, 10:49:24 AM7/12/06
to
<sabara...@hotmail.com> wrote in message
news:1152701673....@s13g2000cwa.googlegroups.com...

>I am not able to access the shared variable in the InitInstance of my
> second exe..What can i do for this?
>

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


sabara...@hotmail.com

unread,
Jul 12, 2006, 10:52:22 AM7/12/06
to
Hi David,
I am not able to use it in the InitInstance of my second application.It
gives a compilation error.What may be the problem?

Thanks in advance
Arun

sabara...@hotmail.com

unread,
Jul 12, 2006, 10:59:01 AM7/12/06
to
Hi David,
Let me explain the problem in detail..
This is the code that i have added before the InitInstance in my first
application:

#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

unread,
Jul 12, 2006, 11:27:54 AM7/12/06
to
What do you mean "not able to access"?
joe

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

Joseph M. Newcomer

unread,
Jul 12, 2006, 11:27:07 AM7/12/06
to
An assignment statement usually works well for changing the value of a variable...

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

Joseph M. Newcomer

unread,
Jul 12, 2006, 11:44:47 AM7/12/06
to
You are using two different applications. The reason it doesn't compile is your whole
model is completely wrong. You cannot declare a shared data segment in application 1 and
somehow access it in application 2. This was never supported under any conditions
imaginable. It sounded initially like you were talking about application instances of the
same application.

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

Joseph M. Newcomer

unread,
Jul 12, 2006, 11:40:59 AM7/12/06
to
Oh, that has nothing to do with "accessing" the variable! It has a lot to do with
potential compilation errors! Why didn't you explain that in the first place?
joe

David Ching

unread,
Jul 12, 2006, 12:03:01 PM7/12/06
to

<sabara...@hotmail.com> wrote in message
news:1152716341....@75g2000cwc.googlegroups.com...

> Hi David,
> Let me explain the problem in detail..

> 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


sabara...@hotmail.com

unread,
Jul 13, 2006, 1:11:07 AM7/13/06
to
Hi,
Thanks for your information...

Regards,
Arun

0 new messages