Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » Calling al_install_audio from a DLL?

This thread is locked; no one can reply to it. rss feed Print
Calling al_install_audio from a DLL?
codetricity
Member #17,059
March 2019

My engine will init allegro inside the DLL on startup and will shut it down before it terminates. When al_install_audio is called, it will hang 100 percent of the time. Why? I worked around the issue by making sure audio init/shutdown is done from the calling process (the EXE). Just wondering what is the problem as all the other init routines work without any apparent issues.

Edgar Reynaldo
Member #8,592
May 2007
avatar

I initialise allegro sound from within a dll as well but it works for me.

codetricity
Member #17,059
March 2019

I initialise allegro sound from within a dll as well but it works for me.

Interesting. I've had this problem since I've first started using Allegro with Delphi. It's been on my list of things to investigate so I figure I would see if I can get to the bottom of it. I will keep digging.

Edgar Reynaldo
Member #8,592
May 2007
avatar

codetricity
Member #17,059
March 2019

#SelectExpand
1procedure TEngine.InitAllegro; 2begin 3 FAudioInstalled := False; 4 FMouseInstalled := False; 5 FCodecInstalled := False; 6 FKeyboardInstalled := False; 7 8 // init allegro 9 al_install_system (ALLEGRO_VERSION_INT, nil); 10 11 // init addons 12 al_init_video_addon; 13 al_init_font_addon; 14 al_init_ttf_addon; 15 al_init_primitives_addon; 16 al_init_native_dialog_addon; 17 al_init_image_addon; 18 19 // install devices 20 FKeyboardInstalled := al_install_keyboard; 21 FMouseInstalled := al_install_mouse; 22 23 // will hang here if called inside dll, but if called from the EXE, works fine. 24 FAudioInstalled := al_install_audio; //<--- hang 25 FCodecInstalled := al_init_acodec_addon; 26 27 // init audio 28 FVoice := al_create_voice(44100, ALLEGRO_AUDIO_DEPTH_INT16, ALLEGRO_CHANNEL_CONF_2); 29 FMixer := al_create_mixer(44100, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2); 30 al_set_default_mixer(FMixer); 31 al_attach_mixer_to_voice(FMixer, FVoice); 32 al_reserve_samples(ALLEGRO_MAX_CHANNELS); 33 34 // init event queues 35 FQueue := al_create_event_queue; 36 al_register_event_source(FQueue, al_get_keyboard_event_source); 37 al_register_event_source(FQueue, al_get_mouse_event_source); 38 39 InitCommonColors; 40end; 41 42constructor TEngine.Create; 43begin 44 InitAllegro 45 ... 46end; 47 48destructor TEngine.Destroy; 49begin 50 ... 51 ShutdownAllegro; 52end;

Edgar Reynaldo
Member #8,592
May 2007
avatar

This is stupid but try initializing audio before installing the keyboard and mouse.

codetricity
Member #17,059
March 2019

This is stupid but try initializing audio before installing the keyboard and mouse.

no, a good idea actually but sadly the same result

Edgar Reynaldo
Member #8,592
May 2007
avatar

Can you debug this? Step into al_install_audio, and see where it hangs.

codetricity
Member #17,059
March 2019

Can you debug this? Step into al_install_audio, and see where it hangs.

Wish I could. Rem I'm using Delphi (Object Pascal). I can step up to the function call is all.

Edgar Reynaldo
Member #8,592
May 2007
avatar

How did you build / link to allegro?

What libraries did you use and where did you get them?

codetricity
Member #17,059
March 2019

You have to convert .h headers to Delphi (object pascal), and just ref as external routines.

void my_cfunction(void);

procedure my_cfunction; cdecl; external 'allegro5.dll';

compile this and you can directly call my_cfunction in Pascal.

The binaries from https://github.com/liballeg/allegro5/releases, 32bit monolithic version

Edgar Reynaldo
Member #8,592
May 2007
avatar

Does allegro.log get created?

You can enable it in release mode by doing...

al_set_config_value(al_get_system_config(), "trace", "level", "debug")

codetricity
Member #17,059
March 2019

ok, good call. Log attached. Thx.

stops at this line:

audio-dsound I dsound.cpp:271 _dsound_open [ 2.27177] Starting DirectSound...

#SelectExpand
1/* FIXME: Use default device until we have device enumeration */ 2 hr = DirectSoundCreate8(NULL, &device, NULL); 3 if (FAILED(hr)) { 4 ALLEGRO_ERROR("DirectSoundCreate8 failed: %s\n", ds_get_error(hr)); 5 return 1; 6 }

Edgar Reynaldo
Member #8,592
May 2007
avatar

codetricity
Member #17,059
March 2019

Ok, useful info there. Thx for that.

In Delphi, it has the concept of a unit and can have an init/final section:

#SelectExpand
1unit uEngine; 2 3interface 4 5implementation 6 7initialization 8 Engine_Init; 9 10finalization 11 Engine_Shutdown; 12 13end.

There is where I usually init and shut down the engine when the DLL loads into the calling process. What I will do to see if it makes a diff is call from DLL_PROCESS_ATTACH/DLL_PROCESS_DETACH.

Edgar Reynaldo
Member #8,592
May 2007
avatar

codetricity
Member #17,059
March 2019

nope, still hangs and there are other issues with the dllmain approach. I discovered that delphi unit init/final methods is safer, the RTL will call in the proper order and they only run once so Engine_Init/Shutdown only runs once. Will keep digging. Thanks for your help bro.

Hmm... what is the difference between calling DirectSoundCreate8(NULL, &device, NULL) inside DLL vs outside I wonder?

Edit 1:
I found this - http://forums.codeguru.com/showthread.php?407844-DirectSoundCreate8-wont-initialize-in-DLL

He suggests calling from main EXE which is the only way I got it to work.

Edgar Reynaldo
Member #8,592
May 2007
avatar

Right, I don't Engine::Init in dllmain but in EagleSystem::Initialize which the user calls from main. At least that's what I see being described here.

Go to: