NPP C# plugin - How to get the list of Notepad++'s open files.
-
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 -
you need to send NPPM_GETNBOPENFILES message.
See here for more
information how to use the messages.Cheers
Claudia -
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 -
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 -
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 -
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 -
did I suggest to download the c# template from file-upload.net? I don’t think so.
From the wiki that you refer toThe 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 -
“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-unicodeWas @dail 's answer in that other thread not correct or insufficient?
-
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 -
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 -
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 -
-
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 -
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 -
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 -
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 infocase 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 -
@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/