Hosted Word is not released with a C# add-in


A

Alex

I'm having a strange issue:

I have an IDTExtensibility2 Word add-in written in C#.
It adds toolbars, menus and buttons, handles events, etc. You know, the stuff that add-ins usually do...

When I start Word as a standalone program, the add-in loads and everything works peachy.
When I close Word, everything gets cleaned up (presumably, at least according to task manager).

However, when Word is hosted inside an application, it does not unload when the application tries to close it.
It just hangs in memory (and the hosting app does too).

Questions:

1) Any ideas what can be wrong and how to fix it?

or, failing that,

2) How can my add-in detect that it is being loaded in a hosted instance of Word and silently not load itself?

Thanks,
Alex.
 
Ad

Advertisements

A

Alex

Update below...

I have an IDTExtensibility2 Word add-in written in C#.
It adds toolbars, menus and buttons, handles events, etc. You know, the stuff that add-ins usually do...

When I start Word as a standalone program, the add-in loads and everything works peachy.
When I close Word, everything gets cleaned up (presumably, at least according to task manager).

However, when Word is hosted inside an application, it does not unload when the application tries to close it.
It just hangs in memory (and the hosting app does too).

Questions:

1) Any ideas what can be wrong and how to fix it?

or, failing that,

2) How can my add-in detect that it is being loaded in a hosted instance of Word and silently not load itself?

Update:

Re Q.2: http://groups.google.ca/groups?selm=fad501c21b2c$5aef2c10$37ef2ecf@TKMSFTNGXA13
(Can someone provide more info about the "custom" parameters?)

Q.1 is still an issue!

Thanks,
Alex.
 
P

Peter Huang [MSFT]

Hi

For the custom argument, currently Office supplies a single value -- a flag
that indicates how the application was started (by user, by OLE, by
Automation) as your link indicate.
For the first problem, I think you may try to explicitly release the
appalication object.

public void OnDisconnection(Extensibility.ext_DisconnectMode
disconnectMode, ref System.Array custom)
{
Trace.WriteLine("OnDisconnection");
System.Runtime.InteropServices.Marshal.ReleaseComObject(applicationObject);
applicationObject =null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}

NOTE: If you have other reference to the Word Objects,(e.g. menu ....), you
will also need to release them recursively.
I suggest you start from the template generated code, so that there will
less object for released.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Alex

Hello Peter,

Thanks for your reply.

"Peter Huang" said:
For the first problem, I think you may try to explicitly release the
appalication object.

public void OnDisconnection(Extensibility.ext_DisconnectMode
disconnectMode, ref System.Array custom)
{
Trace.WriteLine("OnDisconnection");
System.Runtime.InteropServices.Marshal.ReleaseComObject(applicationObject);
applicationObject =null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}

NOTE: If you have other reference to the Word Objects,(e.g. menu .....), you
will also need to release them recursively.
By ReleaseComObject as well?
 
P

Peter Huang [MSFT]

Hi

Comments line.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
From: "Alex" <inssup35@online.nospam>
References: <ek5e0qGQFHA.1340@TK2MSFTNGP10.phx.gbl>
Subject: Re: Hosted Word is not released with a C# add-in
Date: Fri, 15 Apr 2005 18:51:23 -0400
Lines: 31
MIME-Version: 1.0
Content-Type: text/plain;
charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <ew7Bn2gQFHA.2932@TK2MSFTNGP09.phx.gbl>
Newsgroups: microsoft.public.office.developer.com.add_ins
NNTP-Posting-Host: firebox.markham.insystems.com 216.191.154.61
Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP0
9.phx.gbl
Xref: TK2MSFTNGXA02.phx.gbl microsoft.public.office.developer.com.add_ins:8263
X-Tomcat-NG: microsoft.public.office.developer.com.add_ins

Hello Peter,
Thanks for your reply.

By ReleaseComObject as well?
Yes Just as we do with applicationObject.
ReleaseComObject--->set to null--->GC.Collect

 
A

Alex

Hello Peter,

Unfortunately, it does not seem to work.
Yes Just as we do with applicationObject.
ReleaseComObject--->set to null--->GC.Collect
During the life of the add-in, it accesses a lot of Word objects (mostly as local variables that go out of scope).
It is not feasible to call ReleaseComObject on every temporary variable that may contain a Word object in every function.

Is there a simpler way?
 
A

Alex

I narrowed it down, it happens with some MFC applications that open Word documents in a hosted environment.
Word inside IE6 seems to work OK.

Any ideas?

Thanks,
Alex.
 
P

Peter Huang [MSFT]

Hi

Here is link about an ActiveX Control host.
You may have a try to see if that control have problem when hosting word
document.
Visual C++ ActiveX Control for hosting Office documents in Visual Basic or
HTML
http://support.microsoft.com/?id=311765

If the code works, I think you may check your MFC application code with the
DSOFramer code.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Alex

"Peter Huang" said:
Hi

Here is link about an ActiveX Control host.
You may have a try to see if that control have problem when hosting word
document.
Visual C++ ActiveX Control for hosting Office documents in Visual Basic or
HTML
http://support.microsoft.com/?id=311765

If the code works, I think you may check your MFC application code with the
DSOFramer code.
Thanks Peter, I will check it out.
 
P

Peter Huang [MSFT]

Hi

Thanks for your quickly reply!

And I look forward to hearing from you!

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Alex

No joy :-(

Getting exceptions on exit:

************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Windows.Forms.NativeWindow.RemoveWindowFromIDTable(IntPtr handle)
at System.Windows.Forms.TabControl.OnHandleDestroyed(EventArgs e)
at System.Windows.Forms.Control.WmDestroy(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.TabControl.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


and:


************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Windows.Forms.DataGrid.OnHandleDestroyed(EventArgs e)
at System.Windows.Forms.Control.WmDestroy(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


Additional info:

************** Loaded Assemblies **************
mscorlib
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase: file:///c:/winnt/microsoft.net/framework/v1.1.4322/mscorlib.dll
----------------------------------------
MyAddin1
Assembly Version: 0.0.1935.40045
Win32 Version: 0.0.1935.40045
CodeBase: file:///C:/Work/PreAuthor%20Tests/MyAddin1/bin/Debug/MyAddin1.DLL
----------------------------------------
Extensibility
Assembly Version: 7.0.3300.0
Win32 Version: 7.00.9466
CodeBase: file:///c:/winnt/assembly/gac/extensibility/7.0.3300.0__b03f5f7f11d50a3a/extensibility.dll
----------------------------------------
Microsoft.Office.Interop.Word
Assembly Version: 11.0.0.0
Win32 Version: 11.0.5530
CodeBase: file:///c:/winnt/assembly/gac/microsoft.office.interop.word/11.0.0.0__71e9bce111e9429c/microsoft.office.interop.word.dll
----------------------------------------
System.Windows.Forms
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase: file:///c:/winnt/assembly/gac/system.windows.forms/1.0.5000.0__b77a5c561934e089/system.windows.forms.dll
----------------------------------------
System
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase: file:///c:/winnt/assembly/gac/system/1.0.5000.0__b77a5c561934e089/system.dll
----------------------------------------
System.Drawing
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase: file:///c:/winnt/assembly/gac/system.drawing/1.0.5000.0__b03f5f7f11d50a3a/system.drawing.dll
----------------------------------------
office
Assembly Version: 11.0.0.0
Win32 Version: 11.0.5530
CodeBase: file:///c:/winnt/assembly/gac/office/11.0.0.0__71e9bce111e9429c/office.dll
----------------------------------------
CustomMarshalers
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///c:/winnt/assembly/gac/custommarshalers/1.0.5000.0__b03f5f7f11d50a3a/custommarshalers.dll
----------------------------------------
System.Xml
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase: file:///c:/winnt/assembly/gac/system.xml/1.0.5000.0__b77a5c561934e089/system.xml.dll
----------------------------------------
Accessibility
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.573
CodeBase: file:///c:/winnt/assembly/gac/accessibility/1.0.5000.0__b03f5f7f11d50a3a/accessibility.dll
----------------------------------------
 
P

Peter Huang [MSFT]

Hi Alex,

Here I assume that the exception occurred, when you are using the DSOFramer
, Word and Word Addin.
If so, have you tried from the base( i.e. an Addin did nothing), and then
add the code one by one.
NOTE: to call marshal.releaseComobject one by one.

If you still have any concern, please feel send a simple reproduce sample
addin for me to have a test.
You can reach me via removing the "online" from my email address.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Alex

Hi Peter,

"Peter Huang" said:
Hi

Here is link about an ActiveX Control host.
You may have a try to see if that control have problem when hosting word document.
Visual C++ ActiveX Control for hosting Office documents in Visual Basic or HTML
http://support.microsoft.com/?id=311765
I finally isolated the problem, but have no idea how to solve it.

It seems that if I hook any of Word's events, it will not be released when the host application is closed.

Sample code with hooking the Quit event:


public class Connect: Object, Extensibility.IDTExtensibility2
{
public Connect()
{
System.Diagnostics.Trace.WriteLine("*** Connect ***");
}

public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode,
object addInInst, ref System.Array custom)
{
System.Diagnostics.Trace.WriteLine("*** OnConnection ***");

applicationObject = (Word.Application)application;
addInInstance = addInInst;

if (connectMode != ext_ConnectMode.ext_cm_Startup)
OnStartupComplete(ref custom);
}

public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode,
ref System.Array custom)
{
System.Diagnostics.Trace.WriteLine("*** OnDisconnection ***");

if (disconnectMode != ext_DisconnectMode.ext_dm_HostShutdown)
OnBeginShutdown(ref custom);
}

public void OnStartupComplete(ref System.Array custom)
{
System.Diagnostics.Trace.WriteLine("*** OnStartupComplete ***");

// If the following lines are commented out, everything works
((Word.ApplicationEvents3_Event) applicationObject).Quit +=
new Word.ApplicationEvents3_QuitEventHandler(EventQuit);
}

public void OnBeginShutdown(ref System.Array custom)
{
System.Diagnostics.Trace.WriteLine("*** OnBeginShutdown ***");
}

public void OnAddInsUpdate(ref System.Array custom)
{
System.Diagnostics.Trace.WriteLine("*** OnAddInsUpdate ***");
}

private void EventQuit()
{
System.Diagnostics.Trace.WriteLine("*** EventQuit ***");
}

private Word.Application applicationObject;
private object addInInstance;
}


Unfortunately, I can find no way of releasing the events since neither of
EventQuit(), OnBeginShutdown() and OnDisconnection() gets called.

Please advise.


Best wishes,
Alex.
 
P

Peter Huang [MSFT]

Hi

Based on my test that host a word document in IE or in the DSOFramer
control, the OnBeginShutdown ,OnDisconnection ... method will be called if
we directly close the host application.

Here is the dbgview log.

00000000 0.00000000 [3864] OnConnection
00000001 0.00126232 [3864] OnAddInsUpdate
00000002 0.00877375 [3864] Microsoft Word On Connection
00000003 0.00883164 [3864] ConnectMode = 1
00000004 0.00888128 [3864] Custom(1) = 2
00000005 0.00892874 [3864] App Version = 11.0
00000006 0.00920941 [3864] gbIsValidLangSetting = True
00000007 0.01012617 [3864] gbBootLoad = True
00000008 0.01020831 [3864] OnAddInsUpdate
00000009 0.01028770 [3864] Microsoft Word On AddInsUpdate
00000010 0.08180493 [3864] Standard
00000011 0.10025600 [3864] Found Control Undo
00000012 0.10662775 [3864] Found Control Redo
00000013 0.10771541 [3864] Microsoft Word On StartupComplete
00000014 9.48894708 [3864] OnBeginShutdown
00000015 9.48907862 [3864] Microsoft Word On BeginShutdown
00000016 9.48948961 [3864] OnDisconnection
00000017 9.48962602 [3864] Microsoft Word On Disconnection
00000018 9.48967768 [3864] RemoveMode = 0
00000019 9.57271826 [3864] DLL_PROCESS_DETACH in MSGR3SC.DLL


Here is my test addin.

namespace MyWordAddin1
{
using System;
using Microsoft.Office.Core;
using Extensibility;
using System.Runtime.InteropServices;
using Word = Microsoft.Office.Interop.Word;
using System.Diagnostics;
[GuidAttribute("10F9EBA7-4332-4BA2-9C79-59AC13621BD6"),
ProgId("MyWordAddin1.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2
{
public Connect()
{
}

public void OnConnection(object application,
Extensibility.ext_ConnectMode connectMode, object addInInst, ref
System.Array custom)
{
Debug.WriteLine("OnConnection");
wdApp = application as Word.Application;
}

public void OnDisconnection(Extensibility.ext_DisconnectMode
disconnectMode, ref System.Array custom)
{
Debug.WriteLine("OnDisconnection");
}

public void OnAddInsUpdate(ref System.Array custom)
{
Debug.WriteLine("OnAddInsUpdate");
}

public void OnStartupComplete(ref System.Array custom)
{
Debug.WriteLine(wdApp.CommandBars[1].Name);

foreach(CommandBarControl cbc in wdApp.CommandBars[1].Controls)
{
if(cbc.Id == 128)
{
Debug.WriteLine("Found Control Undo");
cbcbUndo = cbc as CommandBarComboBox;
if (cbcbUndo!=null)
cbcbUndo.Change+=new
_CommandBarComboBoxEvents_ChangeEventHandler(cbcb_Change);
else
Debug.WriteLine("cbcbUndo is NULL");
}
if(cbc.Id == 129)
{
Debug.WriteLine("Found Control Redo");
cbcbRedo = cbc as CommandBarComboBox;
if (cbcbRedo!=null)
cbcbRedo.Change+=new
_CommandBarComboBoxEvents_ChangeEventHandler(cbcb_Change);
else
Debug.WriteLine("cbcbRedo is NULL");
}

}
}

public void OnBeginShutdown(ref System.Array custom)
{
Debug.WriteLine("OnBeginShutdown");
}
private Word.Application wdApp=null;
private CommandBarComboBox cbcbUndo=null;
private CommandBarComboBox cbcbRedo=null;

private void cbcb_Change(CommandBarComboBox Ctrl)
{
Debug.WriteLine("cbcb_Change");
}
}
}

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Alex O.

Hello Peter,

(posting from Google - different ID)

Peter Huang" said:
Hi

Based on my test that host a word document in IE or in the DSOFramer
control, the OnBeginShutdown ,OnDisconnection ... method will be called if
we directly close the host application.
When I hooked the Quit event, Word hung in DSOFramer and the methods
above were not called.

Did you try my example?
Your code is quite different from mine so it does not help me find the
problem.

Best wishes,
Alex.
 
P

Peter Huang [MSFT]

Hi Alex,

I can reproduce the issue.
Now I am researching the issue, and I will update you with new information
ASAP.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Huang [MSFT]

Hi

Based on my test, it seems that if we host a document in IE, the problem
did not occur.
So I think maybe the Active Document Host did not do it correctly.
So far we are still researching the issue and I will update you ASAP.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Huang [MSFT]

Hi

We are still researching the issue.
Thanks for your patience.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

C# Word Add-in 5
.net c# word add-in deployment 2
C++ .NET. COM Add-in for word 1
Debugging C# Word add-ins 6
Word Add in C++ .NET 2003 0
C++ Add-In for Visio 0
outlook c# add-in 0
com add-in in c# 0

Top