Ada DLLs and Elaboration (GNAT User’s Guide for Native Platforms)

#### 7.4.8.15 Ada DLLs and Elaboration ¶

The DLL that you are building contains your Ada code as well as all the routines in the Ada library that are needed by it. The first thing a user of your DLL must do is elaborate the Ada code (Elaboration Order Handling in GNAT).

To allow this, you must export an initialization routine (`Initialize_API` in the previous example), which must be invoked before using any of the DLL services. This elaboration routine must call the Ada elaboration routine `adainit` generated by the GNAT binder (Binding with Non-Ada Main Programs). See the body of `Initialize_Api` for an example. Note that the GNAT binder is automatically invoked during the DLL build process by the `gnatdll` tool (Using gnatdll).

When a DLL is loaded, Windows systematically invokes a routine called `DllMain`. It should therefore be possible to call `adainit` directly from `DllMain` without having to provide an explicit initialization routine. Unfortunately, you can’t call `adainit` from the `DllMain` if your program has library level tasks because access to the `DllMain` entry point is serialized by the system (that is, only a single thread can execute ‘through’ it at a time), which means that the GNAT run-time will deadlock waiting for a newly created task to complete its initialization.