and restore them when exiting.
With a function "InitLib(...) the objects are passed from the main program to the library.
The application and library must be compile with the same purebasic version.
But it seems to work without guarantee (Window, Linux and macOS)
Update v1.03
- Linux has not AttachProcess and DetachProcess
- Change InitLib
- Added ReleaseLib
- Optimize code
Update v1.04
- Optimize force compiler use fonts
TestLib.pb
Code: Select all
;-TOP DLL
; Comment : Library
; Author : mk-soft
; Version : v1.04
EnableExplicit
; ----
If 0 : LoadFont(0, "", 9) : EndIf
Import ""
PB_Window_Objects.i
PB_Gadget_Objects.i
PB_Image_Objects.i
PB_Font_Objects.i
EndImport
Global IsInit
Global save_window_objects
Global save_gadget_objects
Global save_image_objects
Global save_font_objects
; ----
ProcedureDLL InitLib(window_objects, gadget_objects, image_objects, font_objects)
If Not IsInit
IsInit = #True
; Save DLL Objects
save_window_objects = PB_Window_Objects
save_gadget_objects = PB_Gadget_Objects
save_image_objects = PB_Image_Objects
save_font_objects = PB_Font_Objects
; Set Main Objects
PB_Window_Objects = window_objects
PB_Gadget_Objects = gadget_objects
PB_Image_Objects = image_objects
PB_Font_Objects = font_objects
EndIf
ProcedureReturn IsInit
EndProcedure
ProcedureDLL ReleaseLib()
If IsInit
IsInit = #False
; Restore DLL Objects
PB_Window_Objects = save_window_objects
PB_Gadget_Objects = save_gadget_objects
PB_Image_Objects = save_image_objects
PB_Font_Objects = save_font_objects
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
EndProcedure
; ++++
ProcedureDLL GadgetResize(Gadget, X.i, Y.i, W.i, H.i)
ResizeGadget(Gadget, X.i, Y.i, W.i, H.i)
ProcedureReturn 1
EndProcedure
; ----
Code: Select all
;-TOP Main
; Comment : Application
; Author : mk-soft
; Version : v1.04
;- Purebasic SDK Objects
If 0 : LoadFont(0, "", 9) : EndIf
Import ""
PB_Window_Objects.i
PB_Gadget_Objects.i
PB_Image_Objects.i
PB_Font_Objects.i
EndImport
;- LibFunctions
Prototype GadgetResize(Gadget, X.i, Y.i, W.i, H.i)
; ----
Procedure OpenTestLibrary(ID)
Protected r1, LibName.s
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
LibName = "TestLib.dll"
CompilerCase #PB_OS_Linux
LibName = "TestLib.so"
CompilerCase #PB_OS_MacOS
LibName = "TestLib.dylib"
CompilerEndSelect
If OpenLibrary(ID, LibName)
; Function
Global GadgetResize.GadgetResize = GetFunction(ID, "GadgetResize")
; Init Library
r1 = CallFunction(ID, "InitLib", PB_Window_Objects, PB_Gadget_Objects, PB_Image_Objects, PB_Font_Objects)
If Not r1
CloseLibrary(ID)
EndIf
ProcedureReturn r1
Else
ProcedureReturn #False
EndIf
EndProcedure
Procedure CloseTestLibrary(ID)
Protected r1
If IsLibrary(ID)
r1 = CallFunction(ID, "ReleaseLib")
If r1
CloseLibrary(ID)
EndIf
EndIf
ProcedureReturn r1
EndProcedure
; ****
;-Window
Enumeration
#Window
EndEnumeration
If OpenTestLibrary(0)
Debug "InitLib Ok"
Else
Debug "InitLib faild"
End
EndIf
If OpenWindow(#Window, 40, 40, 220, 100, "Window", #PB_Window_SystemMenu)
ButtonGadget(1, 10, 60, 20, 30, "Exit")
If GadgetResize(1, 10, 40, 200, 50)
MessageRequester("Result", "GadgetResize Ok")
EndIf
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Break ; Exit mainloop
Case #PB_Event_Gadget
Select EventGadget()
Case 1
CloseWindow(#Window)
Break ; Exit mainloop
EndSelect
EndSelect
ForEver
If CloseTestLibrary(0)
Debug "ReleaseLib Ok"
EndIf
EndIf