Page 1 of 1

How are DLLs correctly programmed?

Posted: Fri Apr 01, 2016 6:50 pm
by Sicro
The PB help says:
Notes about creating DLL's:

- The declaration of arrays, lists or map with Dim, NewList or NewMap must always be done inside the procedure AttachProcess.
- Don't write program code outside procedures. The only exception is the declaration of variables or structures.
- DirectX initialization routines must not be written in the AttachProcess procedure.
If I follow these rules, that isn't allowed:

Code: Select all

Procedure Test()
  Protected NewList MyList.s()
  ...
EndProcedure
The list must be declared globally:

Code: Select all

Procedure AttachProcess(Instance)
  Global NewList MyList.s()
EndProcedure

Procedure Test()
  ... MyList() ...
EndProcedure
On Linux, the procedure "AttachProcess" is not performed automatically when the library opens.
This way is incorrect, because code exists outside procedures:

Code: Select all

Procedure AttachProcess(Instance)
  Global NewList MyList.s()
EndProcedure

CompilerIf #PB_Compiler_OS <> #PB_OS_Windows
  AttachProcess(0)
CompilerEndIf

Procedure Test()
  ... MyList() ...
EndProcedure
It only remains this way:

Code: Select all

OpenLibrary(0, "DLLPath")

CompilerIf #PB_Compiler_OS <> #PB_OS_Windows
  CallFunction(0, "AttachProcess", 0)
CompilerEndIf

...
Is that all right? How is it correct?

Re: How are DLLs correctly programmed?

Posted: Fri Apr 01, 2016 6:57 pm
by Fred
AttachProcess() should be called on linux as well, if not it's a bug. You can also declare list in procedure if needed, just not in the main code.

Re: How are DLLs correctly programmed?

Posted: Fri Apr 01, 2016 8:05 pm
by Sicro
Fred wrote:AttachProcess() should be called on linux as well, if not it's a bug.
Ok, in this case, the text should be adapted:
For advanced programmers: there is 4 special procedures which are called automatically by Windows when one of the following events happen:

- DLL is attached to a new process
- DLL is detached from a process
- DLL is attached to a new thread
- DLL is detached from a thread

To handle that, it's possible to declare 4 special procedures called: AttachProcess(Instance), DetachProcess(Instance), AttachThread(Instance) and DetachThread(Instance). This means these 4 procedures names are reserved and can't be used by the programmer for other purposes.
In this case, it is a bug.
Fred wrote:You can also declare list in procedure if needed, just not in the main code.
Ok, in this case, the text should be adapted, too:
Notes about creating DLL's:

- The declaration of arrays, lists or map with Dim, NewList or NewMap must always be done inside the procedure AttachProcess.
Thank you for your answer. :)

Re: How are DLLs correctly programmed?

Posted: Sat Apr 02, 2016 6:08 pm
by Sicro

Re: How are DLLs correctly programmed?

Posted: Sat Apr 02, 2016 6:41 pm
by skywalk
You can force the app calling your dll to call an init_mydll() or attachprocess() function before using any of the underlying functions. And call close_mydll() or detachprocess() upon exit.

Re: How are DLLs correctly programmed?

Posted: Sun Apr 03, 2016 4:35 pm
by Sicro
skywalk wrote:You can force the app calling your dll to call an init_mydll() or attachprocess() function before using any of the underlying functions. And call close_mydll() or detachprocess() upon exit.
Yes, look at my first post:
Sicro wrote:It only remains this way:

Code: Select all

OpenLibrary(0, "DLLPath")

CompilerIf #PB_Compiler_OS <> #PB_OS_Windows
  CallFunction(0, "AttachProcess", 0)
CompilerEndIf

...