PureBasic Forum
https://www.purebasic.fr/english/

How are DLLs correctly programmed?
https://www.purebasic.fr/english/viewtopic.php?f=13&t=65377
Page 1 of 1

Author:  Sicro [ Fri Apr 01, 2016 6:50 pm ]
Post subject:  How are DLLs correctly programmed?

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?

Author:  Fred [ Fri Apr 01, 2016 6:57 pm ]
Post subject:  Re: How are DLLs correctly programmed?

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.

Author:  Sicro [ Fri Apr 01, 2016 8:05 pm ]
Post subject:  Re: How are DLLs correctly programmed?

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. :)

Author:  Sicro [ Sat Apr 02, 2016 6:08 pm ]
Post subject:  Re: How are DLLs correctly programmed?

Posted:

Author:  skywalk [ Sat Apr 02, 2016 6:41 pm ]
Post subject:  Re: How are DLLs correctly programmed?

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.

Author:  Sicro [ Sun Apr 03, 2016 4:35 pm ]
Post subject:  Re: How are DLLs correctly programmed?

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

...

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/