Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Late Binding & Type Issues In VB

I'm trying to run my code which was originally created using Visual Studio through another application where late bindings are disallowed and this option cannot be altered unfortunately. I am very new to programming in general and struggling to get my head around the issue. Here is the code im using in the invoke code stage:

Dim objIEShell As Object = CreateObject("Shell.Application")
Dim objIEShellWindows As Object = objIEShell.Windows
Dim objIEWin As Object
For Each objIEWin In objIEShellWindows
    If InStr(objIEWin.LocationURL,"google")>0 Then
        objIEWin.Quit
        objIEWin = Nothing
    End If
Next

The code simply closes all instances of Internet Explorer with "google" in the URL. This is the error message I get when trying to compile it:

Message: Error compiling code
error BC30574: Option Strict On disallows late binding. At line 2
error BC32023: Expression is of type 'Object', which is not a collection type. At line 4

From the research I've done so far I realise the first error message on line 2 is to do with the type difference between objIEShell and the Windows method. I think I have to convert objIEShell like this, CType(objIEShell,?), but I don't know the type of the .Windows method or how to find this out. Also any insight on how the fix the second error would be greatly appreciated as I'm not sure where to start with that one either.

like image 842
unknownpresense Avatar asked Nov 16 '17 00:11

unknownpresense


People also ask

What are examples of late binding?

Late binding occurs when we make implicit or indirect function calls in our program. An example of this is using function pointers or virtual functions when using classes. Here, the function call's memory reference is not determined at compile-time, but rather at run-time.

What is late binding and early binding?

The process of using the class information to resolve method calling that occurs at compile time is called Early Binding. The process of using the object to resolve method calling that occurs at run time is called the Late Binding.

What is late binding data?

Late binding is a runtime process of looking up a declaration, by name, that corresponds to a uniquely specified type. It does not involve type checking during compilation, when referencing libraries, including an object, is not required.

What is meant by early binding?

What Does Early Binding Mean? In C#, early binding is a process in which a variable is assigned to a specific type of object during its declaration to create an early-bound object. This contrasts the late-bound object process, where an object type is revealed at the time of instantiation.


1 Answers

This dates back to the wonky days when Microsoft still had plans to make Explorer behave like a web browser. Makes it pretty hard to arrive at the correct code, it is a combination of two separate COM components that don't have much to do with each other.

You need to first add two references to those components so the compiler understands the names. Use Project > Add Reference > COM tab and tick "Microsoft Internet Controls" and "Microsoft Shell Controls and Automation". That adds the Shell32 and SHDocVw namespaces.

Now you can write the code early-bound like this:

    Dim objIEShell = New Shell32.Shell
    Dim objIEShellWindows = CType(objIEShell.Windows, SHDocVw.IShellWindows)
    Dim objIEWin As SHDocVw.WebBrowser
    For Each objIEWin In objIEShellWindows
        If InStr(objIEWin.LocationURL, "google") > 0 Then
            objIEWin.Quit()
        End If
    Next

The CType() expression is probably the most unintuitive one, the Shell.Windows property is of type Object to break the dependency between those two components. The cast is the necessary voodoo to keep the compiler happy.

like image 127
Hans Passant Avatar answered Sep 24 '22 23:09

Hans Passant