64 bit add-ins

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

64 bit add-ins
Should I build both a 32 bit and 64 bit version of my Add-In? 
dan gallery




Posts: 83
Joined: 2009-10-19
Hi guys,

One of my users on a new 64 bit machine is frequently getting the message:

HRESULT: 0x80010100 (RPC_E_SYS_CALL_FAILED)

during a call I make:

----- loop -------
Thread thread = new Thread(new ParameterizedThreadStart(AccessClipboardThreadAndWriteOutEmfFile));

thread.SetApartmentState(ApartmentState.STA);
thread.Start(stuff);
...
----end loop --------

the line in AccessClipboardThreadAndWriteOutEmfFile() that is failing

Excel.Application excelApp = (Excel.Application) System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");


The call to AccessClipboardThreadAndWriteOutEmfFile fails only some of the time. The function takes a snapshot to clipboard of named ranges in Excel, and writes out the clipboard as a file, so sometimes it is slower than others, depending perhaps on the file size.

The system seems to have trouble getting the active excel app. Though I think this may be misleading. I am looping through the workbook and making a call in each iteration to the above. Perhaps the last call to this thread did not get a chance to complete?

I was wondering if this could be related to my Build, Configuration Manager being set to AnyCpu. Should it be x86?

Also, I tried to create an x64 version of my add on and got the message:

Error 133 File "C:\Users\DPO\Documents\Visual Studio 2008\Projects\DatappraiseOffice\DatappraiseOffice\bin\x64\Debug\DatappraiseOffice.dll" is not a valid assembly. DatappraiseOffice

Do I need to upgrade to Visual Studio 2010 and Add-In Express 2010 to build a 64 bit Add-on? Will the 64 bit add-on run under Office 2007? From another forum thread I read it seems I need to upgrade to .Net Framework 4.0 if I go with Add-In Express 2010. Is that true? If so, will that still support Word/Excel 2007 users?

In general, should I be building 2 versions of my add-in? One for x86, and one x64, i.e. for users on Office 64 bit?

Thanks!
Dan
Posted 07 Mar, 2013 20:36:18 Top
dan gallery




Posts: 83
Joined: 2009-10-19
Hi guys,

An upd ate to this question. I don't think it has to do with file size of the EMF that is created from the clipboard. I now see a definite pattern on my clients machine. If I open Excel first, and run my function from the Add-In, it always works fine. But if I open Word or Outlook first, then open Excel and run my function, it always fails. And it always fails after 26 successful iterations. This is weird. Its like Word is blocking/restricting the number of available threads or something. I know this is obscure, but any thoughts would be greatly appreciated.

// calling code - looks about 50 times, collecting named ranges in the workbook
bool moreNamedRanges = true;
while (moreNamedRanges) {

Thread thread = new Thread(new ParameterizedThreadStart(AccessClipboardThreadAndWriteOutEmfFile));
thread.SetApartmentState(ApartmentState.STA);
thread.Start(stuff);

Thread.Sl eep(400);
thread.Join();

...
// eventually moreNamedRanges is se t to false
...

}

private void AccessClipboardThreadAndWriteOutEmfFile(object someInfo) {

Excel.Application activeExcelInstance = null;
try
{
// this works fine 26 times
activeExcelInstance = Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");

}catch (Exception e) {
// on the 27th time this exception excecuted, it fails with the HRESULT: 0x80010100 (RPC_E_SYS_CALL_FAILED) error
}
Posted 09 Mar, 2013 19:23:20 Top
Andrei Smolin


Add-in Express team


Posts: 14106
Joined: 2006-05-11
Hi Dan,

The object models of all Office applications are not thread-safe. This is why you need to access the Excel object model in the main thread. Please find possible solutions in On using threads in managed Office extensions.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 11 Mar, 2013 05:25:44 Top
dan gallery




Posts: 83
Joined: 2009-10-19
Hi Andrei,

Doesn't the thread.Join(); statment add it to the main thread?

I've tried running the app in the main thread. The problem is, when I use the statement

Excel.Application activeExcelInstance =(Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");

This leaves a lingering instance of Excel in Task manager after I close the application, which then messes up the Add-In on subsequent runs.

Thanks,
Dan
Posted 12 Mar, 2013 14:02:03 Top
Andrei Smolin


Add-in Express team


Posts: 14106
Joined: 2006-05-11
dan gallery writes:
Doesn't the thread.Join(); statment add it to the main thread?


No, please see http://msdn.microsoft.com/en-us/library/95hbf2ta.aspx.

dan gallery writes:
This leaves a lingering instance of Excel in Task manager after I close the application


You may also want to check Why doesn’t Excel quit?

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 13 Mar, 2013 06:47:06 Top
dan gallery




Posts: 83
Joined: 2009-10-19
Thank you Andrei, excellent articles. Also, I used your advice that office apps aren't thread safe, and simply moved my code out of the thread call and into the main body of my loop, keeping it in the main thread. It worked perfectly. I dont know why i was spawning a thread, i think its because i copied the code from a forum that recommended it due to Clipboard being a shared object.

Anyway, doesnt matter, your comments gave me the clue i needed to fix it, thank you so much! You guys are the best.

Dan
Posted 13 Mar, 2013 16:18:44 Top
Andrei Smolin


Add-in Express team


Posts: 14106
Joined: 2006-05-11
Thank you!

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 14 Mar, 2013 05:24:25 Top