Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MS Office互操作-can';t发布对RCW的引用_C#_.net_Com_Office Interop - Fatal编程技术网

C# MS Office互操作-can';t发布对RCW的引用

C# MS Office互操作-can';t发布对RCW的引用,c#,.net,com,office-interop,C#,.net,Com,Office Interop,我正在应用程序中使用Microsoft.Office.Interop.PowerPoint控制演示文稿幻灯片的播放。我可以使用代码连接到PowerPoint PowerPointApp = Marshal.GetActiveObject("PowerPoint.Application") as Microsoft.Office.Interop.PowerPoint.Application; 所有的工作都正常,但我不能使用 此调用仍引发异常: A first chance exception o

我正在应用程序中使用Microsoft.Office.Interop.PowerPoint控制演示文稿幻灯片的播放。我可以使用代码连接到PowerPoint

PowerPointApp = Marshal.GetActiveObject("PowerPoint.Application") as Microsoft.Office.Interop.PowerPoint.Application;
所有的工作都正常,但我不能使用

此调用仍引发异常:

A first chance exception of type 'System.InvalidCastException' occurred in mscorlib.dll

Additional information: Unable to cast COM object of type 'System.__ComObject' to interface type 'System.Runtime.InteropServices.ComTypes.IConnectionPoint'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{B196B286-BAB4-101A-B69C-00AA00341D07}' failed due to the following error: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD)).
我尝试将这两个方法(GetActiveObject和FinalEleaseComObject)包装为:

为了确保从同一个线程调用这两个方法,我得到了相同的异常


我还尝试在PowerPoint仍在运行时释放引用-在这种情况下,对RCW的引用会毫无例外地释放(但我需要在关闭PowerPoint后释放这些引用)

使用ReleaseComObject而不是FinalReleaseComObject方法。该条规定如下:

用于在使用完Office/PowerPoint对象后释放该对象。然后在Visual Basic中将变量设置为Nothing(在C#中为null),以释放对对象的引用


它与Outlook相关,但同样的原则也适用于任何Office应用程序

谢谢,但这对我没有帮助-还是一样的问题。已经有无数关于这个的帖子了。
A first chance exception of type 'System.InvalidCastException' occurred in mscorlib.dll

Additional information: Unable to cast COM object of type 'System.__ComObject' to interface type 'System.Runtime.InteropServices.ComTypes.IConnectionPoint'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{B196B286-BAB4-101A-B69C-00AA00341D07}' failed due to the following error: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD)).
 App.Current.Dispatcher.Invoke((MethodInvoker)delegate{ ... });`