Unregister & Replace .xll

2,626 views
Skip to first unread message

Mad1523

unread,
Sep 2, 2011, 5:33:06 PM9/2/11
to Excel-DNA
Hey guys,

Does anyone know of a way to delete or replace an XLL file after you
unregister it with xlfUnregister? A proper method to unregister the
xll in .29 has been discussed here:

http://groups.google.com/group/exceldna/browse_thread/thread/57f13ca6180eb7f7/d8bb68008a8ed616

However, it seems that Excel keeps a file lock on the .xll so that it
cannot be deleted or replaced even after unregistering. Perhaps it
would be useful to do so if someone implemented an auto update feature
where you could update the add-in without having to close excel?

Any ideas?

Govert van Drimmelen

unread,
Sep 2, 2011, 6:22:08 PM9/2/11
to Excel-DNA
Hi,

The behaviour in this case is quite sensitive to how you unregister
the add-in.
This is what I have for the unregister in a test add-in that acts as a
'host':

[ExcelCommand(MenuName="AddinHost", MenuText="Unload Addin")]
public static void UnloadAddin()
{
string theName = @"C:\Work\ExcelDna\Samples\MetaAddin
\AddIn.xll";
object removeId = XlCall.Excel(XlCall.xlfRegister, theName,
"xlAutoRemove", "I" , ExcelMissing.Value, ExcelMissing.Value, 2);
object removeResult = XlCall.Excel(XlCall.xlfCall, removeId);
object removeUnregister = XlCall.Excel(XlCall.xlfUnregister,
removeId);
object success = XlCall.Excel(XlCall.xlfUnregister,
registerId);
}

So I have two add-in .xlls, one the host and the other the real add-
in. The host loads and unloads the other add-in, and I don't find that
the other add-in .xll is locked after unregistering. Maybe you are
doing something different. If so, perhaps you can try to make a small
example that reproduces your problem - preferable just one or two .dna
files that define add-ins.

If you have an ExternalLibrary .dll assembly which you load, then you
can mark it as:
<ExternalLibrary Path="..." LoadFromBytes="true"/>

Then the .dll file is not locked and can be overwritten even while the
add-in is loaded. But you have to reload the .xll file to get Excel-
DNA to load the new version of the .dll assembly.

Having a single file .xll that is able to update itself might be
tricky... I think the file can be renamed while loaded, which might
help.
Is this the scenario you are after?

-Govert

On Sep 2, 11:33 pm, Mad1523 <mad1...@gmail.com> wrote:
> Hey guys,
>
> Does anyone know of a way to delete or replace an XLL file after you
> unregister it with xlfUnregister? A proper method to unregister the
> xll in .29 has been discussed here:
>
> http://groups.google.com/group/exceldna/browse_thread/thread/57f13ca6...

Mad1523

unread,
Sep 2, 2011, 8:28:16 PM9/2/11
to Excel-DNA
Thanks for the response.

In my scenario, I have one packed xll with the compiled dll and some
dependency dlls inside. When I unregistered the add-in from inside
itself, it unloads but the packed xll is still locked. The idea was to
spawn an external exe (from an embedded resource) that would download
and replace the xll after it was unloaded. I did not think about
having a host add-in that loads the real add-in. Perhaps that is a
more elegant solution. I will give that a try and see what happens. In
this case, you would have 2 packed xll's and 2 instances of exceldna?

Mad1523

unread,
Sep 7, 2011, 1:41:59 AM9/7/11
to Excel-DNA
Govert,

I found that after loading/unloading an add-in through a 'host' add-
in, I can rename the loaded sample add-in .xll but it is still locked
for deletion in Windows (so it cannot be replaced). The .dna file is
not locked, but that is not what I'm trying to replace. Do you get the
same result? I get this result regardless of whether or not the .xll
is packed.

Here are the two sample .dna files:

Host: https://gist.github.com/1199844
Sample Add-In: https://gist.github.com/1199848

Thanks



On Sep 2, 3:22 pm, Govert van Drimmelen <gov...@icon.co.za> wrote:

Govert van Drimmelen

unread,
Sep 7, 2011, 5:51:59 AM9/7/11
to Excel-DNA
Hi,

Your host is not quite right.
You need to store the RegisterId when you load the other add-in, and
use that in the unregister calls.
So you have two unregister calls:
1. To unregister the xlAutoRemove function
2. To unregister the loaded add-in after you've called xlAutoRemove.

Try the version below (also at https://gist.github.com/1200178).

-Govert


<DnaLibrary Name="AddInHost" Language="C#" RuntimeVersion="v4.0">
<Reference Name="System.Windows.Forms" />
<![CDATA[
using System;
using System.Windows.Forms;
using ExcelDna.Integration;

public class MyMacros
{

public static object registerId;

[ExcelCommand(MenuName="AddinHost", MenuText="Load Addin")]
public static void LoadAddin()
{
XlCall.XlReturn result = XlCall.TryExcel(XlCall.xlfRegister,
out registerId, @"C:\Work\ExcelDna\Samples\MetaAddin\AddIn.xll");
}

[ExcelCommand(MenuName="AddinHost", MenuText="Unload Addin")]
public static void UnloadAddin()
{
MessageBox.Show("About to call xlAutoRemove.");

string theName = @"C:\Work\ExcelDna\Samples\MetaAddin
\AddIn.xll";
object removeId = XlCall.Excel(XlCall.xlfRegister, theName,
"xlAutoRemove", "I" , ExcelMissing.Value, ExcelMissing.Value, 2);
object removeResult = XlCall.Excel(XlCall.xlfCall, removeId);
object removeUnregister = XlCall.Excel(XlCall.xlfUnregister,
removeId);

object success = XlCall.Excel(XlCall.xlfUnregister,
registerId);
MessageBox.Show("Result of Unregister: " +
success.ToString() );
}
}

public static class MyFunctions
{
public static string AddinHostTestFunction()
{
return "Hello from the add-in host!";
}


[ExcelCommand(MenuName="AddinHost", MenuText="Say Hello!")]
public static void AddIn()
{
MessageBox.Show("Hello from the add-in host!");
}
}
]]>
</DnaLibrary>

Mad1523

unread,
Sep 7, 2011, 11:33:58 PM9/7/11
to Excel-DNA
That was the trick!!

Works great!

Thanks.

On Sep 7, 2:51 am, Govert van Drimmelen <gov...@icon.co.za> wrote:
> Hi,
>
> Your host is not quite right.
> You need to store the RegisterId when you load the other add-in, and
> use that in the unregister calls.
> So you have two unregister calls:
> 1. To unregister the xlAutoRemove function
> 2. To unregister the loaded add-in after you've called xlAutoRemove.
>
> Try the version below (also athttps://gist.github.com/1200178).

Grégory Berger

unread,
Mar 27, 2014, 11:53:44 AM3/27/14
to exce...@googlegroups.com
Hi guys,

works great for "Excel functions" add-ins. However my xll file is still locked when I have a tlb for early-binding on VBA side + some Excel functions.

What I do is:
- load the hosting xll
- load the target xll
- load the tlb
- run the vba code -> works fine
- run the excel function -> works fine
- remove reference on tlb
- unload target xll from host

-> the tlb is released but the xll is still locked by Excel.

Any way to upgrade the XLL in that case without having to close all Excel instance?

Much appreciated,
Greg

Govert van Drimmelen

unread,
Mar 27, 2014, 2:40:03 PM3/27/14
to exce...@googlegroups.com
Hi Greg,

I had a quick look, and you're right - unloading of the COM server is not currently supported.
It looks like it would take some work to implement....

-Govert

Mirror

unread,
Sep 12, 2016, 5:37:30 AM9/12/16
to Excel-DNA
Hi,

I encounter the same issue.
Because I load/unload my Addin using VBA, I translate the above idea to VBA calling XLM functions.
Thank you for the C# code.
It works well so far.

------------------
Option Explicit
Const file As String = "[input your xll file name with full path]"

Sub TestExcel4MacroLoad()
    Dim v As Variant
    v = Application.RegisterXLL(file)
End Sub
 
Sub TestExcel4MacroRemove()
    Dim vXlAutoRemoveHandle As Variant
    Dim s As String
    s = "REGISTER(""" & file & """, " & """xlAutoRemove""" & "," & """J""" & "," & """TestAutoRemoveCode""" & ",,1)"
    vXlAutoRemoveHandle = Application.ExecuteExcel4Macro(s)
    
    Dim vResultXlAutoRemove As Variant
    s = "CALL(" & vXlAutoRemoveHandle & ")"
    vResultXlAutoRemove = Application.ExecuteExcel4Macro(s)
    
    Dim vResultUnregisterXlAutoRemove As Variant
    s = "UNREGISTER(" & vXlAutoRemoveHandle & ")"
    vResultUnregisterXlAutoRemove = Application.ExecuteExcel4Macro(s)

    Dim vResultUnregister As Variant
    s = "UNREGISTER(""" & file & """)"
    vResultUnregister = Application.ExecuteExcel4Macro(s)
End Sub
------------------

Hiroto

Marc G

unread,
Dec 27, 2022, 6:22:02 PM12/27/22
to Excel-DNA
So is it necessary for me to create an Add in for me to remove an Add in?  Or is there an easier way to do this now?

Govert van Drimmelen

unread,
Dec 28, 2022, 2:35:23 AM12/28/22
to exce...@googlegroups.com

You can press Alt+t, i to bring up the Add-ins dialog, and remove your add-in from there.

 

-Govert

--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to exceldna+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/exceldna/e4c7070b-76fc-4d65-83e2-36d9e1bdf68an%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages