Static Variables in ATL Class? 
Author Message
 Static Variables in ATL Class?

I would like to be able to create a static int counter in my ATL Class to
keep track of how many times methods on objects of the class are called and
return this count in a property.

How do I do it? Because I get Linker Error 2001 on compile.

Q151167 applies but simply says dont use static variables.

So what else can I do?



Sat, 11 Oct 2003 10:43:32 GMT  
 Static Variables in ATL Class?
Is your ATL server a DLL or EXE?
The "sharing" scope is among all the clients calling the server?

makoto


Quote:
> I would like to be able to create a static int counter in my ATL Class to
> keep track of how many times methods on objects of the class are called
and
> return this count in a property.

> How do I do it? Because I get Linker Error 2001 on compile.

> Q151167 applies but simply says dont use static variables.

> So what else can I do?



Sat, 11 Oct 2003 11:46:54 GMT  
 Static Variables in ATL Class?
It Is an SMTP Event Sink DLL.


Quote:
> Is your ATL server a DLL or EXE?
> The "sharing" scope is among all the clients calling the server?

> makoto



> > I would like to be able to create a static int counter in my ATL Class
to
> > keep track of how many times methods on objects of the class are called
> and
> > return this count in a property.

> > How do I do it? Because I get Linker Error 2001 on compile.

> > Q151167 applies but simply says dont use static variables.

> > So what else can I do?



Sat, 11 Oct 2003 12:08:14 GMT  
 Static Variables in ATL Class?
If your DLL wants to count numbers of every event-callback
times in all proccess contexts, you can share variable(s) in a
shared memory.  Use #pragma directive:

#pragma data_seg(".YourShareName")
   long g_lCount = 0;
#pragma data_seg()

Also do not forget specify the shared name section in the
DLL's DEF file:

SECTIONS
.YourShareName READ WRITE SHARED

When increasing/descreasing the counter variable from multi-
thread contexts, protect the variable with InterlockedIncrement()
or its family function, or by a critical section.

makoto


Quote:
> It Is an SMTP Event Sink DLL.



Sat, 11 Oct 2003 12:57:59 GMT  
 Static Variables in ATL Class?
Thanks!

Do I need to take any special steps to clean up the shared memory when I'm
done with it?

Peter


Quote:
> If your DLL wants to count numbers of every event-callback
> times in all proccess contexts, you can share variable(s) in a
> shared memory.  Use #pragma directive:

> #pragma data_seg(".YourShareName")
>    long g_lCount = 0;
> #pragma data_seg()

> Also do not forget specify the shared name section in the
> DLL's DEF file:

> SECTIONS
> .YourShareName READ WRITE SHARED

> When increasing/descreasing the counter variable from multi-
> thread contexts, protect the variable with InterlockedIncrement()
> or its family function, or by a critical section.

> makoto



> > It Is an SMTP Event Sink DLL.



Sat, 11 Oct 2003 23:25:56 GMT  
 Static Variables in ATL Class?
On the C++ side of things, you must define every C++ static member
in a CPP file:

int CMyClass::m_nMyCounter = 0;

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD

MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================


Quote:
> I would like to be able to create a static int counter in my ATL Class to
> keep track of how many times methods on objects of the class are called
and
> return this count in a property.

> How do I do it? Because I get Linker Error 2001 on compile.

> Q151167 applies but simply says dont use static variables.

> So what else can I do?



Sun, 12 Oct 2003 02:40:03 GMT  
 Static Variables in ATL Class?
Basically no cleanup needed, as long as the data types declared
in the "#pragma data_seg" block are conventional variables
(such as long, int, double, etc...), array of them, and/or struct
consisting of them.

But if the data is a pointer or an object instance, basically you
you need override the global-scope "new" operator or prepare
the special editions of malloc/free functions.  Memory management
in this kinda case should be handled carefully in terms of memory
allocation.

Basically sharing an object instance or any of pointer between
multiple processes is very difficult topic.  What the "deta_seg"
block can do is only "static data sharing", not for dynamic data.
Sharing conventional data in a DLL is very easy.

makoto


Quote:
> Thanks!

> Do I need to take any special steps to clean up the shared memory when I'm
> done with it?

> Peter



> > If your DLL wants to count numbers of every event-callback
> > times in all proccess contexts, you can share variable(s) in a
> > shared memory.  Use #pragma directive:

> > #pragma data_seg(".YourShareName")
> >    long g_lCount = 0;
> > #pragma data_seg()

> > Also do not forget specify the shared name section in the
> > DLL's DEF file:

> > SECTIONS
> > .YourShareName READ WRITE SHARED

> > When increasing/descreasing the counter variable from multi-
> > thread contexts, protect the variable with InterlockedIncrement()
> > or its family function, or by a critical section.

> > makoto



> > > It Is an SMTP Event Sink DLL.



Mon, 13 Oct 2003 10:30:56 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. ATL COM - Passing ATL COM classes through another ATL COM class in the same app

2. Creating a static class from non static Framework classes

3. Static VS Local Class Level variables and Methods

4. static member variable inside class

5. LNK1169 & static const class member variables

6. Static class variables problem

7. Class static variables confussion

8. static variables in exported class having different values between dll and exe

9. static variables in exported class having different values between dll and exe

10. write to static class member variable...

11. Static member variables of template class

12. unresolved externals using static variable in class

 

 
Powered by phpBB® Forum Software