Shell Extension in C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lushdog

    #1

    Shell Extension in C#

    Hi, i'm writing a shell extension in c# that will add two menu's to the
    right-click menu of explorer if any file is selected, i.e. it's
    registered in the * section of Classes in the registry.

    Here is the problem. If i select one file and use the standard OPEN
    context menu, for a .txt file let's say, all is well, but if i select
    two or more files and use the standard OPEN context menu it seems to
    want to run through the code snippet for MY created menus. Here is the
    code in question for my created menus:

    int IContextMenu.Qu eryContextMenu( uint hmenu,uint iMenu, int
    idCmdFirst,int idCmdLast,uint uFlags)
    {
    // The first id to use (should be 1)
    int id = 1;
    if ((uFlags & 0xf) == 0 || (uFlags & (uint)CMF.CMF_E XPLORE) !=
    0)
    {
    // Create a new Menu Item to add to the context menu
    MENUITEMINFO mii = new MENUITEMINFO();
    mii.cbSize = 48;
    mii.fMask = (uint)MIIM.ID | (uint)MIIM.TYPE |
    (uint)MIIM.STAT E;
    mii.wID = idCmdFirst + id;
    mii.fType = (uint)MF.STRING ;
    mii.dwTypeData = "Send file(s) to CheapFTP member(s)";
    mii.fState = (uint)MF.ENABLE D;
    // Add it to the item
    DllImports.Inse rtMenuItem(hmen u, (uint)2, 1, ref mii);

    id = 2;
    // Create a new Menu Item to add to the context menu
    MENUITEMINFO miii = new MENUITEMINFO();
    miii.cbSize = 48;
    miii.fMask = (uint)MIIM.ID | (uint)MIIM.TYPE |
    (uint)MIIM.STAT E;
    miii.wID = idCmdFirst + id;
    miii.fType = (uint)MF.STRING ;
    miii.dwTypeData = "Quick Send file(s) to CheapFTP preferred
    member(s)";
    miii.fState = (uint)MF.ENABLE D;
    // Add it to the item
    DllImports.Inse rtMenuItem(hmen u, (uint)2, 1, ref miii);

    id = 3;
    // Create a seperator
    MENUITEMINFO sep = new MENUITEMINFO();
    sep.cbSize = 48;
    sep.fMask = (uint)MIIM.TYPE ;
    sep.wID = idCmdFirst + id;
    sep.fType = (uint)MF.SEPARA TOR;
    // Add it to the seperator to the context menu
    DllImports.Inse rtMenuItem(hmen u, (uint)2, 1, ref sep);
    }
    // Return the number of items added
    return id;
    }
    void IContextMenu.In vokeCommand(Int Ptr pici)
    {
    try
    {
    //fileList
    StringBuilder fileList = new StringBuilder() ;
    StringBuilder sb = new StringBuilder(1 024);
    uint i = 0;
    //Get number of files
    uint uNumFiles = 0;
    uNumFiles = DllImports.Drag QueryFile(m_hDr op, 0xFFFFFFFF, null,
    0);
    //make file name
    while (i < uNumFiles)
    {
    DllImports.Drag QueryFile(m_hDr op, i, sb, sb.Capacity + 1);
    fileList.Append (sb + ",");
    i++;
    }
    // Determine which of the context menus was clicked
    Type t = Type.GetType("S hellExt.INVOKEC OMMANDINFO");
    INVOKECOMMANDIN FO ici;
    ici = (INVOKECOMMANDI NFO)Marshal.Ptr ToStructure(pic i, t);
    switch (ici.verb - 1)
    {
    case 0:
    // Send File(s) to CheapFTP client
    Process.Start(" c:\\CheapFTP.ex e", "\"" +
    fileList.ToStri ng() + "\"");
    break;
    case 1:
    // Send File(s) to CheapFTP client with QuickSend Flag
    enabled
    fileList.Append ("QuickSend, ");
    Process.Start(" c:\\CheapFTP.ex e", "\"" +
    fileList.ToStri ng() + "\"");
    break;
    default:
    MessageBox.Show ("Shell Error");
    break;
    }
    }
    catch(Exception e)
    {
    System.Windows. Forms.MessageBo x.Show("Error : " + e.ToString(),

    "Error in CheapFTP installation. Please re-install.");
    }
    }

    If i use my created menus everything works as planned, and so do all
    the other menus i have installed, i.e. WinRar and NOD32 Antivirus. If i
    select one file and use the standard OPEN menu it works correctly.
    Only when i select multiple files and select OPEN, i notice that it
    runs through the code for MY menu, i.e. the MessageBox "Shell Error"
    will pop up as often as the number of files i have selected. It seems
    that if one file is selected and it has an OPEN static command in the
    registry "notepad.ex e %1" it runs that, but if i have two selected does
    it run a different command in the registry?

    It is my understanding that if i select the OPEN menu it should not
    even run through my
    code at all. If I uninstall my menu, selecting multiple files+OPEN
    works properly even with the WinRar context menus installed. The only
    difference i can see is that WinRAR's is a .dll probably made in
    unhandled c++ where mine is made in c# .NET.

    ideas/suggestions? TIA,lushdog

Working...