Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InvokeMember give a different result than direct call

I'm calling Shell.BrowseForFolder in VB.NET because I need to pass an arbitrary path in the rootFolder argument. So I instanciate an objet like this:

Dim shellType As Type = Type.GetTypeFromProgID("Shell.Application")
Dim shell = Activator.CreateInstance(shellType)

Then I call:

Dim folder = shell.BrowseForFolder(0, message, &H241, rootFolder)

It does not work as expected (the root folder F: is not used)

Direct call

But if I use reflection with the same arguments:

Dim folder = shellType.InvokeMember("BrowseForFolder", _
  BindingFlags.InvokeMethod, Nothing, shell, New Object() {0, message, &H241, _
  rootFolder})

It works!

Reflection

But for me the 2 calls (InvokeMember and direct call) should produce similar results (the arguments are identical). What's happening?

Edit:

In fact, it works if I call ToString() or if I put a litteral:

Dim folder = shell.BrowseForFolder(0, message, &H241, rootFolder.ToString())

or

Dim folder = shell.BrowseForFolder(0, message, &H241, "F:")

But it doesn't work if rootFolder is an argument, eg:

Function BrowseForFolder(ByVal message As String, ByVal rootFolder As String) As String
    Dim shellType As Type = Type.GetTypeFromProgID("Shell.Application")
    Dim shell = Activator.CreateInstance(shellType)
    Dim folder = shell.BrowseForFolder(0, message, &H241, rootFolder)
    If folder Is Nothing Then
        Return ""
    End If
    Return folder.Self.Path
End Function
like image 568
Maxence Avatar asked Dec 07 '25 09:12

Maxence


1 Answers

only way for me to reproduce this issue under windows 7 64bits with vs 2012 is to have an invalid rootFolder like an empty string or crap data in that variable.

can you do a breakpoint on that line:

   Dim folder = shell.BrowseForFolder(0, message, &H241, rootFolder)

and check what is rootFolder ?

found a way try this;

  Dim folder = shell.BrowseForFolder(0, message, &H241, rootFolder.ToString())

my code:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim rootFolder As Object = "f:"
    Dim shellType As Type = Type.GetTypeFromProgID("Shell.Application")
    Dim shell = Activator.CreateInstance(shellType)
    Dim folder = shell.BrowseForFolder(0, "message", &H241, rootFolder.ToString())
End Sub
like image 149
Fredou Avatar answered Dec 10 '25 02:12

Fredou



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!