It is currently Thu Dec 05, 2019 11:07 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: How are DLLs correctly programmed?
PostPosted: Fri Apr 01, 2016 6:50 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Jun 25, 2014 5:25 pm
Posts: 375
Location: Germany
The PB help says:
Quote:
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:
Procedure Test()
  Protected NewList MyList.s()
  ...
EndProcedure
The list must be declared globally:
Code:
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:
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:
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
PureBasic-CodeArchiv-Rebirth: Git-Repository / Download -- Any help is welcome!
Manjaro Xfce x64 (Main system) :: WindowsXP/Xubuntu x86 (VirtualBox) :: PureBasic (Linux: x86/x64, Windows: x86) :: All are up to date


Top
 Profile  
Reply with quote  
 Post subject: Re: How are DLLs correctly programmed?
PostPosted: Fri Apr 01, 2016 6:57 pm 
Offline
Administrator
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 13638
Location: France
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: How are DLLs correctly programmed?
PostPosted: Fri Apr 01, 2016 8:05 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Jun 25, 2014 5:25 pm
Posts: 375
Location: Germany
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:
Quote:
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:
Quote:
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
PureBasic-CodeArchiv-Rebirth: Git-Repository / Download -- Any help is welcome!
Manjaro Xfce x64 (Main system) :: WindowsXP/Xubuntu x86 (VirtualBox) :: PureBasic (Linux: x86/x64, Windows: x86) :: All are up to date


Top
 Profile  
Reply with quote  
 Post subject: Re: How are DLLs correctly programmed?
PostPosted: Sat Apr 02, 2016 6:08 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Jun 25, 2014 5:25 pm
Posts: 375
Location: Germany
Posted:

_________________
Image
Why OpenSource should have a license
PureBasic-CodeArchiv-Rebirth: Git-Repository / Download -- Any help is welcome!
Manjaro Xfce x64 (Main system) :: WindowsXP/Xubuntu x86 (VirtualBox) :: PureBasic (Linux: x86/x64, Windows: x86) :: All are up to date


Top
 Profile  
Reply with quote  
 Post subject: Re: How are DLLs correctly programmed?
PostPosted: Sat Apr 02, 2016 6:41 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 3143
Location: Boston, MA
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


Top
 Profile  
Reply with quote  
 Post subject: Re: How are DLLs correctly programmed?
PostPosted: Sun Apr 03, 2016 4:35 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Jun 25, 2014 5:25 pm
Posts: 375
Location: Germany
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:
OpenLibrary(0, "DLLPath")

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

...

_________________
Image
Why OpenSource should have a license
PureBasic-CodeArchiv-Rebirth: Git-Repository / Download -- Any help is welcome!
Manjaro Xfce x64 (Main system) :: WindowsXP/Xubuntu x86 (VirtualBox) :: PureBasic (Linux: x86/x64, Windows: x86) :: All are up to date


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye