Window in dll

Share your advanced PureBasic knowledge/code with the community.
User_Russian
Addict
Addict
Posts: 1547
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Window in dll

Post 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
User avatar
jacdelad
Addict
Addict
Posts: 2010
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Window in dll

Post 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).
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
mk-soft
Always Here
Always Here
Posts: 6248
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Window in dll

Post 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).
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
User avatar
skywalk
Addict
Addict
Posts: 4218
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Window in dll

Post 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?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User_Russian
Addict
Addict
Posts: 1547
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Window in dll

Post 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.
User avatar
HeX0R
Addict
Addict
Posts: 1204
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Window in dll

Post 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.
User avatar
Caronte3D
Addict
Addict
Posts: 1362
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Window in dll

Post 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 :?
User avatar
mk-soft
Always Here
Always Here
Posts: 6248
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Window in dll

Post by mk-soft »

As I described above. Create windows in DLL, have their own event management and object manangment.
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
OgreVorbis
User
User
Posts: 79
Joined: Thu Jan 16, 2020 10:47 pm

Re: Window in dll

Post 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?
My blog/software site: http://dosaidsoft.com/
OgreVorbis
User
User
Posts: 79
Joined: Thu Jan 16, 2020 10:47 pm

Re: Window in dll

Post 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.)
My blog/software site: http://dosaidsoft.com/
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Window in dll

Post by Kwai chang caine »

Thanks for sharing this nice example, works perfectly here 8)
ImageThe happiness is a road...
Not a destination
Post Reply