Solved

What am I allowed to do in "DllMain"?

Posted on 2004-03-30
3
486 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
3 Comments
 
LVL 9

Accepted Solution

by:
_ys_ earned 45 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

  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 …
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 how to clear a vector as well as how to detect empty vectors in C++.

772 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

9 Experts available now in Live!

Get 1:1 Help Now