custom DLL handling

 
hello,
i have a special problem with custom dlls when used within expert advisor.
my dll provides a tray window and a logifle. it is used as brokerinterface, so it has to stay loaded until
the user explicitly removes the expert advisor from chat.

the tray window gets created on DLL_PROCESS_ATTACH and of course should be destroyed on DLL_PROCESS_DETACH.
unfortunately right after the TrayWindow is visible first time, it gets immediately removed. this is due to the DLL_THREAD_DETACH
which is invoked by MetaTrader4 evary time function

start()

is called within mq4.

first i must wonder about this way to handle custom dlls by mt4. why do metatrader detaches the dll from calling thread ? is this thread running just during the time needed to execute functions start ?
i don't believe so because global variables can be used within mq4.

ok, beside this, i don't understand why the tray window gets destroyed. i do not destroy it if the dll gets detached from thread. i do this in process detach. and right here is the next
suspicious behaviour: if destroying the tray window in on process detach (exit instance), it seems that m4 terminates for unkown reason the dll unload.

the next problem is that it seems not be possible to create files from within the dll. why not ? for some security reasons ? i


in my view it should work like that:
- the first time an exported function gets called, mt4 loads the library.
- the library gets unloaded if the user detaches expert advisor from chart.
- allow writing files and creating windows without any restrictions. warn the user about the risk if using modules (dll) doing this.

thank you for any help.
 
DLL loaded once before first using by the LoadLibrary (WinAPI) function.
DLL unloaded by the FreeLibrary function only when expert unloaded.
 
stringo:
DLL loaded once before first using by the LoadLibrary (WinAPI) function.
DLL unloaded by the FreeLibrary function only when expert unloaded.
thanky you stringo,
i know about this and solved the problem by moving tray the window to a separate thread.

as i said, MetaTrader4 seems to detach DLL from calling thread after each call of start() in EA context.
i know this by logging DLLMain which is called on each Tick with param=DLL_THREAD_DETACH.

this (probably mt4 special) behaviour caused my Tray Window created on DLL startup to disappaer.
this kind of dll handling differs slightly from that e.g. in TradeStation.



writing files: this was my fault. i passed \\thislogfile_name which was interpreted as tab.


conclusion: all is working fine with nice MetaTrader
Reason: