473,386 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Static initializers with option /CLR in dlls

Hi,

I have a relatively large C++ code base, which requires that static
initializers are called when a dll is loaded. This seems to work well
for native code, but it does not work for files compiled with the /CLR
option enabled. I will try to give a simple example that illustrates my
problem.

Library code:
-----------
#include <cstdio>
static int dummy = std::puts("initializer called");

Program code:
-----------------
#include "windows.h"
int main()
{
LoadLibrary("library.dll");
return 0;
}

The program prints, "initializer called", when the library is
compiled without the /CLR option, but it prints nothing if the library
is compiled with the /CLR option. My initial thought was that the
linker eliminated the unreferenced 'dummy' symbol, but this does
not seem to be the case because I can locate the 'dummy'
initializer in the 'library.dll'. Does anybody know why the CLR
loader does not call the 'dummy' initializer when the library is
loaded, or does anybody know how I force the CLR loader to call the
'dummy' initializer without referring directly to the 'dummy'
symbol itself?

Many thanks in advance for any help!
Jesper

Oct 30 '06 #1
5 1759
"Jesper Schmidt" <sc******@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
I have a relatively large C++ code base, which requires that static
initializers are called when a dll is loaded. This seems to work well
for native code, but it does not work for files compiled with the /CLR
option enabled. I will try to give a simple example that illustrates my
problem.
Which version of the compiler are you using?

Have you run your application compiled for the /CLR case in both debug and
release configurations?

You see, .Net can initialize an assembly that contains only managed code on
its own. But for one which contains native code as well, it has to use
Windows' loader. The loader uses a critical section to insure that no code
in any DLL runs before its DllMain() sees "attach" notifications. That can
give rise to deadlock situations because the compiler sees to it that static
initializes run while processing the DLL_PROCESS_ATTACH notification:

http://msdn.microsoft.com/library/de...ingproblem.asp

Under VS2003, when one gets into that situation, one often receives cryptic
error messages for which a workaround is described here:

http://support.microsoft.com/kb/814472/en-us

But because you didn't mention those messages, I'm assuming you are using
VS2005.

I still use 2003 for my mixed-mode DLLs so take this with a grain of salt
but I _think_ that VS2005 warns one in debug mode when one does proscribed
things and simply chooses not to do them in release mode. I'm wondering if
ran into that problem.

But you didn't mention seeing a warning. That leaves me confused as to which
compiler you are using. :-)

Finally - and this is just thinking out loud - you might want to put a call
to OutputDebugString() where you have

std::puts("initializer called");

now. I'm wondering whether it could be the case that the project you are
using does not yet have the console I/O stuff initialized, either because
it's of the wrong type or for some other reason.

Regards,
Will
Oct 30 '06 #2
William DePalo [MVP VC++] wrote:
"Jesper Schmidt" <sc******@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
I have a relatively large C++ code base, which requires that static
initializers are called when a dll is loaded. This seems to work well
for native code, but it does not work for files compiled with the /CLR
option enabled. I will try to give a simple example that illustrates my
problem.

Which version of the compiler are you using?
I am using Microsoft Visual Studio 2005.
Have you run your application compiled for the /CLR case in both debug and
release configurations?
Yes, I get the same result in both debug and release.
You see, .Net can initialize an assembly that contains only managed code on
its own. But for one which contains native code as well, it has to use
Windows' loader. The loader uses a critical section to insure that no code
in any DLL runs before its DllMain() sees "attach" notifications. That can
give rise to deadlock situations because the compiler sees to it that static
initializes run while processing the DLL_PROCESS_ATTACH notification:

http://msdn.microsoft.com/library/de...ingproblem.asp

Under VS2003, when one gets into that situation, one often receives cryptic
error messages for which a workaround is described here:

http://support.microsoft.com/kb/814472/en-us

But because you didn't mention those messages, I'm assuming you are using
VS2005.

I still use 2003 for my mixed-mode DLLs so take this with a grain of salt
but I _think_ that VS2005 warns one in debug mode when one does proscribed
things and simply chooses not to do them in release mode. I'm wondering if
ran into that problem.
I know of this problem, but I do not think that this is the case here.
I think that my problem has to do with some kind of
just-in-time-initialization performed by the clr code. I think that the
call to the 'dummy' initializer is deferred to the first time that
'dummy' is referenced. I have tried to proof this by accessing 'dummy'
through a function call from the 'main' function. This does indeed
trigger the initialization of 'dummy', but it does not solve my
problem, because the test framework that I am working on does not know
any functions in the test dll(s) that it loads. Instead, each test case
installs itself into the test framework with the help of a static
initializer.

--
Jesper

Oct 30 '06 #3
I know of this problem, but I do not think that this is the case here.
I think that my problem has to do with some kind of
just-in-time-initialization performed by the clr code. I think that the
call to the 'dummy' initializer is deferred to the first time that
'dummy' is referenced. I have tried to proof this by accessing 'dummy'
through a function call from the 'main' function. This does indeed
trigger the initialization of 'dummy', but it does not solve my
problem, because the test framework that I am working on does not know
any functions in the test dll(s) that it loads. Instead, each test case
installs itself into the test framework with the help of a static
initializer.
Does __crt_dll_initialize() solve your problem? (you don't then need to know
how many static variables there are).

See http://support.microsoft.com/kb/814472. Although, all these problems
were supposed to go away in VC++ 2005.

Another possibility is to use #pragma managed(push, off) ... declare static
initializers ... #pragma managed(pop)
>
--
Jesper

Oct 31 '06 #4

Ben Voigt wrote:
I know of this problem, but I do not think that this is the case here.
I think that my problem has to do with some kind of
just-in-time-initialization performed by the clr code. I think that the
call to the 'dummy' initializer is deferred to the first time that
'dummy' is referenced. I have tried to proof this by accessing 'dummy'
through a function call from the 'main' function. This does indeed
trigger the initialization of 'dummy', but it does not solve my
problem, because the test framework that I am working on does not know
any functions in the test dll(s) that it loads. Instead, each test case
installs itself into the test framework with the help of a static
initializer.

Another possibility is to use #pragma managed(push, off) ... declare static
initializers ... #pragma managed(pop)
I have tried this, but it does not help. Apparently, the unmanaged
intializer exhibits the same just-in-time-initialization as the managed
initializer. Loading the module still does not trigger the 'dummy'
initializer. It looks as if the initialization is deferred until the
module code actually is referenced/called. I would be grateful if
someone could verify this?

--
Jesper

Oct 31 '06 #5

Ben Voigt wrote:
I know of this problem, but I do not think that this is the case here.
I think that my problem has to do with some kind of
just-in-time-initialization performed by the clr code. I think that the
call to the 'dummy' initializer is deferred to the first time that
'dummy' is referenced. I have tried to proof this by accessing 'dummy'
through a function call from the 'main' function. This does indeed
trigger the initialization of 'dummy', but it does not solve my
problem, because the test framework that I am working on does not know
any functions in the test dll(s) that it loads. Instead, each test case
installs itself into the test framework with the help of a static
initializer.

Another possibility is to use #pragma managed(push, off) ... declare static
initializers ... #pragma managed(pop)
I have tried this, but it does not help. Apparently, the unmanaged
intializer exhibits the same just-in-time-initialization as the managed
initializer. Loading the module still does not trigger the 'dummy'
initializer. It looks as if the initialization is deferred until the
module code actually is referenced/called. I would be grateful if
someone could verify this?

--
Jesper

Oct 31 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: A J Le Couteur Bisson | last post by:
Could someone please confirm that static class constructors are only called at the first use of a non-static constructor on the class, or am I doing something wrong? If this is indeed the case...
3
by: Dave | last post by:
Hi everyone, Is it possible, using an Attribute or by some other means, to notify the C# Compiler to serialize all static field's that have initializers before code in an explicit static...
3
by: Ryan Steckler | last post by:
I found this behavior while trying to implement a singleton class. Below is a somewhat more straight forward example, though admittedly less useful in real life. The basic problem is that when a...
3
by: Bonj | last post by:
(using 2005 beta 1 SDK) what tool is used to link static libraries? I am trying to create a static library, but when I run link /out:"project1.lib" project1.obj it gives me 'could not open file...
10
by: Ni | last post by:
I found out smth strange in g++ >= 3.3.5 => I was able to do register char buff;//// where outSTDBuffer is a variable!!!! Is it a bug in gcc or specification of c++ changed?? I've noticed this...
6
by: Stephen Walch | last post by:
Our application environment consists of three basic layers: 1. Third-party unmanaged DLLs that were written before the CLR was invented and maintain a significant amount of information (including...
1
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
1
by: Visame | last post by:
The following example is from C++ Standard(ISO14882) 9.4.2 Static data members class process { static process* run_chain; static process* running; }; process* process::running =...
16
by: Joe Strout | last post by:
One thing I miss as I move from REALbasic to Python is the ability to have static storage within a method -- i.e. storage that is persistent between calls, but not visible outside the method. I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.