В очередно раз - Сom object that has been separated from its
От: senglory  
Дата: 09.11.10 18:50
Оценка:
Вот мой код:

Imports System.Reflection
Imports System.Runtime
Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop.Excel



Module Module1

    ' taken from http://www.codeproject.com/Messages/2893170/com-object-that-has-been-separated-from-its-underl.aspx
    Class RCWTest
        <DllImport("ole32.dll")> _
        Private Shared Function GetRunningObjectTable(ByVal reserved As Int32, ByRef pprot As ComTypes.IRunningObjectTable) As Integer
        End Function
        <DllImport("ole32.dll")> _
        Private Shared Function CreateBindCtx(ByVal reserved As Int32, ByRef pctx As ComTypes.IBindCtx) As Integer
        End Function

        '**Code for GetACtiveWorkbook function. I get error on the function GetDisplayName**

        Public Function GetActiveWorkbook(ByVal xlfile As String) As Workbook
            Dim prot As ComTypes.IRunningObjectTable = Nothing
            Dim pmonkenum As ComTypes.IEnumMoniker = Nothing
            Try
                Dim pfetched As IntPtr = IntPtr.Zero
                ' Query the running object table (ROT) 
                If GetRunningObjectTable(0, prot) <> 0 OrElse prot Is Nothing Then
                    Return Nothing
                End If
                prot.EnumRunning(pmonkenum)
                pmonkenum.Reset()
                Dim monikers As ComTypes.IMoniker() = New ComTypes.IMoniker(0) {}
                While pmonkenum.Next(1, monikers, 0) = 0
                    Dim pctx As ComTypes.IBindCtx
                    Dim filepathname As String
                    ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                    ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                    ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                    CreateBindCtx(0, pctx)
                    ' Get the name of the file 
                    monikers(0).GetDisplayName(pctx, Nothing, filepathname) ' Clean up 
                    Marshal.ReleaseComObject(pctx)
                    ' Search for the workbook 
                    If filepathname.IndexOf(xlfile) <> -1 Then
                        Dim roval As Object
                        ' Get a handle on the workbook 
                        prot.GetObject(monikers(0), roval)
                        roval = CType(roval, Workbook)
                        Return roval
                    End If
                End While
            Catch ex As COMException
                Throw ex
            Catch ex As Exception
                Throw ex
            Finally
                ' Clean up 
                If Not prot Is Nothing Then
                    Marshal.ReleaseComObject(prot)
                End If
                If Not pmonkenum Is Nothing Then
                    Marshal.ReleaseComObject(pmonkenum)
                End If
            End Try
            'Return Nothing
        End Function


    End Class

    Sub Main()
        Dim o As RCWTest = New RCWTest()
        Dim wm As Workbook = o.GetActiveWorkbook("C:\M-OwnershipCost.xls")

        Console.ReadKey()
    End Sub

End Module


На жирной строке при втором заходе выдает:


COM object that has been separated from its underlying RCW cannot be used



Изеачально, код был взят отсюда. Компиляю под .NET 2.0 — .NET 4.0, эффект одинаковый.

В чем причина и как лечить?
com interop rcw excel
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.