?
Solved

Initialize DLL

Posted on 2001-09-02
14
Medium Priority
?
323 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 400 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month12 days, 13 hours left to enroll

777 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