?
Solved

Initialize DLL

Posted on 2001-09-02
14
Medium Priority
?
330 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
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
Industry Leaders: 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

807 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