Community
    • Login

    NPP C# plugin - How to get the list of Notepad++'s open files.

    Scheduled Pinned Locked Moved Notepad++ & Plugin Development
    17 Posts 4 Posters 10.6k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • AlessandroA
      Alessandro
      last edited by Alessandro

      Hi everybody,

      now that I’m able to compile dll I came to a point that I need a way to get the list of the files open in Notepad++.

      On the web I found a couple of plugins dll but I wasn’t able to get their source code.

      Any suggestion on how to get the list of Npp’s open files? May be a link to a C# plugin?

      Thank you in advance

      Regards
      Alessandro

      Claudia FrankC 1 Reply Last reply Reply Quote 0
      • Claudia FrankC
        Claudia Frank @Alessandro
        last edited by

        @Alessandro

        you need to send NPPM_GETNBOPENFILES message.
        See here for more
        information how to use the messages.

        Cheers
        Claudia

        1 Reply Last reply Reply Quote 1
        • AlessandroA
          Alessandro
          last edited by

          @Claudia-Frank

          Thank you for the suggestion. I read the content of your link understanding the meaning of messages and notifications Notepad++ exchanges with or forwards to plugins.

          Nevertheless having a C# example code it should of great help .

          Cheers
          Alessandro

          Claudia FrankC 2 Replies Last reply Reply Quote 0
          • Claudia FrankC
            Claudia Frank @Alessandro
            last edited by

            @Alessandro

            I might be wrong but I thought there is demo included in the c# template, isn’t it?
            If it is search for something like SendMessage.
            I assume you need to provide the npp handle and then the message id and parameters if used.
            It could be that the demo only uses scintilla message but the technique is the same.

            Cheers
            Claudia

            1 Reply Last reply Reply Quote 0
            • Claudia FrankC
              Claudia Frank @Alessandro
              last edited by

              @Alessandro

              from the demo

              StringBuilder sbIniFilePath = new StringBuilder(Win32.MAX_PATH);
              Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_GETPLUGINSCONFIGDIR, Win32.MAX_PATH, sbIniFilePath);
              iniFilePath = sbIniFilePath.ToString();
              

              instead of using NPPM_GETPLUGINSCONFIGDIR you use the NPPM_GETNBOPENFILES
              and the required parameters.

              Cheers
              Claudia

              1 Reply Last reply Reply Quote 0
              • AlessandroA
                Alessandro
                last edited by

                As suggested by Claudia Frank I dowloaded the C# plugin templete from here (I found in Notepad++ Wiki web site).

                Unlikely once copied the its dll templete in the NPP plugin folder (C:\Program Files (x86)\Notepad++\plugins) when Notepad++ starts a message box appears saying that

                “This ANSI plugin is not compatible with your Unicode Notepad++”

                What am I doing wrong?
                Am I using the wrong C# plugin templete?

                Cheers
                Alessandro

                Claudia FrankC Scott SumnerS 2 Replies Last reply Reply Quote 0
                • Claudia FrankC
                  Claudia Frank @Alessandro
                  last edited by

                  @Alessandro

                  did I suggest to download the c# template from file-upload.net? I don’t think so.
                  From the wiki that you refer to

                  The Common Plugin Interface
                  bool isUnicode()
                  A plugin is designed to either work with an ANSI or Unicode build of Notepad++. ANSI plugins must not define this function. Unicode plugins must define it, and it must return true.

                  Cheers
                  Claudia

                  1 Reply Last reply Reply Quote 0
                  • Scott SumnerS
                    Scott Sumner @Alessandro
                    last edited by

                    @Alessandro

                    “This ANSI plugin is not compatible with your Unicode Notepad++” What am I doing wrong? Am I using the wrong C# plugin templete?

                    You also asked this SAME question in this OTHER thread:
                    https://notepad-plus-plus.org/community/topic/14495/npp-c-plugin-template-for-unicode

                    Was @dail 's answer in that other thread not correct or insufficient?

                    1 Reply Last reply Reply Quote 0
                    • AlessandroA
                      Alessandro
                      last edited by

                      I made a lot of confusion, so sorry.

                      You didn’t suggest to download the C# template from file-upload.net.
                      Reading your kind post I thought it was right place from which download it. I was evidently wrong also because that template doesn’t work ( “This ANSI plugin is not compatible with your Unicode Notepad++” issue).

                      Let’s start from the beginning.

                      Can you suggest where to get the demo C# template you talked about and where you get the SendMessage example?

                      Regards
                      Alessandro

                      Claudia FrankC 1 Reply Last reply Reply Quote 1
                      • Claudia FrankC
                        Claudia Frank @Alessandro
                        last edited by

                        @Alessandro

                        the plugin template is from kblisted as stated in your other post, to be exact it is from
                        https://github.com/kbilsted/NotepadPlusPlusPluginPack.Net
                        If you downloaded from this site you should have a Demo Plugin folder which contains the
                        demo.cs file which itself has several SendMessage examples.

                        The error about unicode and ansi version is coming from notepadd++.
                        What it does is, simple explanation, notepad++ loads your plugin dll and is calling
                        a dll function called isUnicode(). If this function returns true then npp assumes it is
                        a valid unicode plugin. If this function cannot be called, because you didn’t implement it or it returns false, then it throws this error.
                        The plugin template has this function already defined within UnmanagedExports.cs.

                        Makes this sense to you?

                        Cheers
                        Claudia

                        1 Reply Last reply Reply Quote 0
                        • AlessandroA
                          Alessandro
                          last edited by

                          Dear Claudia,

                          I want to thank you so much for the precious help you gave me.

                          Everything is clear now. Right C# template, right demo project, exactly what I was looking for. The reason of the NPP’s incompatible dll message when NPP stars is now clear.

                          I greatly appreciated your patience and the clarity of your explanation thanks to which I fully understood.

                          Cheers
                          Alessandro

                          Claudia FrankC 1 Reply Last reply Reply Quote 1
                          • Claudia FrankC
                            Claudia Frank @Alessandro
                            last edited by

                            @Alessandro,

                            my pleasure!

                            Cheers
                            Claudia

                            1 Reply Last reply Reply Quote 0
                            • AlessandroA
                              Alessandro
                              last edited by

                              @Claudia

                              Can you explain me why the method saveCurrentSessionDemo present in the Demo NPP C# plugin, even if the Win32.SendMessage return the full path of the file to be saved (i.e. C:\text.txt) the file text.txt is not created. It happens only if the static string sessionFilePath = @“C:\text.txt”" and it doesn’t if *static string sessionFilePath = @“C:\Users\USR1\Desktop\text.txt”.

                              Here is the source code of the saveCurrentSessionDemo

                              static void saveCurrentSessionDemo()
                              {
                              string sessionPath = Marshal.PtrToStringUni(Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_SAVECURRENTSESSION, 0, sessionFilePath));
                              if (!string.IsNullOrEmpty(sessionPath))
                              MessageBox.Show(sessionPath, “Saved Session File :”, MessageBoxButtons.OK);
                              }

                              Regards and thanks in advance
                              Alessandro

                              Claudia FrankC 1 Reply Last reply Reply Quote 0
                              • Claudia FrankC
                                Claudia Frank @Alessandro
                                last edited by

                                @Alessandro

                                I’m a little bit confused about what is working and what doesn’t.
                                Are you saying if you specify sessionFilePath = “C:\text.txt” it is working
                                but if you specify sessionFilePath = “C:\Users\USR1\Desktop\text.txt"
                                it does not work?

                                Or is it vice versa?

                                As I’m not a csharp developer I tried this with python and both ways work for me
                                so I have to assume that the path C:\Users\USR1\Desktop does not exist or permissions
                                problems happen.

                                Cheers
                                Claudia

                                1 Reply Last reply Reply Quote 0
                                • AlessandroA
                                  Alessandro
                                  last edited by

                                  You confusion comes from the fact I’m not a native english, sorry.
                                  But yes in my case sessionFilePath = “C:\text.txt” doesn’t work. It also for me a matter of permissions. Then the question is Win32.SendMessage doesn’t return a path null or empty as the file ins’t created.

                                  Thank you so much.
                                  Alessandro

                                  Claudia FrankC rinku singhR 2 Replies Last reply Reply Quote 0
                                  • Claudia FrankC
                                    Claudia Frank @Alessandro
                                    last edited by Claudia Frank

                                    I tried this with python and it is behaving the same. I don’t get an info that the call failed.
                                    I did a quick look to the npp source code and it looks like we should get the info

                                    	case NPPM_SAVECURRENTSESSION:
                                    	{
                                    		return (LRESULT)fileSaveSession(0, NULL, reinterpret_cast<const TCHAR *>(lParam));
                                    	}
                                    

                                    and

                                    const TCHAR * Notepad_plus::fileSaveSession(size_t nbFile, TCHAR ** fileNames, const TCHAR *sessionFile2save)
                                    {
                                        if (sessionFile2save)
                                        {
                                            Session currentSession;
                                            if ((nbFile) && (fileNames))
                                            {
                                                for (size_t i = 0 ; i < nbFile ; ++i)
                                                {
                                                    if (PathFileExists(fileNames[i]))
                                                        currentSession._mainViewFiles.push_back(generic_string(fileNames[i]));
                                                }
                                            }
                                            else
                                                getCurrentOpenedFiles(currentSession);
                                    
                                            (NppParameters::getInstance())->writeSession(currentSession, sessionFile2save);
                                            return sessionFile2save;
                                        }
                                        return NULL;
                                    }
                                    

                                    In case of success it should return the full path, in case of error NULL.
                                    So I assume, this isn’t correctly implemented, either in python and c# plugin pack
                                    or npp. Hmm, as I do not have a windows operating system anymore I can hardly test it. Sorry.

                                    Cheers
                                    Claudia

                                    1 Reply Last reply Reply Quote 0
                                    • rinku singhR
                                      rinku singh @Alessandro
                                      last edited by

                                      @Alessandro
                                      BGuenthner has good answer here for get all open files and current open files include new tab
                                      https://sourceforge.net/p/notepad-plus/discussion/482781/thread/90ae2b1a/

                                      1 Reply Last reply Reply Quote 0
                                      • First post
                                        Last post
                                      The Community of users of the Notepad++ text editor.
                                      Powered by NodeBB | Contributors