Solved

Initialize DLL

Posted on 2001-09-02
14
320 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +2
14 Comments
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 6449925
include a DllMain in your Dll
0
 

Author Comment

by:lecossois
ID: 6450025
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
ID: 6450365
is this an MFC DLL by any chance ?

if so use the override of CWinApp::InitInstance to do your inititializations
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:lecossois
ID: 6450491
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
ID: 6450592
is this funtion in your header (.h) or in a .cpp file?
0
 

Author Comment

by:lecossois
ID: 6450617
I could only find it in DLLMODUL.CPP.
0
 
LVL 2

Expert Comment

by:nahumd
ID: 6450641
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
 

Author Comment

by:lecossois
ID: 6450657
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
ID: 6450676
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
ID: 6450850
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
ID: 6456637
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
ID: 6456717
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
ID: 6456737
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
ID: 6456751
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

738 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