473,398 Members | 2,165 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,398 software developers and data experts.

Automatic Initialisation of an Assembly

I have an assembly written in C++/CLI that also links in non-clr Native C++
(the C++/CLI wraps the Native C++ functionality).

This assembly has an in-built tracing system that needs to be initialised,
amongst other things. At present the user of the assembly needs to do this
prior to invoking any other functionality, by calling a static member
function of a managed class.

I would like this initialisation to occur automatically if at all possible.
The simplest idea would be to have DllMain call the static member function,
but I believe that it is illegal to have DllMain call any managed code,
because of the mixed-mode loader lock issue.

So my question is, what alternatives exist for me to achieve my
initialisation without forcing the user of the assembly to do it explicitly?

And, a related question, when are native C++ static variables initialised in
relation to DLLMain, and initialisation of the CRT? I'm not sure if this
would help me though because of potential order-of-initialisation
consequences.

Thanks

Kevin
Mar 5 '07 #1
4 1634
05 Mar 2007 18:13:15 -0500, Kevin Frey <ke**********@hotmail.comwrote:
I would like this initialisation to occur automatically if at all possible.
The simplest idea would be to have DllMain call the static member function,
but I believe that it is illegal to have DllMain call any managed code,
because of the mixed-mode loader lock issue.

So my question is, what alternatives exist for me to achieve my
initialisation without forcing the user of the assembly to do it explicitly?

You can avoid loader lock by moving code from DllMain into the constructor of a global object.

// initializing_mixed_assemblies.cpp
// compile with: /clr /LD
#pragma once
#include <stdio.h>
#include <windows.h>
struct __declspec(dllexport) A {
A() {
System::Console::WriteLine("Module ctor initializing based on global instance of class.\n");
}

void Test() {
printf("Test called so linker does not throw away unused object.\n");
}
};

#pragma unmanaged
// Global instance of object
A obj;

extern "C"
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) {
// Remove all managed code from here and put it in constructor of A.
return true;
}

// initializing_mixed_assemblies_2.cpp
// compile with: /clr initializing_mixed_assemblies.lib
#include <windows.h>
using namespace System;
#include <stdio.h>
#using "initializing_mixed_assemblies.dll"
struct __declspec(dllimport) A {
void Test();
};

int main() {
A obj;
obj.Test();
}

Best regards

Crest
Mar 6 '07 #2
Explained here:
http://msdn2.microsoft.com/en-us/library/ms682583.aspx
http://download.microsoft.com/downlo...L_bestprac.doc

Regards,
--PA

"Kevin Frey" wrote:
I have an assembly written in C++/CLI that also links in non-clr Native C++
(the C++/CLI wraps the Native C++ functionality).

This assembly has an in-built tracing system that needs to be initialised,
amongst other things. At present the user of the assembly needs to do this
prior to invoking any other functionality, by calling a static member
function of a managed class.

I would like this initialisation to occur automatically if at all possible.
The simplest idea would be to have DllMain call the static member function,
but I believe that it is illegal to have DllMain call any managed code,
because of the mixed-mode loader lock issue.

So my question is, what alternatives exist for me to achieve my
initialisation without forcing the user of the assembly to do it explicitly?

And, a related question, when are native C++ static variables initialised in
relation to DLLMain, and initialisation of the CRT? I'm not sure if this
would help me though because of potential order-of-initialisation
consequences.

Thanks

Kevin
Mar 6 '07 #3
Hi Pavel,

The references given don't seem to talk about native/managed code at all.
Any other ideas?

Thanks

Kevin

"Pavel A." <pa*****@NOwritemeNO.comwrote in message
news:9F**********************************@microsof t.com...
Explained here:
http://msdn2.microsoft.com/en-us/library/ms682583.aspx
http://download.microsoft.com/downlo...L_bestprac.doc

Regards,
--PA

"Kevin Frey" wrote:
>I have an assembly written in C++/CLI that also links in non-clr Native
C++
(the C++/CLI wraps the Native C++ functionality).

This assembly has an in-built tracing system that needs to be
initialised,
amongst other things. At present the user of the assembly needs to do
this
prior to invoking any other functionality, by calling a static member
function of a managed class.

I would like this initialisation to occur automatically if at all
possible.
The simplest idea would be to have DllMain call the static member
function,
but I believe that it is illegal to have DllMain call any managed code,
because of the mixed-mode loader lock issue.

So my question is, what alternatives exist for me to achieve my
initialisation without forcing the user of the assembly to do it
explicitly?

And, a related question, when are native C++ static variables initialised
in
relation to DLLMain, and initialisation of the CRT? I'm not sure if this
would help me though because of potential order-of-initialisation
consequences.

Thanks

Kevin

Mar 7 '07 #4
Thanks Crest, although this *is* a blatant copy of the exact MSDN
documentation noted here:

http://msdn2.microsoft.com/en-us/lib...66(VS.80).aspx

However, how is what you have shown me providing "automatic initialisation"?
I still need my main-line to instantiate an object and call a method within
the assembly. My current approach simply requires the calling of a static
method.

What am I missing?

Kevin

"Crest Teethgel" <crest@teethgelwrote in message
news:op.toqvprhx1vz4pt@localhost...
05 Mar 2007 18:13:15 -0500, Kevin Frey <ke**********@hotmail.comwrote:
>I would like this initialisation to occur automatically if at all
possible.
The simplest idea would be to have DllMain call the static member
function,
but I believe that it is illegal to have DllMain call any managed code,
because of the mixed-mode loader lock issue.

So my question is, what alternatives exist for me to achieve my
initialisation without forcing the user of the assembly to do it
explicitly?


You can avoid loader lock by moving code from DllMain into the constructor
of a global object.

// initializing_mixed_assemblies.cpp
// compile with: /clr /LD
#pragma once
#include <stdio.h>
#include <windows.h>
struct __declspec(dllexport) A {
A() {
System::Console::WriteLine("Module ctor initializing based on global
instance of class.\n");
}

void Test() {
printf("Test called so linker does not throw away unused
object.\n");
}
};

#pragma unmanaged
// Global instance of object
A obj;

extern "C"
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID
lpReserved) {
// Remove all managed code from here and put it in constructor of A.
return true;
}

// initializing_mixed_assemblies_2.cpp
// compile with: /clr initializing_mixed_assemblies.lib
#include <windows.h>
using namespace System;
#include <stdio.h>
#using "initializing_mixed_assemblies.dll"
struct __declspec(dllimport) A {
void Test();
};

int main() {
A obj;
obj.Test();
}

Best regards

Crest

Mar 7 '07 #5

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

Similar topics

2
by: Tim | last post by:
Please advise if you can. Presumably initialisation of members in member initialisation lists is perfomed by 'C' run-time startup. If the CRT was never started-up would those members be garbage?...
6
by: David Lindgren | last post by:
Hi. I'm using automatic increase on the assembly build number. Only problem is it's doesn't increase. Anyone know what might cause this? In AssemblyInfo.cs: Thanks!
29
by: Natan | last post by:
When you create and aspx page, this is generated by default: using System; using System.Collections; using System.Collections.Specialized; using System.Configuration; using System.Text; using...
3
by: Steven T. Hatton | last post by:
In this code, when will (should) Xfromer::release be called? struct C { C(const char* c) { f = Xformer::xform(c); } ~C() { Xformer::release(f); } const F* get() const { return f; } F* f; }; ...
13
by: Frederick Gotham | last post by:
I have just been reading 8.5 in the Standard, and am trying to make sense of the different kinds of initialisations. Up until now, I thought of an object as either NOT being initialised (i.e....
2
by: Kevin Frey | last post by:
I have an ASP.NET application that references an Assembly called Model. This assembly contains model-centric classes for dealing with database operations. The Model assembly contains a lot of...
2
by: =?Utf-8?B?RnJlZW1hbg==?= | last post by:
Hi, I'm new to vb.net. How to prevent vb.net to automatic execute code in design time? When I open a form design view in VS2005, vs pop up an error message: "Could not load file or assembly...
6
by: Rahul | last post by:
Hi Everyone, I have the following code, class B; class A { public : operator B();
25
by: sidd | last post by:
In the following code: int i = 5; ---it goes to .data segment int j; ---it goes to bss segment int main() { int c; int i = 5; ---stack
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
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...

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.