Hello

Given a class, how can I define a global variable out of it in a driver as:


CMyClass A;

NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObj, PUNICODE_STRING pRegPath)
{
return STATUS_SUCCESS;
}

The compiler is complaining about atexit()

But when atexit() is supplied as:
extern "C" int __cdecl atexit(void (__cdecl*)())

{

return 0;

}

I get a warning which is treated as an error:

driver.obj : warning LNK4210: .CRT section exists; there may be unhandled
static initializers or terminators



How to fix this? and how to remove the "treat warning as errors" compile
option?

--

Elias

Re: define a global class variable? by Maxim

Maxim
Wed Mar 03 05:40:22 CST 2004

Generally speaking, you cannot. Nobody will call the constructor for such
an instance, unless you go in for hackery of reverse-engineering the C++
runtime library and calling such constructors manually from DriverEntry.

--
Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
maxim@storagecraft.com
http://www.storagecraft.com


"lallous" <lallous@lgwm.org> wrote in message
news:c243b2$1nplp6$1@ID-161723.news.uni-berlin.de...
> Hello
>
> Given a class, how can I define a global variable out of it in a driver as:
>
>
> CMyClass A;
>
> NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObj, PUNICODE_STRING pRegPath)
> {
> return STATUS_SUCCESS;
> }
>
> The compiler is complaining about atexit()
>
> But when atexit() is supplied as:
> extern "C" int __cdecl atexit(void (__cdecl*)())
>
> {
>
> return 0;
>
> }
>
> I get a warning which is treated as an error:
>
> driver.obj : warning LNK4210: .CRT section exists; there may be unhandled
> static initializers or terminators
>
>
>
> How to fix this? and how to remove the "treat warning as errors" compile
> option?
>
> --
>
> Elias
>
>



Re: define a global class variable? by Alex

Alex
Wed Mar 03 06:14:58 CST 2004

If you reeealy know what you are doing by using classes, you can use this:

http://frontline.compuware.com/nashua/papers/csupport.htm


"lallous" <lallous@lgwm.org> wrote in message
news:c243b2$1nplp6$1@ID-161723.news.uni-berlin.de...
> Hello
>
> Given a class, how can I define a global variable out of it in a driver
as:
>
>
> CMyClass A;
>
> NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObj, PUNICODE_STRING pRegPath)
> {
> return STATUS_SUCCESS;
> }
>
> The compiler is complaining about atexit()
>
> But when atexit() is supplied as:
> extern "C" int __cdecl atexit(void (__cdecl*)())
>
> {
>
> return 0;
>
> }
>
> I get a warning which is treated as an error:
>
> driver.obj : warning LNK4210: .CRT section exists; there may be unhandled
> static initializers or terminators
>
>
>
> How to fix this? and how to remove the "treat warning as errors" compile
> option?
>
> --
>
> Elias
>
>



Re: define a global class variable? by Ray

Ray
Wed Mar 03 11:51:12 CST 2004

Am I the only one slightly annoyed by the way this supposedly public
article points to a source file with tons of warnings of the form
"confidential information, don't use without written permission"?

Alex wrote:

> If you reeealy know what you are doing by using classes, you can use this:
>
> http://frontline.compuware.com/nashua/papers/csupport.htm
>
>
> "lallous" <lallous@lgwm.org> wrote in message
> news:c243b2$1nplp6$1@ID-161723.news.uni-berlin.de...
>
>>Hello
>>
>>Given a class, how can I define a global variable out of it in a driver
>
> as:
>
>>
>>CMyClass A;
>>
>>NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObj, PUNICODE_STRING pRegPath)
>>{
>> return STATUS_SUCCESS;
>>}
>>
>>The compiler is complaining about atexit()
>>
>>But when atexit() is supplied as:
>>extern "C" int __cdecl atexit(void (__cdecl*)())
>>
>>{
>>
>>return 0;
>>
>>}
>>
>>I get a warning which is treated as an error:
>>
>>driver.obj : warning LNK4210: .CRT section exists; there may be unhandled
>>static initializers or terminators
>>
>>
>>
>>How to fix this? and how to remove the "treat warning as errors" compile
>>option?
>>
>>--
>>
>>Elias
>>
>>
>
>
>

--
../ray\..

Re: define a global class variable? by Alex

Alex
Wed Mar 03 12:45:28 CST 2004

This one better?
http://www.hollistech.com/Resources/Cpp/kernel_c_runtime_library.htm

"Ray Trent" <rat@synaptics.com.spamblock> wrote in message
news:OENA4gUAEHA.2300@TK2MSFTNGP10.phx.gbl...
> Am I the only one slightly annoyed by the way this supposedly public
> article points to a source file with tons of warnings of the form
> "confidential information, don't use without written permission"?
>
> Alex wrote:
>
> > If you reeealy know what you are doing by using classes, you can use
this:
> >
> > http://frontline.compuware.com/nashua/papers/csupport.htm
> >
> >
> > "lallous" <lallous@lgwm.org> wrote in message
> > news:c243b2$1nplp6$1@ID-161723.news.uni-berlin.de...
> >
> >>Hello
> >>
> >>Given a class, how can I define a global variable out of it in a driver
> >
> > as:
> >
> >>
> >>CMyClass A;
> >>
> >>NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObj, PUNICODE_STRING
pRegPath)
> >>{
> >> return STATUS_SUCCESS;
> >>}
> >>
> >>The compiler is complaining about atexit()
> >>
> >>But when atexit() is supplied as:
> >>extern "C" int __cdecl atexit(void (__cdecl*)())
> >>
> >>{
> >>
> >>return 0;
> >>
> >>}
> >>
> >>I get a warning which is treated as an error:
> >>
> >>driver.obj : warning LNK4210: .CRT section exists; there may be
unhandled
> >>static initializers or terminators
> >>
> >>
> >>
> >>How to fix this? and how to remove the "treat warning as errors" compile
> >>option?
> >>
> >>--
> >>
> >>Elias
> >>
> >>
> >
> >
> >
>
> --
> ../ray\..



Re: define a global class variable? by Ray

Ray
Wed Mar 03 15:57:48 CST 2004

Actually, that one is considerably worse, since it's licensed under the
lesser GPL (the "lesser" part is irrelevant for code you compile into
your program, so it's essentially under the full GPL).

I wasn't really complaining about NuMega's page... It's generous of them
to make it publically available, and I don't *really* think that they
would go after anyone for using it... it just seems like an unnecessary
bit of FUD to add to the world...

Alex wrote:

> This one better?
> http://www.hollistech.com/Resources/Cpp/kernel_c_runtime_library.htm
>
> "Ray Trent" <rat@synaptics.com.spamblock> wrote in message
> news:OENA4gUAEHA.2300@TK2MSFTNGP10.phx.gbl...
>
>>Am I the only one slightly annoyed by the way this supposedly public
>>article points to a source file with tons of warnings of the form
>>"confidential information, don't use without written permission"?
>>
>>Alex wrote:
>>
>>
>>>If you reeealy know what you are doing by using classes, you can use
>
> this:
>
>>>http://frontline.compuware.com/nashua/papers/csupport.htm
>>>
>>>
>>>"lallous" <lallous@lgwm.org> wrote in message
>>>news:c243b2$1nplp6$1@ID-161723.news.uni-berlin.de...
>>>
>>>
>>>>Hello
>>>>
>>>>Given a class, how can I define a global variable out of it in a driver
>>>
>>>as:
>>>
>>>
>>>>CMyClass A;
>>>>
>>>>NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObj, PUNICODE_STRING
>
> pRegPath)
>
>>>>{
>>>> return STATUS_SUCCESS;
>>>>}
>>>>
>>>>The compiler is complaining about atexit()
>>>>
>>>>But when atexit() is supplied as:
>>>>extern "C" int __cdecl atexit(void (__cdecl*)())
>>>>
>>>>{
>>>>
>>>>return 0;
>>>>
>>>>}
>>>>
>>>>I get a warning which is treated as an error:
>>>>
>>>>driver.obj : warning LNK4210: .CRT section exists; there may be
>
> unhandled
>
>>>>static initializers or terminators
>>>>
>>>>
>>>>
>>>>How to fix this? and how to remove the "treat warning as errors" compile
>>>>option?
>>>>
>>>>--
>>>>
>>>>Elias
>>>>
>>>>
>>>
>>>
>>>
>>--
>>../ray\..
>
>
>

--
../ray\..

Re: define a global class variable? by Mark

Mark
Thu Mar 04 05:29:40 CST 2004

Unless you plan to sell my c++ runtime source code the gpl is a non-issue.
I'm sorry its free. The folks at Numega should be ashamed as well. Maybe we
will both take this stuff out of the public domain 'cause it annoys you so
much.

--

=====================
Mark Roddy
Windows 2003/XP/2000 Consulting
Hollis Technology Solutions 603-321-1032
www.hollistech.com
markr@hollistech.com


"Ray Trent" <rat@synaptics.com.spamblock> wrote in message
news:e4sdSqWAEHA.3284@TK2MSFTNGP09.phx.gbl...
> Actually, that one is considerably worse, since it's licensed under the
> lesser GPL (the "lesser" part is irrelevant for code you compile into
> your program, so it's essentially under the full GPL).
>
> I wasn't really complaining about NuMega's page... It's generous of them
> to make it publically available, and I don't *really* think that they
> would go after anyone for using it... it just seems like an unnecessary
> bit of FUD to add to the world...
>
> Alex wrote:
>
> > This one better?
> > http://www.hollistech.com/Resources/Cpp/kernel_c_runtime_library.htm
> >
> > "Ray Trent" <rat@synaptics.com.spamblock> wrote in message
> > news:OENA4gUAEHA.2300@TK2MSFTNGP10.phx.gbl...
> >
> >>Am I the only one slightly annoyed by the way this supposedly public
> >>article points to a source file with tons of warnings of the form
> >>"confidential information, don't use without written permission"?
> >>
> >>Alex wrote:
> >>
> >>
> >>>If you reeealy know what you are doing by using classes, you can use
> >
> > this:
> >
> >>>http://frontline.compuware.com/nashua/papers/csupport.htm
> >>>
> >>>
> >>>"lallous" <lallous@lgwm.org> wrote in message
> >>>news:c243b2$1nplp6$1@ID-161723.news.uni-berlin.de...
> >>>
> >>>
> >>>>Hello
> >>>>
> >>>>Given a class, how can I define a global variable out of it in a
driver
> >>>
> >>>as:
> >>>
> >>>
> >>>>CMyClass A;
> >>>>
> >>>>NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObj, PUNICODE_STRING
> >
> > pRegPath)
> >
> >>>>{
> >>>> return STATUS_SUCCESS;
> >>>>}
> >>>>
> >>>>The compiler is complaining about atexit()
> >>>>
> >>>>But when atexit() is supplied as:
> >>>>extern "C" int __cdecl atexit(void (__cdecl*)())
> >>>>
> >>>>{
> >>>>
> >>>>return 0;
> >>>>
> >>>>}
> >>>>
> >>>>I get a warning which is treated as an error:
> >>>>
> >>>>driver.obj : warning LNK4210: .CRT section exists; there may be
> >
> > unhandled
> >
> >>>>static initializers or terminators
> >>>>
> >>>>
> >>>>
> >>>>How to fix this? and how to remove the "treat warning as errors"
compile
> >>>>option?
> >>>>
> >>>>--
> >>>>
> >>>>Elias
> >>>>
> >>>>
> >>>
> >>>
> >>>
> >>--
> >>../ray\..
> >
> >
> >
>
> --
> ../ray\..



Re: define a global class variable? by Ray

Ray
Thu Mar 04 11:15:14 CST 2004

I greatly appreciate people that are willing to put sample code into the
public domain. I even appreciate people putting out code under the GPL.
Almost all of them mean well. The vast majority of them just don't
understand the actual legal ramifications.

Please don't take my comments as criticism of people's good works.
However, if it were actually in the public domain, it wouldn't have GPL
or other copyright restrictions.

That said, if anyone out there thinks that the GPL or other copyright
notice is a "non-issue", think again. Unless you're content with your
driver potentially being forced open-source, or at least being liable
for copyright infringement, you need to be extremely *careful* about
*how* you use any GPL'd code, lesser GPL or not.

For example, the LGPL requires that if your program links to the
library, you must include a prominant notice indicating the use of the
library and noting any modifications, a copy of the LGPL, etc. There are
lots of other requirements: if you don't use a DLL-like mechanism and
ship the library as a separate file, you must provide an object module
that could be linked to a modified version of the library to recreate a
working program. If you fail to follow any of these requirements, you
might well be violating the copyright on the library.

Obviously, I sincerely doubt Mark would enforce any of these
requirements... in fact, I somewhat suspect he's unaware of them. That's
not the point.

BTW, I'm not a lawyer and don't pretend to be giving any kind of legal
advise... I've merely talked with a lot of lawyers about this, and in
fact went to seminar at a law firm on this issue just last week... which
probably explains my paranoia.

Mark Roddy wrote:

> Unless you plan to sell my c++ runtime source code the gpl is a non-issue.
> I'm sorry its free. The folks at Numega should be ashamed as well. Maybe we
> will both take this stuff out of the public domain 'cause it annoys you so
> much.
>

--
../ray\..

Re: define a global class variable? by J

J
Fri Mar 05 02:09:02 CST 2004

lallous wrote:
> Hello

Hi Iallous,

> Given a class, how can I define a global variable out of it in a
> driver as:
>
> CMyClass A;
>
> NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObj, PUNICODE_STRING
> pRegPath) {
> return STATUS_SUCCESS;
> }
>
> The compiler is complaining about atexit()

Don't know if it fits your needs, but I would play it as follows (with
the appropriate operator new, of course):

CMyClass *pA;

NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObj, PUNICODE_STRING
pRegPath) {
pA = new CMyClass;
return STATUS_SUCCESS;
}

and I would delete pA when my driver exits. This way, you should avoid
initialization issues regarding global objects.

HTH,
--
Jean-Paul Iribarren (aka JPI)
Anti-spam: ma véritable adresse électronique ne comporte pas de
chiffres;donc...
Anti-spam: my real e-mail address has no digits; consequently...


Re: define a global class variable? by lallous

lallous
Fri Mar 05 07:36:29 CST 2004

Hello

Thanks all for your replies.

I solved it with placement new and it apparently works after some tests,
what are your thoughts?

driver.cpp:
extern "C"
{
#include <ntddk.h>
#include <new.h>
}

class CMyClass
{
private:
enum {ARRAY_SIZE = 100};
int a[ARRAY_SIZE];
int *idx;
public:
CMyClass();
void Fill(int ch);
void dtor();
~CMyClass();
};

void CMyClass::dtor()
{
Fill(-1);
}

CMyClass::~CMyClass()
{
dtor();
}

void CMyClass::Fill(int ch)
{
for (int i=0;i<ARRAY_SIZE;i++)
a[i] = ch;
}


CMyClass::CMyClass()
{
Fill(1);
}

template <class cls> cls *CreateClassInstance()
{
void * p = ExAllocatePool(NonPagedPool, sizeof(cls) + 100); // for
overhead
return new (p) cls;
}

template <class cls> void DestroyClassInstance(cls *c)
{
if (c != NULL)
{
c->dtor();
ExFreePool((PVOID)c);
}
}

CMyClass *cls;

extern "C" NTSTATUS DriverEntry(
IN PDRIVER_OBJECT pDriverObject,
IN PUNICODE_STRING pRegPath)
{

cls = CreateClassInstance<CMyClass>(); // will call ctor()

cls->Fill(0x11223344); // test a method and see if member variables are ok

DestroyClassInstance(cls); // calls dtor() and frees instance

return STATUS_FAILED_DRIVER_ENTRY; // don't load driver
}

On DriverUnload() I would call DestroyClassInstance(theGlobalInstance)

--
Elias
"lallous" <lallous@lgwm.org> wrote in message
news:c243b2$1nplp6$1@ID-161723.news.uni-berlin.de...
> Hello
>
> Given a class, how can I define a global variable out of it in a driver
as:
>
>
> CMyClass A;
>
> NTSTATUS DriverEntry(PDRIVER_OBJECT pDriverObj, PUNICODE_STRING pRegPath)
> {
> return STATUS_SUCCESS;
> }
>
> The compiler is complaining about atexit()
>
> But when atexit() is supplied as:
> extern "C" int __cdecl atexit(void (__cdecl*)())
>
> {
>
> return 0;
>
> }
>
> I get a warning which is treated as an error:
>
> driver.obj : warning LNK4210: .CRT section exists; there may be unhandled
> static initializers or terminators
>
>
>
> How to fix this? and how to remove the "treat warning as errors" compile
> option?
>
> --
> Elias