Registering UDF dynamically at runtime

Add-in Express™ Support Service
That's what is more important than anything else

Registering UDF dynamically at runtime
 
PeterL




Posts: 5
Joined: 2011-09-09
Is it possible using Addin Express to add a new UDF dynamically at runtime without restarting Excel?
Posted 15 Sep, 2011 14:29:33 Top
Andrei Smolin


Add-in Express team


Posts: 16846
Joined: 2006-05-11
Hello Peter,

This is how you can register an Add-in Express based XLL:

string assemblyPath = ...;
ExcelApp.RegisterXLL(Path.Combine(Path.GetDirectoryName(assemblyPath), "adxloader.dll"));


Regards from Belarus (GMT+2),

Andrei Smolin
Add-in Express Team Leader
Posted 16 Sep, 2011 03:05:27 Top
PeterL




Posts: 5
Joined: 2011-09-09
Hi Andrei,

thanks for the reply. To clarify my question, in my scenario I'd like to be able dynamically (e.g. at runtime) register methods from a certain assembly as Excel functions (and for this RegisterXll would work) But then user should be able to provide a newer implementation of those methods (e.g. newer version of the assembly) and replace previous methods with new ones. Will calling RegisterXLL on a newer version of assembly replace previous implementations with newer one? Or it would cause the conflict?

Additionally, what's interesting, I see public RegisterMethods on ADXXLLModule that seems like an ideal candidate for that (UnregisterMethods is unfortunately private but I suppose I could use reflection to invoke it). Unfortunately simply passing there a list of MethodInfo fails with puzzling InvalidCastException in RegisterXlMethod. Is there any way to invoke this method so that it would work?
Posted 16 Sep, 2011 08:37:11 Top
Andrei Smolin


Add-in Express team


Posts: 16846
Joined: 2006-05-11
Hi Peter,

Replacing an Office extension with a new version requires that you restart the host application. This is a must because the host application (Excel in your case) loads the extension and there's no way to unload it. While the extension is loaded, the corresponding file is locked and you cannot replace it.

With managed extensions, we have more freedom because Excel loads the loader (which loads the assimbly) and, thinking abstractly, we could program the loader to allow you to unload the assembly and, in this way, to let you replace the assembly with a new version. But in the general case this will not work because it doesn't allow to update the loader itself.

Regards from Belarus (GMT+2),

Andrei Smolin
Add-in Express Team Leader
Posted 16 Sep, 2011 09:33:25 Top
PeterL




Posts: 5
Joined: 2011-09-09
Well, if I could dynamically load/unload the assembly that implements my xll functions that would be enough as that assembly changes much more frequently than the loader.

Trying another angle then, what might be the reason why on RegisterMethods ADXSLLModule throws InvalidCastException when I'm trying to call it (even for trivial methods with no parameters)?

Judging by this method I presume you do something similar to what Excel-DNA is doing internally (e.g. making Win32 call to Excel asking it to register a function) If that's the case then this might give me the functionality I need.

Thanks again.
Posted 16 Sep, 2011 11:08:20 Top
Andrei Smolin


Add-in Express team


Posts: 16846
Joined: 2006-05-11
Peter,

PeterL writes:
Trying another angle then, what might be the reason why on RegisterMethods ADXSLLModule throws InvalidCastException when I'm trying to call it (even for trivial methods with no parameters)?


It doesn't expect to be used in this way.

Probably, you can make use of How to develop the modular architecture of your COM and XLL Add-in? Is it what you are looking for?

Regards from Belarus (GMT+2),

Andrei Smolin
Add-in Express Team Leader
Posted 19 Sep, 2011 06:12:10 Top