Automatic Initialisation of an Assembly

K

Kevin Frey

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
 
C

Crest Teethgel

05 Mar 2007 18:13:15 -0500 said:
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
 
K

Kevin Frey

Thanks Crest, although this *is* a blatant copy of the exact MSDN
documentation noted here:

http://msdn2.microsoft.com/en-us/library/ms173266(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
 
K

Kevin Frey

Hi Pavel,

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

Thanks

Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top