Recent

Author Topic: SOLVED: Notepad++ plugin: conversion questions from Delphi  (Read 27331 times)

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
SOLVED: Notepad++ plugin: conversion questions from Delphi
« on: August 18, 2012, 01:12:16 am »
Just downloaded the Delphi plugin template for Notepad++
http://sourceforge.net/projects/npp-plugins/files/DelphiPluginTemplate/DelphiPluginTemplate%201.0/

I'm trying to get this working first on the ANSI version of Notepad++, then migrate to the Unicode version.
If that works, I'll post the conversion for the Notepad++ people to pick up as the LazarusPluginTemplate ;)
... and finally add a function that spits out the current date/time (e.g. Friday July 13th, 13:13:13 (GMT+4) or something like that)... and assign that to the Ctrl-: key so I can insert the date in my "Captain's log" ;)
(Yes, it would probably be easier to use some kind of python plugin which apparently exists, but where's the fun in that).

Thanks to Juha's conversion tool, converting the Delphi project went flawlessly in Win x64, Laz SVN x86, FPC 2.6.1 SVN x86.

Edit:
had a compilation problem:
However, compilation fails: Application has no Handle.

Code: [Select]
{ This is hacking for troubble...
  We need to unset the Application handler so that the forms
  don't get berserk and start throwing OS error 1004.
  This happens because the main NPP HWND is already lost when the
  DLL_PROCESS_DETACH gets called, and the form tries to allocate a new
  handler for sending the "close" windows message...
}
procedure TNppPlugin.BeforeDestruction;
begin
  Application.Handle := 0;
  Application.Terminate;
  inherited;
end;

Edit: found some code on the forum; hope this is correct:
Searching the forum brought me this:
Code: [Select]
uses
//...
InterfaceBase, win32int

Code: [Select]
 
procedure TNppPlugin.BeforeDestruction;
begin
  //Application.Handle := 0;
  TWin32WidgetSet(WidgetSet).AppHandle:=0;
  Application.Terminate;
  inherited;
end;

Please confirm if this OK ;) (Monkey with wristwatch feeling here but this may improve with sleep and/or caffeine and/or calm reflection...)
Edit: latest code/download available via https://bitbucket.org/reiniero/notepad-pluginlazarus/overview
« Last Edit: August 18, 2012, 05:32:50 pm by BigChimp »
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Notepad++ plugin: conversion questions from Delphi (ongoing)
« Reply #1 on: August 18, 2012, 01:51:14 am »
Ok, apart from the problem in the first post, I have this code:
Code: [Select]
  { First, assign the procedure to the DLLProc variable }
  DllProc := @DLLEntryPoint;
  { Now invoke the procedure to reflect that the DLL is attaching to the process }
  DLLEntryPoint(DLL_PROCESS_ATTACH); 
DllProc is unknown. Any tips? FYI the DLLEntryPoint code is:
Code: [Select]
procedure DLLEntryPoint(dwReason: DWord);
begin
  case dwReason of
  DLL_PROCESS_ATTACH:
  begin
    // create the main object
    //Npp := TDbgpNppPlugin.Create;
  end;
  DLL_PROCESS_DETACH:
  begin
    if (Assigned(Npp)) then Npp.Destroy;
  end;
  //DLL_THREAD_ATTACH: MessageBeep(0);
  //DLL_THREAD_DETACH: MessageBeep(0);
  end;
end;
... yes, I feel like a monkey fiddling with a wristwrach
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Phil

  • Hero Member
  • *****
  • Posts: 2750
Re: Notepad++ plugin: conversion questions from Delphi (ongoing)
« Reply #2 on: August 18, 2012, 02:48:03 am »
Ok, apart from the problem in the first post, I have this code:
Code: [Select]
  { First, assign the procedure to the DLLProc variable }
  DllProc := @DLLEntryPoint;
  { Now invoke the procedure to reflect that the DLL is attaching to the process }
  DLLEntryPoint(DLL_PROCESS_ATTACH); 
DllProc is unknown. Any tips?

I believe DllProc is Delphi-only. Searching the FPC System unit source didn't turn up anything like that.

http://delphi.about.com/cs/adptips2003/a/bltip1003_2.htm

In any case, even it it did exist, aren't you creating a Windows-only package?

In the school of hard knocks I learned two things about porting from Delphi to Lazarus:
(1) It will take 10x longer than you thought it will.
(2) It will never work as well as it does in Delphi because the code is almost always tied tightly to Delphi and Win API.

http://wiki.lazarus.freepascal.org/OrphPort
http://wiki.lazarus.freepascal.org/THtmlPort
http://wiki.lazarus.freepascal.org/GeckoPort

Thanks.

-Phil

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Notepad++ plugin: conversion questions from Delphi (ongoing)
« Reply #3 on: August 18, 2012, 07:29:15 am »
Thanks for your answer and the nice link about DllProc, Phil.

Edit: Microsoft documentation suggests you can use a dllmain procedure http://msdn.microsoft.com/en-us/library/windows/desktop/ms682583%28v=vs.85%29.aspx... but it says:
Quote
DllMain is a placeholder for the library-defined function name. You must specify the actual name you use when you build your DLL. For more information, see the documentation included with your development tools.
... FPC could have defined it as something else.
I looked through the FPC source code for DLL_PROCESS_ATTACH and found hits but at this points, I'd think it best if some DLL guru gives me some guidance ;)

OT: I've attached the current code to the first post and will update that in case anybody wants to point out my mistakes... or improve them ;)

In any case, even it it did exist, aren't you creating a Windows-only package?

In the school of hard knocks I learned two things about porting from Delphi to Lazarus:
(1) It will take 10x longer than you thought it will.
(2) It will never work as well as it does in Delphi because the code is almost always tied tightly to Delphi and Win API.
Yes, I'm creating a Windows only package as Notepad++ is Windows only, too. Though I do have Turbo Delphi, and could conceivably use that for the ANSI plugin, I don't know whether I could use that for the Unicode one. Regardless, having this plugin as Lazarus code may help more people build plugins for Notepad++

The first thing is not a problem. That factor often occurs with my code anyway, and there's no deadline ;)
The second thing may be a problem, depending on how well it turns out to work... but I'd like to try to learn and find out what can be made workable.

http://wiki.lazarus.freepascal.org/OrphPort
http://wiki.lazarus.freepascal.org/THtmlPort
http://wiki.lazarus.freepascal.org/GeckoPort
Don't really see what these links are about? Do you want to me to embed HTML pages in the plugin?
« Last Edit: August 18, 2012, 08:55:07 am by BigChimp »
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

eny

  • Hero Member
  • *****
  • Posts: 1617
Re: Notepad++ plugin: conversion questions from Delphi (ongoing)
« Reply #4 on: August 18, 2012, 09:04:25 am »
Good stuff!
I'll keep following this one  :D

(I'm often at client locations where I use this editor. Still has an annoying regexp search bug though  ;))
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Notepad++ plugin: conversion questions from Delphi (ongoing)
« Reply #5 on: August 18, 2012, 10:59:25 am »
@Eny: any suggestions on the dllproc thing?
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: Notepad++ plugin: conversion questions from Delphi (ongoing)
« Reply #6 on: August 18, 2012, 12:23:52 pm »
Quote
any suggestions on the dllproc thing?
Since you are only interested in the detaching of the dll (the attach is handled directly in the library code) you can use the following:
Code: [Select]
Dll_Process_Detach_Hook := @DLLEntryPoint;to replace
Code: [Select]
DLLProc := @DLLEntryPoint;
If you need to respond to thread attach and detach parameters add also
Code: [Select]
Dll_Thread_Attach_Hook:= @DLLEntryPoint;
Dll_Thread_Detach_Hook:= @DLLEntryPoint;

See function Dll_entry in fpc\rtl\win\syswin.inc to get the "details of operation"  ;)

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Notepad++ plugin: conversion questions from Delphi (ongoing)
« Reply #7 on: August 18, 2012, 01:22:34 pm »
Thanks Ludo, I've thrown in all 3 for completeness' sake (IIUC, that most closesly resembles the Delphi DllProc usage, even though you're right and in this case only DLL_PROCESS_DETACH: is called)
This at least compiles ;) - updated the code in first post, thanks.

Now I'm going to see in what way it will horribly crash notepad++

Edit: unknown exception :) Perhaps it's time to commit to version control and strip out everything that smells of GUI/LCL ;)

Edit: updated http://wiki.lazarus.freepascal.org/shared_library#dllproc, though probably with the wrong terms. As usual, corrections welcome.

Edit: started repository. Latest code available via https://bitbucket.org/reiniero/notepad-pluginlazarus/overview

« Last Edit: August 18, 2012, 01:56:24 pm by BigChimp »
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: Notepad++ plugin: conversion questions from Delphi (ongoing)
« Reply #8 on: August 18, 2012, 02:42:15 pm »
Quote
unknown exception  Perhaps it's time to commit to version control and strip out everything that smells of GUI/LCL
The same here with the unicode version of DelphiPluginTemplate ported to lazarus. 

Got a little further. The exception is caused by the lcl windowclass not registered. The widgetset isn't initialised. The following changed lpr works for me now (mind also the added units):
Code: [Select]
uses
  SysUtils,
  Classes,
  Types,
  Interfaces, LCLIntf, LCLType, LMessages,forms,
  Messages,
  nppplugin in 'lib\nppplugin.pas',
  scisupport in 'lib\SciSupport.pas',
  NppForms in 'lib\NppForms.pas' {NppForm},
  NppDockingForms in 'lib\NppDockingForms.pas' {NppDockingForm},
  helloworldplugin in 'helloworldplugin.pas',
  AboutForms in 'AboutForms.pas' {AboutForm},
  helloworlddockingforms in 'helloworlddockingforms.pas' {HelloWorldDockingForm},
  windows;

{$Include 'lib\NppPluginInclude.pas'}

begin
  { First, assign the procedure to the DLLProc variable }
  Dll_Process_Detach_Hook:= @DLLEntryPoint;
  { Now invoke the procedure to reflect that the DLL is attaching to the process }
  DLLEntryPoint(DLL_PROCESS_ATTACH);
  Application.Initialize;
end.

The docking window works fine but there are some weird characters in the title bar. Probably hacked the unicode too much ;)

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Notepad++ plugin: conversion questions from Delphi (ongoing)
« Reply #9 on: August 18, 2012, 03:06:28 pm »
Heh, I knew I'd get you on board ;)

Thanks, I'll give the ANSI version a whirl...

Edit: this seems to compile, and Notepad++ does not crash :)
Code: [Select]
uses
  SysUtils,
  Classes,
  Types,
  {$ifdef fpc}
  // These allow the LCL windowclass to be registered
  Interfaces, LCLIntf, LCLType, LMessages,forms,
  {$else}
  Windows,
  {$endif fpc}
  Messages,
  nppplugin in 'lib\nppplugin.pas',
  scisupport in 'lib\SciSupport.pas',
  NppForms in 'lib\NppForms.pas' {NppForm},
  NppDockingForms in 'lib\NppDockingForms.pas' {NppDockingForm},
  helloworldplugin in 'helloworldplugin.pas',
  AboutForms in 'AboutForms.pas' {AboutForm},
  helloworlddockingforms in 'helloworlddockingforms.pas' {HelloWorldDockingForm}
  {$ifdef fpc}
  ,
  Windows
  {$endif fpc}
  ;

{$R *.res}

{$ifdef fpc}
{$Include 'lib\NppPluginInclude.pas'}
{$else}
// original code though I doubt that's correct - there is no paslib directory??!?
{$Include '.paslib\NppPluginInclude.pas'}
{$endif}

begin
  {$ifdef fpc}
  Dll_Process_Detach_Hook := @DLLEntryPoint;
  // Leaving these two in causes a crash for some reason:
  //Dll_Thread_Attach_Hook:= @DLLEntryPoint;
  //Dll_Thread_Detach_Hook:= @DLLEntryPoint;
  {$else}
  // Delphi
  { First, assign the procedure to the DLLProc variable }
  DllProc := @DLLEntryPoint;
  {$endif fpc}
  { Now invoke the procedure to reflect that the DLL is attaching to the process }
  DLLEntryPoint(DLL_PROCESS_ATTACH);
end.
« Last Edit: August 18, 2012, 03:14:47 pm by BigChimp »
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: Notepad++ plugin: conversion questions from Delphi (ongoing)
« Reply #10 on: August 18, 2012, 03:11:07 pm »
Attached the unicode version ported to Lazarus. Mind the output path and the run/host application.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Notepad++ plugin: conversion questions from Delphi (ongoing)
« Reply #11 on: August 18, 2012, 03:18:21 pm »
Thanks. The ANSI version does run Plugins/Hello World/Replace hello world (it inserts the text Hello World in the editor).
However, the program quits (presumably crashes) when choosing Plugins/Hello World/Docking Test or About... still, my goal of inserting current date is getting closer, thanks ;)
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: Notepad++ plugin: conversion questions from Delphi (ongoing)
« Reply #12 on: August 18, 2012, 03:27:50 pm »
I don't have ascii notepad++ any more. I did notice however that the unicode version of the template had a few things changed compared to the ascii version. Try the unicode version I attached earlier and remove the -dNPPUnicode in the project settings/other. That supposedly results also in a ascii version of the plugin.
The About and Docking Test are working fine here, except for the weird characters mentioned before.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Notepad++ plugin: conversion questions from Delphi (ongoing)
« Reply #13 on: August 18, 2012, 03:30:15 pm »
The docking window works fine but there are some weird characters in the title bar. Probably hacked the unicode too much ;)
Mmm, in my standalone Unicode version (v 5.9.8, built Jan 2012) it seems to work fine. See screenshot.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Notepad++ plugin: conversion questions from Delphi (ongoing)
« Reply #14 on: August 18, 2012, 03:35:09 pm »
I don't have ascii notepad++ any more. I did notice however that the unicode version of the template had a few things changed compared to the ascii version. Try the unicode version I attached earlier and remove the -dNPPUnicode in the project settings/other. That supposedly results also in a ascii version of the plugin.
The About and Docking Test are working fine here, except for the weird characters mentioned before.
Yep, willdo. Otherwise I can leave the ascii version as well, as the unicode one is the mainstream version anyway.

Ehmm, how to put this nicely? Naah, no nice way. How does one print unicode text though instead of the Hello World stuff? You'd presumably have to use unicodestring (=UTF16) characters?

Also, why don't we have huge problems with GUI in the main program as well as in the DLL? Because the main program does not use the LCL or something?

Inquiring minds want to know ;)

Thanks
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

 

TinyPortal © 2005-2018