Solved

What am I allowed to do in "DllMain"?

Posted on 2004-03-30
3
511 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 73
computer science syllabus 3 117
Issues with C++ Class 19 121
How to determine if the result of an equation is an integer in C++? 3 32
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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.

752 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