Page 1 of 1

Window in dll

Posted: Wed Dec 22, 2021 2:44 pm
by User_Russian
Small example of creating window in dll.

application

Code: Select all

If OpenLibrary(0, "WinDLL.dll")=0
  MessageRequester("", "Not DLL")
  End
EndIf

OpenWindow(0, 0, 0, 300, 300, "Main Window", #PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
ButtonGadget(0, 10, 10, 80, 24, "Open window")

Repeat
  Event = WaitWindowEvent()
  If Event = #PB_Event_Gadget And EventGadget()=0
    CallFunction(0, "OpenWin")
  EndIf
Until Event = #PB_Event_CloseWindow

CloseLibrary(0)
dll

Code: Select all

Procedure CloseEvent()
  id = EventWindow()
  UnbindEvent(#PB_Event_CloseWindow, @CloseEvent(), id)
  CloseWindow(id)
EndProcedure

ProcedureDLL OpenWin()
  id=OpenWindow(#PB_Any, 0, 0, 200, 200, "DLL Window", #PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
  BindEvent(#PB_Event_CloseWindow, @CloseEvent(), id)
EndProcedure

ProcedureDLL DetachProcess(Instance)
  CloseWindow(#PB_All)
EndProcedure

Re: Window in dll

Posted: Wed Dec 22, 2021 3:59 pm
by jacdelad
Erm, please ignore my ignorance, but isn't that very obvious? (I have a good excuse: I have a surgery tomorrow and I'm on many, many painkillers).

Re: Window in dll

Posted: Wed Dec 22, 2021 5:30 pm
by mk-soft
The GUI (Windows, Gadgets) in DLLs does not work with the GUI of the main programme. The DLL has its own object management and therefore no objects can be exchanged with the main programme (event management, windows, gadgets, images, fonts, etc).

Re: Window in dll

Posted: Wed Dec 22, 2021 5:43 pm
by skywalk
Curious, what is your application for a dll that spawns its own window(s)?
Is this to modularize an About window or a Help screen?

Re: Window in dll

Posted: Wed Dec 22, 2021 6:40 pm
by User_Russian
skywalk wrote: Wed Dec 22, 2021 5:43 pmCurious, what is your application for a dll that spawns its own window(s)?
For example, plugins that have a GUI for configuration.

Re: Window in dll

Posted: Wed Dec 22, 2021 6:42 pm
by HeX0R
I'm using it more or less exactly like that since years.
My app uses DLLs as plugins, and they need to have their own windows sometimes.

But mine is windows only, maybe that would not work for other OS.

Re: Window in dll

Posted: Wed Dec 22, 2021 7:47 pm
by Caronte3D
In my last project I need to add a window to a third party application so I used a dll but opened the window in a separate thread. I did not know that a window could be kept open as User_Russian has shown :?

Re: Window in dll

Posted: Wed Dec 22, 2021 8:22 pm
by mk-soft
As I described above. Create windows in DLL, have their own event management and object manangment.

Re: Window in dll

Posted: Thu Dec 23, 2021 12:14 am
by OgreVorbis
Interesting. Can I have a full on event loop in a DLL like I would in a normal program? Or is this bindevent the only way? (I'm not sure I would actually need it if I have bindevent though.) I suspect there must be a way because of rundll32 that runs DLLs like an EXE.

Also, can other programs access the same instance of one DLL or do they each have to load a separate copy into their memory space?

Re: Window in dll

Posted: Fri Dec 24, 2021 2:38 am
by OgreVorbis
OgreVorbis wrote: Thu Dec 23, 2021 12:14 am Interesting. Can I have a full on event loop in a DLL like I would in a normal program? Or is this bindevent the only way? (I'm not sure I would actually need it if I have bindevent though.) I suspect there must be a way because of rundll32 that runs DLLs like an EXE.

Also, can other programs access the same instance of one DLL or do they each have to load a separate copy into their memory space?
Looks like the answer is YES. It's really cool cause it seems to work just like my normal EXE program. I even tested a window callback and that worked too. I made a window and used the callback to process WM_COPYDATA and display on a textgadget. I then tested by launching the DLL with "rundll32 DLLTest.dll,OpenWin" - OpenWin being the name of my function; it makes the callback, and runs an event loop.
I'm surprised. I love PB so much cause I looked at examples of making a DLL (especially one that works with rundll32) in C and the code is ugly and not trivial. There's some special stuff needed apparently to make it work with rundll32, but PB just works. None of the languages I worked with in the past made REAL DLLs.

I think the second question is a no though. (At least not without hackery.)

Re: Window in dll

Posted: Thu Dec 30, 2021 7:21 pm
by Kwai chang caine
Thanks for sharing this nice example, works perfectly here 8)