1

我有一个显示图像的应用程序,我们将它们移动到不同的服务器。在现有服务器上,他们可以查看任意数量的图像,并且不会要求他们再次登录。在新服务器上,即使浏览器控件有他们的凭据,它也会要求他们在查看图像之前登录。如果我将 Web 浏览器的 absoluteUri 属性的值粘贴到 IE 中,它会在不要求登录的情况下加载图像。

我正在尝试通过实现 IAuthenticate 将用户 ID 和密码凭据传递给表单上的 webBrowser 控件,并且我在网上找到的所有示例都在 C# 中。在试图缩小问题所在的范围时,我已经注释掉了 IAuthenticate 实现,因为它在调用实现之前就出现了错误。现在我有:

Imports System.Runtime.InteropServices

<ComImport(),
Guid("00000112-0000-0000-C000-000000000046"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
Public Interface IOleObject
    Sub SetClientSite(pClientSite As IOleClientSite)
    Sub GetClientSite(ppClientSite As IOleClientSite)
    Sub SetHostNames(szContainerApp As Object, szContainerObj As Object)
    Sub Close(dwSaveOption As UInteger)
    Sub SetMoniker(dwWhichMoniker As UInteger, pmk As Object)
    Sub GetMoniker(dwAssign As UInteger, dwWhichMoniker As UInteger, ppmk As Object)
    Sub InitFromData(pDataObject As IDataObject, fCreation As Boolean, dwReserved As UInteger)
    Sub GetClipboardData(dwReserved As UInteger, ppDataObject As IDataObject)
    Sub DoVerb(iVerb As UInteger, lpmsg As UInteger, pActiveSite As Object, lindex As UInteger, hwndParent As UInteger, lprcPosRect As UInteger)
    Sub EnumVerbs(ppEnumOleVerb As Object)
    Sub Update()
    Sub IsUpToDate()
    Sub GetUserClassID(pClsid As UInteger)
    Sub GetUserType(dwFormOfType As UInteger, pszUserType As UInteger)
    Sub SetExtent(dwDrawAspect As UInteger, psizel As UInteger)
    Sub GetExtent(dwDrawAspect As UInteger, psizel As UInteger)
    Sub Advise(pAdvSink As Object, pdwConnection As UInteger)
    Sub Unadvise(dwConnection As UInteger)
    Sub EnumAdvise(ppenumAdvise As Object)
    Sub GetMiscStatus(dwAspect As UInteger, pdwStatus As UInteger)
    Sub SetColorScheme(pLogpal As Object)
End Interface

<ComImport(),
Guid("00000118-0000-0000-C000-000000000046"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
Public Interface IOleClientSite
    Sub SaveObject()
    Sub GetMoniker(ByVal dwAssign As Integer, ByVal dwWhichMoniker As Integer, ByRef ppmk As Object)
    Sub GetContainer(ByRef ppContainer As Object)
    Sub ShowObject()
    Sub OnShowWindow(ByVal fShow As Boolean)
    Sub RequestNewObjectLayout()
End Interface

<ComImport(),
Guid("6d5140c1-7436-11ce-8034-00aa006009fa"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
Public Interface IServiceProvider
    <PreserveSig()>
    Sub QueryService(ByRef guidService As Guid, ByRef riid As Guid,
          <Out(), MarshalAs(UnmanagedType.Interface)> ByRef ppvObject As Object)
End Interface

<ComImport> _
<Guid("79EAC9D0-BAF9-11CE-8C82-00AA004BA90B")> _
<InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
Public Interface IAuthenticate
    '<MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)
Sub Authenticate(phwnd As IntPtr,
<MarshalAs(UnmanagedType.LPWStr)> ByRef pszUsername As String,
<MarshalAs(UnmanagedType.LPWStr)> ByRef pszPassword As String)
End Interface

Public Class frmIE
    Implements IOleClientSite
    Implements IServiceProvider

    Public Shared IID_IAuthenticate As New Guid("79eac9d0-baf9-11ce-8c82-00aa004ba90b")

    Private _user As String
    Private _pwd As String

    Friend WriteOnly Property url(user As String, pwd As String) As System.Uri
        Set(ByVal value As System.Uri)
            Dim oc As IOleObject

            webIE.Navigate("about:blank")       ' iAuthenticate not always called on first run
            oc = DirectCast(webIE.ActiveXInstance, IOleObject)
            ' if I comment the following line out, it loads the image ok and asks for credentials
            oc.SetClientSite(DirectCast(Me, IOleClientSite))   ' instead of trycast, so can see error: An unhandled exception of type 'System.ExecutionEngineException' occurred in System.Windows.Forms.dll

            _user = user
            _pwd = pwd
            webIE.Navigate(value)
        End Set
    End Property

    Public Sub GetContainer(ByRef ppContainer As Object) Implements IOleClientSite.GetContainer
        ppContainer = Me   ' doesn't reach stop placed here
    End Sub

    Public Sub GetMoniker(dwAssign As Integer, dwWhichMoniker As Integer, ByRef ppmk As Object) Implements IOleClientSite.GetMoniker
        Dim i As Integer
        i = 0           ' doesn't reach stop placed here
    End Sub
    Public Sub OnShowWindow(fShow As Boolean) Implements IOleClientSite.OnShowWindow
        Dim i As Integer
        i = 0          ' doesn't reach stop placed here
    End Sub
    Public Sub RequestNewObjectLayout() Implements IOleClientSite.RequestNewObjectLayout
        Dim i As Integer
        i = 0           ' doesn't reach stop placed here
    End Sub
    Public Sub SaveObject() Implements IOleClientSite.SaveObject
        Dim i As Integer
        i = 0          ' doesn't reach stop placed here
    End Sub
    Public Sub ShowObject() Implements IOleClientSite.ShowObject
        Dim i As Integer
        i = 0         ' doesn't reach stop placed here
    End Sub


    Public Sub QueryService(ByRef guidService As System.Guid, ByRef riid As System.Guid,
                             <Out(), MarshalAs(UnmanagedType.Interface)> ByRef ppvObject As Object) Implements IServiceProvider.QueryService
        If guidService.CompareTo(IID_IAuthenticate) = 0 AndAlso riid.CompareTo(IID_IAuthenticate) = 0 Then
            'ppvObject = Marshal.GetComInterfaceForObject(Me, GetType(IAuthenticate))   ' doesn't reach stop placed here
        Else
            ppvObject = IntPtr.Zero        ' does reach stop placed here, 2x
            ' 1st guid is 4c96be40-915c-11cf-99d3-00aa004ae837 = SID_SToplevelBrowser 
            ' 1st riid is 02ba3b52-0547-11d1-b833-00c04fc9b31f
            ' 2nd guid is SID_SToplevelBrowser
            ' 2nd riid is 6d5140c1-7436-11ce-8034-00aa006009fa
        End If
    End Sub
End Class

但它给了我一个在线错误

oc.SetClientSite(DirectCast(Me, IOleClientSite)) 

说“System.Windows.Forms.dll 中发生'System.ExecutionEngineException' 类型的未处理异常”这看起来像是一个可能有很多原因的一般错误。

我假设我错过了一些声明或使用细节,但我不知道在哪里或者这是否真的是问题所在。

所有帮助表示赞赏,

-贝丝

4

1 回答 1

0

我们最终向 webBrowser 对象的 navigate 方法传递了一个字符串,而不是一个新的 system.uri 对象,这为我们修复了它。

于 2013-02-27T21:40:32.663 回答