none
Shell Extension breaks RRS feed

  • Question

  • Hello to All

    I have written a shell extension on .NET 2.0

    My shell extension breaks after when CreateViewObject() has finished his work

    I can't go to CreateViewWindow() because "Access violation" is occured after CreateViewObject()

    Implementation of CreateViewObject()

    public long CreateViewObject(IntPtr hwndOwner, [In()] ref Guid riid, [Out(), MarshalAs(UnmanagedType.Interface)] out object ppv)
    {          
                ShellViewImpl loShellViewImpl = new ShellViewImpl();
                loShellViewImpl.Init(this, m_oCurrentPidl);

                ppv = loShellViewImpl;
             

                return ShellWrapper.S_OK;
    }

    Declaration of CreateViewWindow()

    [PreserveSig]
            long CreateViewWindow([In(), MarshalAs(UnmanagedType.Interface)] IntPtr psvPrevious,
                                    [In()] IntPtr pfs,
                                    [In(), MarshalAs(UnmanagedType.Interface)] IntPtr psb,
                                    [In()] IntPtr prcView,
                                    [Out()] out IntPtr phWnd);

    Help me, please
    Sunday, June 1, 2008 11:58 AM

Answers

  • The "riid" argument is passed by value, not by reference.  The return type (HRESULT) is int, not long.  Declaring ppv as out object doesn't seem right, declare as out IntPtr and use Marshal.GetFunctionPointerForDelegate() to assign it.  Be sure to keep the delegate instance alive by assigning it to a class field so the garbage collector won't delete it.

    Last but not least, writing shell extension handlers in .NET is *not* recommended.  It will inject the CLR into a process that doesn't expect it when it uses a shell dialog like OpenFileDialog.  Or your extension may fail when the process has .NET 1.1 loaded, possibly crashing the application.

    Hans Passant.
    • Marked as answer by Bruno Yu Friday, June 6, 2008 5:50 AM
    • Marked as answer by Bruno Yu Friday, June 6, 2008 5:50 AM
    Sunday, June 1, 2008 4:44 PM
    Moderator

All replies

  • The "riid" argument is passed by value, not by reference.  The return type (HRESULT) is int, not long.  Declaring ppv as out object doesn't seem right, declare as out IntPtr and use Marshal.GetFunctionPointerForDelegate() to assign it.  Be sure to keep the delegate instance alive by assigning it to a class field so the garbage collector won't delete it.

    Last but not least, writing shell extension handlers in .NET is *not* recommended.  It will inject the CLR into a process that doesn't expect it when it uses a shell dialog like OpenFileDialog.  Or your extension may fail when the process has .NET 1.1 loaded, possibly crashing the application.

    Hans Passant.
    • Marked as answer by Bruno Yu Friday, June 6, 2008 5:50 AM
    • Marked as answer by Bruno Yu Friday, June 6, 2008 5:50 AM
    Sunday, June 1, 2008 4:44 PM
    Moderator
  • IShellFolder::CreateViewObject(HWND hwndOwner, REFIID riid, VOID** ppvOut ) REFIID - is reference to IID, isn' it?

    Can I use Marshal.GetComInterfaceForObject() instead of Marshal.GetFunctionPointerForDelegate()?


    Sunday, June 1, 2008 8:16 PM
  • Right.  Reference-to, not pointer-to.  Marshal.GetComInterfaceForObject() call QueryInterface() on the object.  That might work, I have no idea what "ShellViewImpl" means in your code snippet.
    Hans Passant.
    Sunday, June 1, 2008 9:22 PM
    Moderator
  • I have changed old signature of CreateViewObject() to

    public long CreateViewObject(IntPtr hwndOwner, ref Guid riid, out IntPtr ppv)
            {          
                ShellViewImpl loShellViewImpl = new ShellViewImpl();
                loShellViewImpl.Init(this, m_oCurrentPidl);

                if (riid == typeof(IShellView).GUID)
                {
                    ppv = Marshal.GetComInterfaceForObject(loShellViewImpl, typeof (IShellView));
                    Marshal.AddRef(ppv);             
                }
                else
                {
                    ppv = IntPtr.Zerol;
                    return ShellWrapper.E_NOINTERFACE;
                }

                return ShellWrapper.S_OK;
            }

    ShellViewImpl - is implementation of IShellView

    Exception is occured I can't go to IShellView::CreateViewWindow()

    I don't know what can I do for executing of IShellView::CreateViewWindow()?
    Sunday, June 1, 2008 10:36 PM
  • Hmya, that's what happens when somebody tells you that you did three things wrong.  You fix one problem and it still doesn't work.  Good luck with it, I'm sure you'll figure it out, eventually.
    Hans Passant.
    Sunday, June 1, 2008 11:46 PM
    Moderator
  • You told me that I have done a three mistakes. One mistake was fixed. What mistakes have not yet fixed?
    Monday, June 2, 2008 11:09 AM
  • I can't outdo this problem help me
    Tuesday, June 3, 2008 10:55 PM