How are DLLs correctly programmed?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

How are DLLs correctly programmed?

Post 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?
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
Fred
Administrator
Administrator
Posts: 16619
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: How are DLLs correctly programmed?

Post 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.
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: How are DLLs correctly programmed?

Post 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. :)
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: How are DLLs correctly programmed?

Post by Sicro »

Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: How are DLLs correctly programmed?

Post 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.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Sicro
Enthusiast
Enthusiast
Posts: 538
Joined: Wed Jun 25, 2014 5:25 pm
Location: Germany
Contact:

Re: How are DLLs correctly programmed?

Post 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

...
Image
Why OpenSource should have a license :: PB-CodeArchiv-Rebirth :: Pleasant-Dark (syntax color scheme) :: RegEx-Engine (compiles RegExes to NFA/DFA)
Manjaro Xfce x64 (Main system) :: Windows 10 Home (VirtualBox) :: Newest PureBasic version
Post Reply