Initialize DLL

Hi,

I would like to do a first-time initialization of a bunch of variables in a DLL.  Ideally, this should be done when the DLL is loaded in memory, so that the variables are ready by the time a function from the DLL is called.

Is there a way to do this first-time initialization (e.g. using something like a onLoad() hook) ?

Thanks,

Lecossois.
lecossoisAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
romanmConnect With a Mentor Commented:
are you crazy? global variables, mutex????

first resolve your linking problems,
why do you include <afx.h>? if you're not using MFC, don't
include <windows.h>
and in the project settings in the general tab change the "use MFC in a..." to "not using MFC...".

now inside the DllMain:

if( fdwReason == DLL_PROCESS_ATTACH ){
// one time init code goes here...
}
0
 
ShaunWildeCommented:
include a DllMain in your Dll
0
 
lecossoisAuthor Commented:
I get a linker error that DllMain is declared twice.  Do you know where this comes from ?  How can I override the first declaration (I don't even know where that one is declared) ?

0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
ShaunWildeCommented:
is this an MFC DLL by any chance ?

if so use the override of CWinApp::InitInstance to do your inititializations
0
 
lecossoisAuthor Commented:
I will give some more information on what I try to achieve, maybe that will help.  

I'm not a very good C developer, but I was forced to because I needed a specific DLL for a product we use (Siteminder).  The DLL structure looks like this :

--------------------------------------
#include <string.h>
#include "SmApi.h"
#ifdef _WIN32
#include <afx.h>
#include <ldap.h>
#else
extern "C" {
#endif



int SM_EXTERN Afunction()
{
    /* code.... */
    return 1;
}



#ifndef _WIN32
}
#endif
--------------------------------------

Not very complicated, I thought.  But when I add the DllMain function :

BOOL WINAPI DllMain(
    HINSTANCE hinstDLL,  // handle to DLL module
    DWORD fdwReason,     // reason for calling function
    LPVOID lpReserved )  // reserved

I get the linking error :

Linking...
nafxcw.lib(dllmodul.obj) : error LNK2005: _DllMain@12 already defined in smazapi.obj
nafxcw.lib(dllmodul.obj) : warning LNK4006: _DllMain@12 already defined in smazapi.obj; second definition ignored
   Creating library Release/smazapi.lib and object Release/smazapi.exp
Release/smazapi.dll : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.


Any idea ?  I'm willing to increase the points if it becomes too complicated.

Thanks,

Lecossois.

0
 
ShaunWildeCommented:
is this funtion in your header (.h) or in a .cpp file?
0
 
lecossoisAuthor Commented:
I could only find it in DLLMODUL.CPP.
0
 
nahumdCommented:
You can also declare a static variable in the entry point of your dll and execute an initialization code only if it is 0 , and then set it to 1 (for example).
This way it will execute only once.
0
 
lecossoisAuthor Commented:
I also thought of that solution, but the problem is that the code must be thread-safe.  I know I have to use mutexes, but I have no idea how this is done in C...

Lecossois.

0
 
ShaunWildeCommented:
are you linking with MFC?

> I know I have to use mutexes

or critical sections.

if it is a mutex you can do this (without a function)

HANDLE hMutex = ::CreateMutex(NULL,TRUE,"MyMutex");

void myFunc()
{
   // use mutex
}



0
 
abesoftCommented:
Since the source includes:
     #include <afx.h>

then you are using MFC, and so you should override CWinApp::InitInstance as ShaunWilde suggested.  You will probably have some class derived from CWinApp, and you can modify this class (either adding the InitInstance or modifying one that is already defined.)
0
 
lecossoisAuthor Commented:
Thanks Romanm,

I still needed windows.h, otherwise the compiler wouldn't accept my declaration of DllMain.

But it's working now.  Thanks,

Lecossois.
0
 
romanmCommented:
yes, yes, english syntax:
why do you include <afx.h>?
if you're not using MFC, don't include <afx.h>.
include <windows.h>.

sorry for the mistake.

0
 
romanmCommented:
also,
there are other reasons for the DllMain to be called:

DLL_THREAD_ATTACH // whenever a new thread is created by the process.

DLL_THREAD_DETACH // whenever a previously created thread terminates.

and last:
DLL_PROCESS_DETACH // when unloading the dll from the process.

thought you should know.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.