0

System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'Shell32.Shell'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{34936BA1-67AD-4C41-99B8-8C12DFF1E974}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

at DisGruntled.Copy_Replace.CopyReplace(Object param) in F:\Copy Replace.vb:line 31

at DisGruntled.Copy_Replace.CopyReplaceFromMTAThread(String Path_copy, String Path_Replace) in F:\Copy Replace.vb:line 70

at DisGruntled.Form3.btnStart_Click(Object sender, EventArgs e) in F:\Form3.vb:line 442

Here Is my Codes in Module(Copy_Replace).vb.vb:

Imports System.Threading
Imports Shell32

Module Copy_Replace

Dim objFolder As Folder
    Dim objShell As Shell
    Public Sub CopyReplace(ByVal param As Object)
        Dim args As Object() = CType(param, Object())
        Dim Path_copy As String = CStr(args(0))
        Dim Path_Replace As String = CStr(args(1))

        objShell = New Shell                                        ' line 31  (Which has Error) ??????

        '1
        objFolder = objShell.NameSpace(Path_Replace)

        If (Not objFolder Is Nothing) Then

            objFolder.CopyHere(Path_copy, 4 + 16)
        End If

        objFolder = Nothing
        objShell = Nothing
    End Sub
 Public Sub CopyReplace(ByVal Path_copy As String, ByVal Path_Replace As String)
        Dim args As Object() = New Object() {Path_copy, Path_Replace}

        If Thread.CurrentThread.GetApartmentState() = ApartmentState.STA Then
            'UnZip(args)
            CopyReplace(args)                                       ' line 70  (Which has Error) ??????
        Else
            Dim staThread1 As Thread = New Thread(New ParameterizedThreadStart(AddressOf CopyReplace))
            staThread1.SetApartmentState(ApartmentState.STA)
            staThread1.Start(args)
            staThread1.Join()
        End If
    End Sub
End Module  

I picked this answer from here and edited it to the code which you can see in my Module(Copy_Replace).vb.

Here is my Button_click code: (in form Class)

Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
        CopyReplaceFromMTAThread(X_path, O_path)  ' line 442  (Which has Error) ??????
End Sub
  • 1
    Why not just use the built in file/management in System.IO? Am I not seeing some complication? – Hursey May 12 '20 at 21:17
  • Because i download The File from The Internet and replace it with Existed One and it succeded in Windows 7 and Failed With Windows 10 – Eslam Alawy May 12 '20 at 21:19
  • Ok, probably could of mentioned that. Anyway, from experience a couple things to check. The bitness of your app in win 10 32/64 etc. Next is the .net framework. I've had issues with COM objects in .NetCore. Identical code worked fine in .net framework – Hursey May 12 '20 at 21:25
  • My Program Running In `.Net Framework 4.7.2` And Debug `Any CPU` – Eslam Alawy May 12 '20 at 21:30
  • Getting back to the original comment though, why couldn't you use the built in System.IO file management? Potentially save yourself a fair amount of headaches using the build in tools rather than reaching out to 3rd party com objects? – Hursey May 12 '20 at 21:34
  • because i don`t need the user know that there is a replacement operations is just done by this program and make this operations silent – Eslam Alawy May 12 '20 at 21:37
  • If you have embedded the Interop types of `Microsoft Shell Controls And Automation` in Windows 7, it won't work in Windows 10. You may have better results with `Activator.CreateInstance()`. Or declare yourself the COM interfaces (preferable, IMO). Not clear what's the scenario here, where this MTA Thread comes from and why you need the Shell to copy files (to use the pre-built copy/replace dialog? Maybe you can just build your own) – Jimi May 12 '20 at 21:38
  • @Jimi please write your idea as a code in an answer , yes i need it just do copy/replace in silent mode – Eslam Alawy May 12 '20 at 21:41
  • Think you may want to read up on system.io. File operations can be done quite simply without any user interaction. Or as @Jimi suggested update your question with the full scenario – Hursey May 12 '20 at 21:42
  • @Hursey i need the codes of the @jimi idea and i`ll check it – Eslam Alawy May 12 '20 at 21:44
  • @Jimi I just picked this MTA Thread from another Question only and i tried it but failed with win 10 – Eslam Alawy May 12 '20 at 21:48
  • It should fail, that is not a valid IID. Very hard to guess where it came from, there is exactly *one* other programmer that had the same problem you did, their question did not provide any cues either. Use another machine or use the File.Replace() overload with 3 arguments. – Hans Passant May 12 '20 at 21:49
  • @HansPassant my program download a zip file and extract it successfully and take this file and replace the existed one with it i use this Function `CopyReplaceFromMTAThread` to copy and replace it successfully worked with win7 but failed with win10, how can we solve this .! – Eslam Alawy May 12 '20 at 21:52
  • @HansPassant `Replace(,,)` Failed because i replace this file when it used by another proccess so the successfull way is to use Shell32 it successfully do the work but i need it work on other operation systems like win10 , 8 ,8.1 – Eslam Alawy May 12 '20 at 22:18
  • @Jimi Can you tell me the idea of using `Activator.CreateInstance()` with my codes ? Write some codes please ! use mine and edit on it – Eslam Alawy May 12 '20 at 23:40
  • @HansPassant There is no MTAthread , it just a name the following codes to make the operation done in STAthread Way . – Eslam Alawy May 12 '20 at 23:51
  • @HansPassant so What Can we Do and how to Use `Activator.CreateInstance()` ! use my codes – Eslam Alawy May 12 '20 at 23:52

0 Answers0