Page 1 of 1
DLL question about create image
Posted: Sun Mar 15, 2015 10:59 am
by marc_256
Hi,
I want to create a .DLL to include all my cad/cam controls.
I never used .dll files, so ??
Also all my image stuff,
example:
for create an image with backcolor
data to send to the DLL
1) image number
2) image width
3) image height
4) image backcolor
ms_create_image(1, 800, 600, $00000000)
inside the DLL i will use the procedure ms_create_image(number.w, width.w, height.w, backcolor.l)
Q1)
is it possible to create an image inside DLL, and how to receive this image data back inside PB main program ?
thanks,
marc
Re: DLL question about create image
Posted: Sun Mar 15, 2015 11:48 am
by Joris
One way is to preserve a memory-block and pass the adress to the dll in which you then do the drawings and recieve that after the drawings are finished.
Re: DLL question about create image
Posted: Mon Mar 16, 2015 7:59 am
by netmaestro
DLL code:
Code: Select all
ProcedureDLL.i ReturnImage(w, h, depth, backcolor)
tmp=CreateImage(#PB_Any, w, h, depth, backcolor)
UsePNGImageEncoder()
Global *this
*this = EncodeImage(tmp,#PB_ImagePlugin_PNG)
ProcedureReturn *this
EndProcedure
Calling Code:
Code: Select all
Prototype ReturnImage(w,h,d,bkg)
OpenLibrary(0, "test.dll")
Global ReturnImage.ReturnImage = GetFunction(0, "ReturnImage")
*this = ReturnImage(64,64,24,#Red)
UsePNGImageDecoder()
CatchImage(0, *this)
OpenWindow(0,0,0,ImageWidth(0),ImageHeight(0),"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
ImageGadget(0,0,0,0,0,ImageID(0))
Repeat:Until WaitWindowEvent() = #PB_Event_CloseWindow
Guaranteed to possibly work, maybe if you hold your mouth juuuust.. right.
Nah, it works.
Re: DLL question about create image
Posted: Mon Mar 16, 2015 12:20 pm
by luis
Nice idea netmaestro, the encoding in the DLL to be able to catch it from the client and get this way a valid PB image bypassing the DLL barrier.
I didn't think of that, can be useful.
Uhm... on closer inspection while the idea is valid I see a couple of problems.
The first one is easy to fix ... you should just free the image in the DLL after the encoding.
The other one is the buffer allocated in the DLL to encode the image is never released, so one should make available a freememory() alias callable from the client.
Code: Select all
ProcedureDLL.i ReturnImage (w, h, depth, backcolor)
Protected *this
Protected tmp = CreateImage(#PB_Any, w, h, depth, backcolor)
UsePNGImageEncoder()
*this = EncodeImage(tmp, #PB_ImagePlugin_PNG)
FreeImage(tmp)
ProcedureReturn *this
EndProcedure
ProcedureDLL FreeMemoryDLL (*add)
FreeMemory(*add)
EndProcedure
Code: Select all
Prototype ReturnImage(w,h,d,bkg)
Prototype FreeMemoryDLL(*add)
UsePNGImageDecoder()
OpenLibrary(0, "dll.dll")
Global ReturnImage.ReturnImage = GetFunction(0, "ReturnImage")
Global FreeMemoryDLL.FreeMemoryDLL = GetFunction(0, "FreeMemoryDLL")
*this = ReturnImage(256,256,24,#Red)
CatchImage(0, *this)
FreeMemoryDLL(*this)
OpenWindow(0,0,0,ImageWidth(0),ImageHeight(0),"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
ImageGadget(0,0,0,0,0,ImageID(0))
Repeat:Until WaitWindowEvent() = #PB_Event_CloseWindow
Re: DLL question about create image
Posted: Mon Mar 16, 2015 2:32 pm
by luis
Even if in this case I prefer netmaestro's suggestion, this is another possible approach which could be useful in other cases.
I'll use the image example anyway to make it more easily comparable.
In the above the image is created in the DLL and then re-created from the encoded data format in the client, in the following the image is created directly in the client instead, by passing the address of an helper client function to the DLL.
Code: Select all
Prototype.i CreateImageClient(n, w, h, depth, back)
Global CreateImageClient.CreateImageClient
Structure DLL_INIT
CreateImageClient.i
EndStructure
ProcedureDLL.i ReturnImage (w, h, depth, backcolor)
Protected r, img = CreateImageClient(#PB_Any, w, h, depth, backcolor)
StartDrawing(ImageOutput(img))
If w > h
r = h /2
Else
r = w /2
EndIf
Circle(w/2, h/2, r, #White)
StopDrawing()
ProcedureReturn img
EndProcedure
ProcedureDLL InitDLL (*init.DLL_INIT)
CreateImageClient = *init\CreateImageClient
EndProcedure
Code: Select all
Structure DLL_INIT ; this way you can communicate further stuff to the dll if required
CreateImageClient.i
EndStructure : Global DI.DLL_INIT
Prototype ReturnImage(w,h,d,bkg)
Prototype InitDLL (*init.DLL_INIT)
Procedure.i CreateImageClient (n, w, h, depth, back)
ProcedureReturn CreateImage (n, w, h, depth, back)
EndProcedure
OpenLibrary(0, "dll.dll")
Global ReturnImage.ReturnImage = GetFunction(0, "ReturnImage")
Global InitDLL.InitDLL = GetFunction(0, "InitDLL")
DI\CreateImageClient = @CreateImageClient()
InitDLL(@DI)
Define img = ReturnImage(256,256,24,#Red)
OpenWindow(0,0,0,ImageWidth(img),ImageHeight(img),"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
ImageGadget(0,0,0,0,0,ImageID(img))
Repeat:Until WaitWindowEvent() = #PB_Event_CloseWindow
Re: DLL question about create image
Posted: Tue Mar 17, 2015 7:20 am
by marc_256
hi netmaestro and luis,
I work nights now, so, just home...
thanks for the wonderful programs.
After some hours of sleep I will test them.
THANKS,
marc