Recent

Author Topic: Popup menu problem in dll vs. WidgetSet.AppHandle  (Read 5427 times)

balijani

  • New member
  • *
  • Posts: 8
Popup menu problem in dll vs. WidgetSet.AppHandle
« on: April 04, 2014, 04:17:04 pm »
Hi!

My good old project VCLua, has the following problem with Lazarus 1.x versions.

The Popup function isn't working in menus. I found some useful posts in this forum, and if I set the WidgetSet.Apphandle just before the popup method called, then the popup menu starts work.
But! If I close the form, the application crashes.

With the earlier Lazarus versions (0.9.x) the compiled application works well.

Help please!

howardpc

  • Hero Member
  • *****
  • Posts: 3176
Re: Popup menu problem in dll vs. WidgetSet.AppHandle
« Reply #1 on: April 04, 2014, 07:20:47 pm »
Reliable crystal balls are hard to come by.
What is your OS/widgetset?
Where can we see the problematic code?

balijani

  • New member
  • *
  • Posts: 8
Re: Popup menu problem in dll vs. WidgetSet.AppHandle
« Reply #2 on: April 04, 2014, 07:50:34 pm »
Sorry!

win7.64/win32

Hard to detect the problematic code, the TrackPopupMenuEx called, but with AppHandle=0
I think this is the main problem for me.

thanks

Cyrax

  • Hero Member
  • *****
  • Posts: 758
Re: Popup menu problem in dll vs. WidgetSet.AppHandle
« Reply #3 on: April 05, 2014, 02:28:29 am »
Check this patch out:
Code: [Select]
diff --git a/lcl/interfaces/win32/win32object.inc b/lcl/interfaces/win32/win32object.inc
index 7a0aeb1..83f20dc 100644
--- a/lcl/interfaces/win32/win32object.inc
+++ b/lcl/interfaces/win32/win32object.inc
@@ -146,10 +146,11 @@ begin
   end;
 
 
+  // Create parent of all windows, 'button on taskbar'
+  CreateAppHandle;
+
   if not IsLibrary then
   begin
-    // Create parent of all windows, 'button on taskbar'
-    CreateAppHandle;
     // set nice main icon
     AIcon := Windows.LoadIcon(MainInstance, 'MAINICON');
     AppSetIcon(AIcon, AIcon);

Open LCL package and find file win32object.inc in it. Open that file and navigate to line 151. Move CreateAppHandle outside of if-clause.

Recompile your project and test if popupmenu works correctly.

Are you by change creating forms inside your DLL? Which version of Lazarus you are using? Please give full version info.

balijani

  • New member
  • *
  • Posts: 8
Re: Popup menu problem in dll vs. WidgetSet.AppHandle
« Reply #4 on: April 05, 2014, 10:25:02 am »
This patch works! Thank you very much!

I'm using the latest stable Lazarus version 1.2.0.
All lcl components created inside the dll, driven by a lua scripts.
Here is the lua sample:
Quote
package.cpath=package.cpath..";lib/?.dll;lib/?.so"

require "vcl"

form = VCL.Form()
form._ = {name="TestForm", onMouseDown="dopopup", onClose="onClose"}
popupMenu = VCL.PopupMenu(form)
popupMenu:LoadFromTable({
   {caption="Close me", onclick="File.Close"},   
   {caption="Save me", onclick="File.Save"},
   {caption="Save me as...", onclick="File.SaveAs"},
})

function dopopup(s,b,sh,x,y)   
   if b=="mbRight" then
      local x,y = VCL.GetCursorPos()
      popupMenu:Popup(x,y)
   end
end

function onClose( Sender, Action)
  Action = "caFree"
end

form:ShowModal()


Cyrax

  • Hero Member
  • *****
  • Posts: 758
Re: Popup menu problem in dll vs. WidgetSet.AppHandle
« Reply #5 on: April 05, 2014, 03:09:26 pm »
OK. You need to export a procedure from your DLL which calls Application.ProcessMessages and call this exported procedure periodically inside your main app thread so LCL in DLL can free  forms, process events from pipes and execute threads.

Here is another patch which should prevent application from crashing when freeing forms:
Code: [Select]
diff --git a/lcl/include/customform.inc b/lcl/include/customform.inc
index a842eb2..29b53a8 100644
--- a/lcl/include/customform.inc
+++ b/lcl/include/customform.inc
@@ -1349,6 +1349,8 @@ var
   MenuItem: TMenuItem;
 begin
   //debugln(['TCustomForm.WndProc ',dbgsname(Self)]);
+  if isLibrary and (csDestroying in ComponentState) Then
+    Exit;
   with TheMessage do
   case Msg of
     LM_SETFOCUS:

balijani

  • New member
  • *
  • Posts: 8
Re: Popup menu problem in dll vs. WidgetSet.AppHandle
« Reply #6 on: April 05, 2014, 03:42:24 pm »
Thanks.
this dll called from non LCL based applications, i think in this way a should not use this patch.
Maybe the previous patch is enough for me, now my app works without any crash...