Loading plugins in second instance of OD

This forum is for programmers who have questions about the source code.
Post Reply
User avatar
wjstarck
Posts: 936
Joined: Tue Jul 31, 2007 7:18 am
Location: Keller, TX
Contact:

Loading plugins in second instance of OD

Post by wjstarck » Tue Jul 18, 2023 12:57 pm

Is there a reason why plugins won't load when a second (or more) instance of Open Dental is launched?

This creates a lot of confusion for our customers. One scenario in particular is when person A is logged in to Windows and has OD running, and they switch users to person B without quitting their running instance of OD. When person B launches OD, the plugin doesn't load.

If they could be loaded, is this a feature request?
Cheers,

Bill Starck, DDS
Big Idea Software, LLC
Developer, EASy(Electronic Anesthesia System) for Open Dental
817-807-1709
TX, USA

User avatar
jsalmon
Posts: 1555
Joined: Tue Nov 30, 2010 12:33 pm
Contact:

Re: Loading plugins in second instance of OD

Post by jsalmon » Tue Jul 18, 2023 5:10 pm

Not that I'm aware of. I just wrote a 'hello world' plug-in and it loaded up in both instances I just tested.

Code: Select all

using OpenDentBusiness;
using System.Windows.Forms;

namespace PluginHelloWorld {
	public class Plugin : PluginBase {

		public override bool HookAddCode(object sender,string hookName,params object[] parameters) {
			switch(hookName) {
				case "FormOpenDental.Load_end":
					MessageBox.Show("Hello World!");
					return true;
				default:
					return false;
			}
		}

	}
}
The best thing about a boolean is even if you are wrong, you are only off by a bit.

Jason Salmon
Open Dental Software
http://www.opendental.com

User avatar
wjstarck
Posts: 936
Joined: Tue Jul 31, 2007 7:18 am
Location: Keller, TX
Contact:

Re: Loading plugins in second instance of OD

Post by wjstarck » Tue Jul 18, 2023 8:31 pm

Jason-

Thanks for that.

OK this is weird. Some time ago OD staff added myPlugin[VersionMajMin].dll to the Plug-in.dll field in the Program Link setup form so OD could select from a list of plugins and load the plugin that matched the running version of OD. So far so good.

So in my OD program directory there'll be a number of plugins

Anesthesia.dll
Anesthesia22.3.dll
Anesthesia22.4.dll
Anesthesia23.1.dll

OD will grab the matching plugin version, strip off the version number and rename it Anesthesia.dll and load it.

So, if I'm running, say, OD 23.1, I notice that if I remove Anesthesia23.1.dll from the OD program folder, the plugin will load in more than one instance of OD. If I put it back it will only run in the first instance.

Any idea why that would be the case?
Cheers,

Bill Starck, DDS
Big Idea Software, LLC
Developer, EASy(Electronic Anesthesia System) for Open Dental
817-807-1709
TX, USA

User avatar
jsalmon
Posts: 1555
Joined: Tue Nov 30, 2010 12:33 pm
Contact:

Re: Loading plugins in second instance of OD

Post by jsalmon » Wed Jul 19, 2023 8:45 am

I have a theory based on the description from our website:
...For example, if using version 14.3.23, it would look for MyPlugin14.3.dll. If that file is found, it would replace MyPlugin.dll with the contents of MyPlugin14.3.dll, and then it would load MyPlugin.dll as normal. In a typical setting, this copy sequence gets triggered every time Open Dental starts up, ensuring a fresh copy of the dll. If Open Dental is on a newer version, and the dll is still on an older version with no matching [VersionMajMin], then Open Dental will attempt to load the old MyPlugin.dll.
https://www.opendental.com/manual/plugins.html

My theory is that the first instance of Open Dental has your plug-in dll tied up (it is using it) and the 'copy sequence' is failing. Therefore, the program ignores the failure and just launches Open Dental without the plug-in assembly loaded into memory. I'll create a ticket to have someone look into this. You could try moving all of the 'versioned' dlls out of the folder and see if you are then able to load your plug-in with multiple instances of Open Dental.
The best thing about a boolean is even if you are wrong, you are only off by a bit.

Jason Salmon
Open Dental Software
http://www.opendental.com

User avatar
wjstarck
Posts: 936
Joined: Tue Jul 31, 2007 7:18 am
Location: Keller, TX
Contact:

Re: Loading plugins in second instance of OD

Post by wjstarck » Wed Jul 19, 2023 8:16 pm

Jason-

I was able to modify the code in PluginLoader.LoadAllPlugins like so (<------ denotes modified code):

Code: Select all

				string dllPathWithVersion = String.Empty; <----------------------------------------------
				if (dllPath.Contains("[VersionMajMin]")) {
					Version vers=Assembly.GetAssembly(typeof(Db)).GetName().Version;
					dllPathWithVersion=dllPath.Replace("[VersionMajMin]",vers.Major.ToString()+"."+vers.Minor.ToString());
					dllPath=dllPath.Replace("[VersionMajMin]","");//now stripped clean
					if(File.Exists(dllPathWithVersion)) {
						File.Copy(dllPathWithVersion,dllPath,true);
					}
					else{
						//try the Plugins folder
						if(PrefC.AtoZfolderUsed!=DataStorageType.InDatabase) {//must have an AtoZ folder to check
							string dllPathVersionCentral=FileAtoZ.CombinePaths(ImageStore.GetPreferredAtoZpath(),"Plugins",
								listPrograms[i].PluginDllName.Replace("[VersionMajMin]",vers.Major.ToString()+"."+vers.Minor.ToString()));
							if(FileAtoZ.Exists(dllPathVersionCentral)) {
								FileAtoZ.Copy(dllPathVersionCentral,dllPath,FileAtoZSourceDestination.AtoZToLocal,doOverwrite:true);
							}
						}
					}
				}
and a little lower:

Code: Select all

				//The dll was found, try and load it in.
				PluginBase plugin = null;
				Assembly ass = null;
				string assName = "";
				try {
					byte[] bytes = File.ReadAllBytes(dllPathWithVersion); <----------------------------------------
					Assembly assembly = Assembly.Load(bytes); <----------------------------------------------
					//ass =Assembly.LoadFile(dllPath); <-----------------------------------------------
					assName = Path.GetFileNameWithoutExtension(dllPath);
					string typeName = assName + ".Plugin";
					Type type = assembly.GetType(typeName); <--------------------------------------
					plugin = (PluginBase)Activator.CreateInstance(type);
					
This was modified from this little gem:

https://social.msdn.microsoft.com/Forum ... rplanguage
"Another option is to never load the actual assembly into the domain.

byte[] bytes = File.ReadAllBytes(@"C:\Users\billybob\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe");
Assembly assm = Assembly.Load(bytes);

This leaves the assembly on disk completely unrelated to the current application."
Cheers,

Bill Starck, DDS
Big Idea Software, LLC
Developer, EASy(Electronic Anesthesia System) for Open Dental
817-807-1709
TX, USA

User avatar
jsalmon
Posts: 1555
Joined: Tue Nov 30, 2010 12:33 pm
Contact:

Re: Loading plugins in second instance of OD

Post by jsalmon » Thu Jul 20, 2023 2:12 pm

That is certainly an elaborate way to confirm my theory. Glad we were able to figure out the exact issue you were running into.
The best thing about a boolean is even if you are wrong, you are only off by a bit.

Jason Salmon
Open Dental Software
http://www.opendental.com

User avatar
jsalmon
Posts: 1555
Joined: Tue Nov 30, 2010 12:33 pm
Contact:

Re: Loading plugins in second instance of OD

Post by jsalmon » Fri Aug 18, 2023 11:25 am

Added the bug "Plug-ins are not loading in additional instances of Open Dental when using the update sequence" to our tracker and I'm training another engineer in the ways of plug-ins.
https://opendentalsoft.com:1943/ODBugTr ... sions.aspx
The best thing about a boolean is even if you are wrong, you are only off by a bit.

Jason Salmon
Open Dental Software
http://www.opendental.com

User avatar
jsalmon
Posts: 1555
Joined: Tue Nov 30, 2010 12:33 pm
Contact:

Re: Loading plugins in second instance of OD

Post by jsalmon » Wed Aug 30, 2023 10:33 am

The bug "Plug-ins were not loading in additional instances of Open Dental when using the update sequence" has been fixed and will be released with v23.1.45 and v23.2.15.
https://opendentalsoft.com:1943/ODBugTr ... sions.aspx
The best thing about a boolean is even if you are wrong, you are only off by a bit.

Jason Salmon
Open Dental Software
http://www.opendental.com

User avatar
wjstarck
Posts: 936
Joined: Tue Jul 31, 2007 7:18 am
Location: Keller, TX
Contact:

Re: Loading plugins in second instance of OD

Post by wjstarck » Thu Sep 14, 2023 8:53 am

Thanks for that Jason.

Definitely will cut down on the support calls :|
Cheers,

Bill Starck, DDS
Big Idea Software, LLC
Developer, EASy(Electronic Anesthesia System) for Open Dental
817-807-1709
TX, USA

Post Reply