3

This is killing me.

I've read what SO posts I could find on the subject: here was the most relevant, but it didn't cover passing the delegate to an Event (though I'd have expected it would be straightforward).

Specifically, the error in VS is:

This function value is being used to construct a delegate type whose signature includes a byref argument. You must use an explicit lambda expression taking 2 arguments.

The best reasoning for the error I could find is the aside in Eric Lippert's blog, but I thought I had it handled below.

// Documentation: http://msdn.microsoft.com/en-us/library/aa752084(v=vs.85).aspx    
// Don't forget to unwrap the instance before using it.
let mutable ieInstance : option<InternetExplorer> = None

let onDocumentComplete (pDisp : Object) (url : byref<Object>) =
    let doc = ieInstance.Value.Document :?> IHTMLDocument2
    let window = doc.parentWindow
    window.execScript(@"alert('Message added by addon.');") |> ignore

// All of the following underline "DocumentComplete" as the source of the error.
do ieInstance.Value.DocumentComplete.AddHandler(new Handler<byref<Object>>(fun pDisp url -> onDocumentComplete pDisp &url))
do ieInstance.Value.DocumentComplete.AddHandler(fun pDisp url -> onDocumentComplete pDisp &url)
do ieInstance.Value.DocumentComplete.Add(fun pDisp url -> onDocumentComplete pDisp &url)
do ieInstance.Value.DocumentComplete.Add(fun _ _ -> ())
do ieInstance.Value.DocumentComplete.Add(fun (_, _) -> ())

I appreciate any suggestions!

Update 1 The library I'm referencing is Interop.SHDocVw, in 'Microsoft Internet Controls'. I also tried using SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler as the delegate type, still failing.

Community
  • 1
  • 1
dan p
  • 65
  • 4
  • What specific library are you referencing with that InternetExplorer class? – Ryan Lundy Jul 31 '14 at 17:34
  • Looks like some sort of COM interop class, but the documentation link does not say what. – Tahir Hassan Jul 31 '14 at 17:35
  • 1
    It's part of Interop.SHDocVw, in the Assembly "Microsoft Internet Controls". It can be cast to interfaces like [IWebBrowser2](http://msdn.microsoft.com/en-us/library/aa752127(v=vs.85).aspx) and of course [DWebBrowserEvents2](http://msdn.microsoft.com/en-us/library/aa768283(v=vs.85).aspx). – dan p Jul 31 '14 at 17:52
  • Note that you also need a reference to COM component "Microsoft HTML Object Library", and `open` statements for `mshtml`, `System`, and `SHDocVw` – latkin Jul 31 '14 at 19:11

1 Answers1

6

I get a slightly different error than you:

error FS1091: The event 'DocumentComplete' has a non-standard type. If this event is declared in another CLI language, you may need to access this event using the explicit add_DocumentComplete and remove_DocumentComplete methods for the event. If this event is declared in F#, make the type of the event an instantiation of either 'IDelegateEvent<_>' or 'IEvent<_,_>'.

If you follow that advice, you get to this, which compiles for me:

ieInstance.Value.add_DocumentComplete(fun pDisp url -> onDocumentComplete pDisp &url)

Edit

Keith and Tahir's comments made me realize - this scenario was indeed very difficult to diagnose, and in fact was fixed in February, shortly after the F# 3.1.1 release.

New behavior is the better error message (shown above) and better intellisense entries (the required add_ and remove_ members are now shown, and the useless DocumentComplete entry is actually now hidden). So if you are using released F# bits, sorry. If you are using latest prerelease F# bits, it's less painful.

The change was made before everything became open source on Codeplex, but some record of it can still be seen in a later merge to Github (see change to infos.fs and last change to nameres.fs).

latkin
  • 16,402
  • 1
  • 47
  • 62
  • 1
    I get the same error as dan p, though your suggested solution works anyway (even though `add_DocumentComplete` doesn't appear in IntelliSense). – kvb Jul 31 '14 at 19:43
  • 1
    In VS 2013, F# Intellisense does not list `add_DocumentComplete`. Although in VS 14 CTP I see it. Weird. – Tahir Hassan Jul 31 '14 at 19:52
  • I was going crazy with this same issue. I have VS 2010 and the intellisense didn't show add_DocumentComplete although it says it implements the Events2 interface. After a three hours wasted, glad I found this page. –  Jun 20 '17 at 16:49