Solved

What am I allowed to do in "DllMain"?

Posted on 2004-03-30
3
518 Views
Last Modified: 2012-06-27
Can anyone enlighten me about this code? Im trying to make a DLL who initializes some stuff in the DLLMain attaching process. I could of course just move the initializing code to start() or to a new function initialize() or something but I figured that the meaning of DllMain was just for cases like this? Or am I wrong? ... please Im goooing nuuuuts!

#include "stuff.h"

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{                          
      switch(fdwReason)
      {
      case DLL_PROCESS_ATTACH:
// ----- When moving this code segment to "start()", everything works good.. whats the difference?
            Randomize();
            SetSpeaker( 0, 90 );
            SetSpeaker( 1, 270 );
            audio = new RtAudio(&stream, 0, (int)CHANNELS, 0, 0, RtAudio::RTAUDIO_FLOAT64, SAMPLE_RATE, &buffersize, 4);
            audio->setStreamCallback(stream, &RainSynth, NULL);
// ------
            break;

      case DLL_PROCESS_DETACH:
            audio->closeStream(stream);
            break;
      }

      return TRUE;
}

extern "C" __declspec(dllexport) void Start()
{
// ----- When moving the above code segment to this place instead, everything works good.. why?
      audio->startStream(stream);
}

extern "C" __declspec(dllexport) void Stop()
{
      audio->stopStream(stream);
}
0
Comment
Question by:AMZita
[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
3 Comments
 
LVL 9

Accepted Solution

by:
_ys_ earned 45 total points
ID: 10715890
DllMain is non-reentrant ... process wide!!

Should any code create a new thread, dllMain needs to be notified of the new thread. But it's already executing within dllMain and waiting / blocking. This results in a deadlock.

When the code is moved to start, and start invoked outside of dllMain, everything's Ok again.

For the same reason:
audio->closeStream(stream);
may induce another deadlock. DllMain is notified of thread termination as well.

Without the need for a start/stop funciton pair, DisableThreadLibraryCalls can be called to prevent DllMain from receiving these thread notifications:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/disablethreadlibrarycalls.asp
0
 

Author Comment

by:AMZita
ID: 10717076
Sorry for beeing so lame but what is non-reentrant code/process wide?

What would you recommend me to do? (except learning more c++)

When does a new thread initialize?, as far as I new Im only using one DLL-thread so why sould anything block anything at all? Hmmm ... I _am_ going nuts ... Is the best/only way of doing this to implement a Create() and Clear() function to create/clear my "audio" object?

Is a new thread initialized every time I set something like this? ... :

[DllImport("mydll.dll")]
private static extern void SomeFunction();

... or when Im calling SomeFunction() ?
0
 
LVL 14

Assisted Solution

by:wayside
wayside earned 40 total points
ID: 10717298
You should generally do as little as possible in DllMain. In addition to the threading issues mentioned above, you must make certain that LoadLibrary doesn't get called while you are in your DllMain function, as this can lead to all kinds of nastiness.

Unless you know precisely what the side effects of all the calls are going to do, you have to assume the worst. It's quite possible "new RtAudio()" causes lots of other dll's to get loaded, and if you are in your DllMain these might fail/crash/worse.

So yes, move your code out of DllMain and into your Start/Stop functions, or create Create/Clear functions and move it there.

"Reentrant" means that something else calls the same function while you are still in that function.
0

Featured Post

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

630 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