0

I'm trying to play music from within a DLL. However, whenever I try this, the PlaySound function does nothing.

I've already attempted having the wav file in a resource and I know that the code works as when I compile the DLLMain as a normal main, it plays the music with no problems. The DLLMain code looks like this:

if (ul_reason_for_call == DLL_PROCESS_ATTACH) {
    PlaySoundA("C:\\Sound.wav", NULL, SND_FILENAME);
}

return TRUE;

The strange thing is, I can see that the song is in the program's memory; the memory usage of it goes up to what it should do when it plays a song, but it has no sound. Also, the same code compiled to an exe works perfectly; I can't figure out what's going wrong.

UPDATE: It turns out this is because you can't execute lots of things in DLLMain - are there any ways around this?

  • 4
    Dont do anything interesting in DllMain: https://learn.microsoft.com/en-us/windows/win32/dlls/dllmain – Mike Vine Aug 23 '19 at 16:09

1 Answers1

2

You can only do a limited number of things in DllMain, and I'm pretty sure that PlaySound isn't one of them.

Instead, try calling it from another function in the DLL.

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
  • Are there any ways of disabling the restrictions of DLLMain? I'm loading the DLL from a program which I have no control over, so I can't make it call another function from the DLL. If there aren't, are there any ways of making a process call a DLL function? – Aperture32 Aug 23 '19 at 16:30
  • @Aperture32 "_Are there any ways of disabling the restrictions of DLLMain?_" No. Read linked materials for the reasons, about why they exist in the first place. – Algirdas Preidžius Aug 23 '19 at 16:35
  • @AlgirdasPreidžius, I'm not trying to do anything dangerous here; I'm just trying to make a song play, and if there aren't any ways then could you tell me if there are any ways of making DLLMain call another function in a DLL – Aperture32 Aug 23 '19 at 16:39
  • @Aperture32: The rules for what you can do from DLLMain don't know your intentions, so they're highly unlikely to make an exception for your code. What you can and can't do from DLLMain is well documented and not changeable. And no, you cannot force an application that you don't have the source code for to call a function in your DLL; think about what a massive security breach that would be. Stop trying to force another application to play your music or sounds. – Ken White Aug 23 '19 at 16:47
  • @ape: You *can* create a thread in `DllMain`. There are no restrictions for that thread, as long as you don't synchronize with it from `DllMain`. It can call `PlaySound`, for example. – IInspectable Aug 23 '19 at 17:51