By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,561 Members | 3,083 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,561 IT Pros & Developers. It's quick & easy.

Automatic Initialisation of an Assembly

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.