Module Initialization and Dll`s

Everything else that doesn't fall into one of the other PB categories.
User avatar
mk-soft
Always Here
Always Here
Posts: 5393
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Module Initialization and Dll`s

Post by mk-soft »

It is times in the German forum again discussed whether this is allowed.
Do not create codes when creating Dll's outside of procedures (AttachProcess).

When programming modules, I use a Procedure InitModule ().
This is called directly in the module.
Now all say definitely, call the function in AttachProcess.

This is not always possible, however, if these procedures are created via macros.

After intensive evaluation, the procedure InitModule () is called as the ProcedureDLL AttachProcess. Only without the parameter 'Instance'

The call is executed from the internal procedure PB_DllInit () after the initialization.
Because the call to the procedure PB_DllInit is followed by the call to the Procedure AttachProcess (), if it exists.

This procedure is identical to all PB versions supporting the modules.

What does the official team say about the procedure?

Code:

Code: Select all

DeclareModule MyModule
  ;TODO
  Global name.s
EndDeclareModule

Module MyModule
  
  Procedure InitModule()
    ;TODO
    name.s = "Init My Module!"
  EndProcedure
  
  InitModule()
  
EndModule

Global name.s
ProcedureDLL AttachProcess(Instance)
  ;TODO
  name.s = "Init My DLL"
EndProcedure
ASM:

Code: Select all

; 
; PureBasic 5.60 (Windows - x64) generated code
; 
; (c) 2016 Fantaisie Software
; 
; The header must remain intact for Re-Assembly
; 
; :DLL
; :System
; kernel32.lib
; :Import
; 
format MS64 COFF
; 
; 
extrn GetModuleHandleW
extrn HeapCreate
extrn HeapDestroy
extrn memset
extrn SYS_FastAllocateStringFree4
extrn PB_StringBase
extrn SYS_InitString
extrn SYS_FreeStrings
; 
extrn PB_StringBasePosition
public _PB_Instance
public PB_ExecutableType
public PB_OpenGLSubsystem
public _PB_MemoryBase
public PB_Instance
public PB_MemoryBase
public PB_EndFunctions
public _DLLEntryPoint@12
public _Procedure2

macro pb_public symbol
{
  public  _#symbol
  public symbol
_#symbol:
symbol:
}

macro    pb_align value { rb (value-1) - ($-_PB_DataSection + value-1) mod value }
macro pb_bssalign value { rb (value-1) - ($-_PB_BSSSection  + value-1) mod value }

; 
section '.code' code readable executable align 4096
; 
; 
_DLLEntryPoint@12:
  SUB    rsp,40
  CMP    rdx,1
  JNE   .SkipProcessAttach
  MOV    [_PB_Instance],rcx
  CALL   PB_DllInit
  MOV    rcx,[_PB_Instance]
  CALL  _Procedure2
  JMP   .End
.SkipProcessAttach:
  CMP    rdx,2
  JNE   .SkipThreadAttach
  JMP   .End
.SkipThreadAttach:
  CMP    rdx,0
  JNE   .SkipProcessDetach
  CALL  _PB_EOP
  JMP   .End
.SkipProcessDetach:
  CMP    rdx,3
  JNE   .SkipThreadDetach
.SkipThreadDetach:
.End:
  MOV    rax,1
  ADD    rsp,40
  RET
; 
PB_DllInit:
  SUB    rsp,40
  XOR    r8,r8
  MOV    rdx,4096
  XOR    rcx,rcx
  CALL   HeapCreate
  MOV    [PB_MemoryBase],rax
  CALL   SYS_InitString

; DeclareModule MyModule
; 
; Global name.s
; EndDeclareModule
; 
; Module MyModule
; 
; 
; InitModule()
  CALL  _Procedure0
; 
; EndModule
; 
; Global name.s
; 
  ADD    rsp,40
  RET
_PB_EOP:
  SUB    rsp,40
  CALL   PB_EndFunctions
  CALL   SYS_FreeStrings
  MOV    rcx,[PB_MemoryBase]
  CALL   HeapDestroy
  ADD    rsp,40
  RET
PB_EndFunctions:
  SUB    rsp,40
  ADD    rsp,40
  RET
; 
; ProcedureDLL AttachProcess(Instance)
_Procedure2:
  MOV    qword [rsp+8],rcx
  PS2=48
  SUB    rsp,40
; 
; name.s = "Init My DLL"
  LEA    rdx,[_S2]
  LEA    rcx,[v_name]
  CALL   SYS_FastAllocateStringFree4
; EndProcedure
_EndProcedureZero3:
  XOR    rax,rax
_EndProcedure3:
  ADD    rsp,40
  RET
; Procedure InitModule()
_Procedure0:
  PS0=48
  SUB    rsp,40
; 
; name.s = "Init My Module!"
  LEA    rdx,[_S1]
  LEA    rcx,[mymodule.v_name]
  CALL   SYS_FastAllocateStringFree4
; EndProcedure
_EndProcedureZero1:
  XOR    rax,rax
_EndProcedure1:
  ADD    rsp,40
  RET
; 
section '.data' data readable writeable
; 
_PB_DataSection:
PB_OpenGLSubsystem: db 0
pb_public PB_DEBUGGER_LineNumber
  dd     -1
pb_public PB_DEBUGGER_IncludedFiles
  dd     0
pb_public PB_DEBUGGER_FileName
  db     0
pb_public PB_Compiler_Unicode
  dd     1
pb_public PB_Compiler_Thread
  dd     0
pb_public PB_Compiler_Purifier
  dd     0
pb_public PB_Compiler_Debugger
  dd     0
PB_ExecutableType: dd 0
pb_align 8
public _SYS_StaticStringStart
_SYS_StaticStringStart:
_S2: dw 73,110,105,116,32,77,121,32,68,76,76,0
_S1: dw 73,110,105,116,32,77,121,32,77,111,100,117,108,101,33,0
pb_public PB_NullString
  dw     0
public _SYS_StaticStringEnd
_SYS_StaticStringEnd:
pb_align 8
pb_align 8
pb_align 8
s_s:
  dq     0
  dq     -1
pb_align 8
; 
section '.bss' readable writeable
_PB_BSSSection:
pb_bssalign 8
; 
I_BSSStart:
_PB_MemoryBase:
PB_MemoryBase: rq 1
_PB_Instance:
PB_Instance: rq 1
PB_ExitCode: rq 1
; 
pb_bssalign 8
PB_DataPointer rq 1
v_name rq 1
mymodule.v_name rq 1
pb_bssalign 8
pb_bssalign 8
pb_bssalign 8
pb_bssalign 8
I_BSSEnd:
section '.data' data readable writeable
SYS_EndDataSection:
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive