Solved

Initialize DLL

Posted on 2001-09-02
14
314 Views
Last Modified: 2007-11-27
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.
0
Comment
Question by:lecossois
  • 5
  • 4
  • 3
  • +2
14 Comments
 
LVL 9

Expert Comment

by:ShaunWilde
Comment Utility
include a DllMain in your Dll
0
 

Author Comment

by:lecossois
Comment Utility
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
 
LVL 9

Expert Comment

by:ShaunWilde
Comment Utility
is this an MFC DLL by any chance ?

if so use the override of CWinApp::InitInstance to do your inititializations
0
 

Author Comment

by:lecossois
Comment Utility
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
 
LVL 9

Expert Comment

by:ShaunWilde
Comment Utility
is this funtion in your header (.h) or in a .cpp file?
0
 

Author Comment

by:lecossois
Comment Utility
I could only find it in DLLMODUL.CPP.
0
 
LVL 2

Expert Comment

by:nahumd
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:lecossois
Comment Utility
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
 
LVL 9

Expert Comment

by:ShaunWilde
Comment Utility
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
 
LVL 2

Expert Comment

by:abesoft
Comment Utility
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
 
LVL 3

Accepted Solution

by:
romanm earned 100 total points
Comment Utility
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
 

Author Comment

by:lecossois
Comment Utility
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
 
LVL 3

Expert Comment

by:romanm
Comment Utility
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
 
LVL 3

Expert Comment

by:romanm
Comment Utility
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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now