4

I create this menu using code placed by Sertac Akyuz in answer here: Show the default right-click menu - Delphi and it works good, but there is problem: When I click in created menu: delete (delete file to recycled) - it asks me: Are you sure want to delete?

When i click Yes - it works ok, but when I click no - it shows me an error, I see in debugger, error is on line:

OleCheck(ContextMenu.InvokeCommand(CommandInfo));

Error: ###(gdb unparsed remainder:s 0x0 out of bounds>)###.

I use Lazarus, but i think, that in Delphi it is actual too.

It seems to me, that the menu (windows) try to return to my program the answer - No, and in this situation error occurs.

How to solve this problem? How correct solve this situation with answer 'No'?

Community
  • 1
  • 1
user2154246
  • 55
  • 1
  • 6
  • Please don't tag a debug-runtime-issue-in-lazarus as delphi. It's confusing. If you're asking a debug-issue question pick one tool please. – Warren P Mar 16 '13 at 21:39
  • 1
    Try to dig implementation of it in the [Double Commander](http://doublecmd.sourceforge.net/) sources: `svn co https://doublecmd.svn.sourceforge.net/svnroot/doublecmd doublecmd`.
    I not sure, but may be answer can be found in this file: `doublecmd\trunk\src\platform\win\ushellcontextmenu.pas`
    – Abelisto Mar 16 '13 at 20:10

1 Answers1

3

You haven't specified the error number, but when I tried to duplicate the steps, the OleCheck call failed with 0x80270000. The high word part, save the error bit, is 0x27 (39), that's FACILTY_SHELL as defined in 'winerror.h'. As you can see, the low word is '0', the shell does not give any specific error code, in fact the code is identical with ERROR_SUCCESS or NO_ERROR.

My interpretation is, the shell is just notifying that the command (the delete operation) failed. The fail is due to the user canceling the operation. My suggestion is, modify the code accordingly as you see fit. You have the knowledge that the operation failed, but you may choose to ignore it, or perhaps notify the user. Maybe something like this:

var
  ...
  InvokeResult: HRESULT;
begin

  ...
//      OleCheck(ContextMenu.InvokeCommand(CommandInfo));
      InvokeResult := ContextMenu.InvokeCommand(CommandInfo);
      if not Succeeded(InvokeResult) then begin
        if LoWord(InvokeResult) = NO_ERROR then
          ShowMessage('Command did not carried out')
        else
          OleError(InvokeResult);
      end;
    ...
Sertac Akyuz
  • 54,131
  • 4
  • 102
  • 169
  • But if i don't want to analyze the result, may i do: try OleCheck(ContextMenu.InvokeCommand(CommandInfo)); except end; or simply: InvokeResult := ContextMenu.InvokeCommand(CommandInfo); without analyzing the result. – user2154246 Mar 17 '13 at 05:15
  • Your code is correct - there is no menu item: rename. In my app i don't need this item, but theoretically: how we may modify the code to show the item: rename and to handle the click on this item and to show the special form, where user can rename. – user2154246 Mar 17 '13 at 05:36
  • @user - To your second comment. Yes, the *simply* part would be the way to go, if you don't want to check the result, don't call OleCheck. To the 3rd comment - normally you'd be writing a shell extension, see [this question](http://stackoverflow.com/questions/3797817/how-to-add-item-to-windows-explorer-content-menu-in-delphi). I have no idea if it should be possible to intervene with the menu once it is shown, ask a question if you like.   And, you're welcome. :) – Sertac Akyuz Mar 17 '13 at 09:55
  • I mean in standard explorer menu - there is item Rename, and i check some other variants of emplementation of creating this menu and there was item Rename. – user2154246 Mar 17 '13 at 12:03
  • @user - If you're asking how to replace the standard 'rename' item, I'd think it to be not possible - but again asking it in its own different question might be an idea. Otherwise, despite my previous comment, I think you could add your item in your menu before calling TrackPopupMenu and then handle it in the overriden WndProc. – Sertac Akyuz Mar 17 '13 at 13:19
  • Thanks again, 1) Note: I mean, here: http://www.freepascal.ru/forum/viewtopic.php?f=5&t=3300 is code with this item rename and there no some special adding of this item. What it the differences between your code and this variant - i don't know, i didn't do the analysis. 2) Theoretical question: Can we somehow delete items from the created menu, e.x. delete item 'delete'? – user2154246 Mar 20 '13 at 11:52
  • 1
    I gess i understood: reneme item appears if use flag CMF_CANRENAME in QueryContextMenu command. – user2154246 Apr 12 '13 at 17:18