473,698 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("init ializer called");

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

The program prints, "initialize r 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 1778
"Jesper Schmidt" <sc******@gmail .comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.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_ATT ACH 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 OutputDebugStri ng() where you have

std::puts("init ializer 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.goo glegroups.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_ATT ACH 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_initi alize() 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
2427
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 then I would consider this a serious error in the language implementation, not to mention a pain in the backside :( Also, is it to much to ask that the method Type.GetTypeFromCLSID be documented
3
2805
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 constructor? Example: public class MyClass {
3
5791
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 static property of a class is called (StaticInit.MyProperty), the property code executes BEFORE the static members are initialized. This would lead one to assume that the static members would equate to null, but that isn't the case either. They...
3
1322
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 'project1.lib'' - as if it's thinking that is an input file? I can't use the /DLL option as then it will create a .dll, when I want to be able to link all the functions into the app with all the code in the .lib file, not the DLL. I noticed there...
10
2768
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 a mistake after successfull compilation of my program! Should I change the code. I think `register char buff;` is much safer then use pointers in case if outSTDBuffer is small!!!
6
4122
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 memory management and connection pooling constructs) as static variables which were intended to scoped to the process. 2. Managed C++ assemblies (.NET 1.1) that wrap the unmanaged DLLs as nice neat classes with managed interfaces.
1
3531
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 following (with incomplete succes): I want in a given namespace Parameters a list of "initializers" (which are objects derived from a simple interface that can be implemented anywhere, and are used to define which parameters the program will take at...
1
2415
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 = get_main();//what does get_main() mean here? process* process::run_chain = running; C++ Standard also says in:
16
2047
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 frequently use this for such things as caching, or for keeping track of how many objects a factory function has created, and so on. Today it occurred to me to use a mutable object as the default value of a parameter. A simple example: def...
0
8609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7739
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.