CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Apr 2004
    Posts
    2

    Get filename from context menu

    Please help me to find how can I get filename from windows explorer context menu and pass it to my own dialog based application?
    Thanks in advance

  2. #2
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    1,384

    Re: Get filename from context menu

    Have a look at Shell Extension.

    Regards,
    Usman.

  3. #3
    Join Date
    Apr 2004
    Posts
    2

    Re: Get filename from context menu

    Thank you but are there any other methods beside COM to catch and pass file name to the custom app ?

  4. #4
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    1,384

    Re: Get filename from context menu

    May be by hooking, but I didnt try it myself. Someone else here might be able to help you with that.

    Regards,
    Usman.

  5. #5
    Join Date
    Feb 2005
    Location
    Madrid (Spain)
    Posts
    511

    Re: Get filename from context menu

    I try to do this obtaining the title of the explorer and searching selected file but crasshes:

    Code:
    void myDlg::OnButtonSearch() 
    {
      char szWindow[1000];
      EnumWindows(SearchWin,(LPARAM)szWindow);
    }
    
    BOOL CALLBACK SearchWin(HWND hWnd,LPARAM lpParam) {    
      SendMessage(hWnd, WM_GETTEXT, 1000, (LPARAM) lpParam); 
      char sText[1000];
      strcpy(sText,(char*)lpParam);
      if(strcmp(sText,"Title of explorer") == 0) {
        EnumChildWindows(hWnd,SearchChildWin,NULL);
      }  
      return TRUE; 
    }
    
    BOOL CALLBACK SearchChildWin(HWND hWnd,LPARAM lpParam) {
      char sClass[1000];
      GetClassName(hWnd,sClass,1000);  
      if(strcmp(sClass,"SysListView32") == 0) {
        LVITEM *iItem = new LVITEM;
        iItem->iSubItem = 0;
        iItem->cchTextMax = 255;
        iItem->pszText = "";
        int nItem = SendMessage(hWnd, LVM_GETITEMCOUNT, 0, 0);
        bool bFound = false;
        int i=0;
        while(!bFound && i < nItem) {
          int nStatus = SendMessage(hWnd,LVM_GETITEMSTATE,i,LVIS_SELECTED);
          if(nStatus == LVIS_SELECTED) {
            SendMessage(hWnd,LVM_GETITEMTEXT, i, (LPARAM)iItem);
            bFound = true;        
          } else i++;
        }    
      }  
      return TRUE;
    }


    Someone know what this code crasshes in the red line??

  6. #6
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    1,384

    Re: Get filename from context menu

    Quote Originally Posted by juanpast
    I try to do this obtaining the title of the explorer and searching selected file but crasshes:

    Code:
    void myDlg::OnButtonSearch() 
    {
      char szWindow[1000];
      EnumWindows(SearchWin,(LPARAM)szWindow);
    }
    
    BOOL CALLBACK SearchWin(HWND hWnd,LPARAM lpParam) {    
      SendMessage(hWnd, WM_GETTEXT, 1000, (LPARAM) lpParam); 
      char sText[1000];
      strcpy(sText,(char*)lpParam);
      if(strcmp(sText,"Title of explorer") == 0) {
        EnumChildWindows(hWnd,SearchChildWin,NULL);
      }  
      return TRUE; 
    }
    
    BOOL CALLBACK SearchChildWin(HWND hWnd,LPARAM lpParam) {
      char sClass[1000];
      GetClassName(hWnd,sClass,1000);  
      if(strcmp(sClass,"SysListView32") == 0) {
        LVITEM *iItem = new LVITEM;
        iItem->iSubItem = 0;
        iItem->cchTextMax = 255;
        iItem->pszText = new char[iItem->cchTextMax + 1];
        int nItem = SendMessage(hWnd, LVM_GETITEMCOUNT, 0, 0);
        bool bFound = false;
        int i=0;
        while(!bFound && i < nItem) {
          int nStatus = SendMessage(hWnd,LVM_GETITEMSTATE,i,LVIS_SELECTED);
          if(nStatus == LVIS_SELECTED) {
            SendMessage(hWnd,LVM_GETITEMTEXT, i, (LPARAM)iItem);
            bFound = true;        
          } else i++;
        }    
      }  
      return TRUE;
    }


    Someone know what this code crasshes in the red line??
    Notice the bold part of the code.

    Regards,
    Usman.

  7. #7
    Join Date
    Feb 2005
    Location
    Madrid (Spain)
    Posts
    511

    Re: Get filename from context menu

    Continue with error... I change SearchChildWin funcion as usman999_1 told me and include the delete command and there are an persisting error, jejej

    Code:
    BOOL CALLBACK SearchChildWin(HWND hWnd,LPARAM lpParam) {
      char sClass[1000];
      GetClassName(hWnd,sClass,1000);  
      if(strcmp(sClass,"SysListView32") == 0) {
        LVITEM *iItem = new LVITEM;
        ZeroMemory(iItem,sizeof(LVITEM));
        iItem->iSubItem = 0;
        iItem->cchTextMax = 255;
        iItem->pszText = new char[iItem->cchTextMax + 1];
        int nItem = SendMessage(hWnd, LVM_GETITEMCOUNT, 0, 0);
        bool bFound = false;
        int i=0;
        while(!bFound && i < nItem) {
          int nStatus = SendMessage(hWnd,LVM_GETITEMSTATE,i,LVIS_SELECTED);
          if(nStatus == LVIS_SELECTED) {
            SendMessage(hWnd,LVM_GETITEMTEXT, i, (LPARAM)iItem);
            char *sNombre = iItem->pszText;
            bFound = true;
          } else i++;
        }
        delete iItem->pszText;
        delete iItem;
      }  
      return TRUE;
    }
    Last edited by juanpast; October 15th, 2007 at 10:06 AM.

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Get filename from context menu

    Quote Originally Posted by Nixonjan
    Thank you but are there any other methods beside COM to catch and pass file name to the custom app ?
    Explorer shell extension is the most native and standard way for this kind of tasks.
    Best regards,
    Igor

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Get filename from context menu

    Quote Originally Posted by juanpast
    Continue with error... I change SearchChildWin funcion as usman999_1 told me and include the delete command and there are an persisting error, jejej
    Man, don't you see you're trying to get the item text from another process? And pass the LVITEM address accross the process boundaries? Nothing surprising then that you come with access violation - there's no memory available at the address in explorer process.
    Best regards,
    Igor

  10. #10
    Join Date
    Feb 2005
    Location
    Madrid (Spain)
    Posts
    511

    Re: Get filename from context menu

    If you write the code above this post the two first SendMessage works fine:

    int nItem = SendMessage(hWnd, LVM_GETITEMCOUNT, 0, 0);
    int nStatus = SendMessage(hWnd,LVM_GETITEMSTATE,i,LVIS_SELECTED);

    ...


    Why can not allocate memory in my aplication and get the text of the Explorer sending another message? The LV struct are not outside of his boundarie because I define until to call SendMessage and delete him before the call.

    Can you explain better why the LV struct if outside of his boundaries?
    Last edited by juanpast; October 15th, 2007 at 11:58 AM.

  11. #11
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Get filename from context menu

    Quote Originally Posted by juanpast
    If you write the code above this post the two first SendMessage works fine:

    int nItem = SendMessage(hWnd, LVM_GETITEMCOUNT, 0, 0);
    int nStatus = SendMessage(hWnd,LVM_GETITEMSTATE,i,LVIS_SELECTED);

    ...
    Right. Perfect. I don't believe it, can't you see the difference? You never pass a pointer while dealing with LVM_GETITEMCOUNT and LVM_GETITEMSTATE. The returned value comes from SendMessage. And Windows takes care about passing the value between process contexts.


    Why can not allocate memory in my aplication and get the text of the Explorer sending another message? The LV struct are not outside of his boundarie because I define until to call SendMessage and delete him before the call.

    Can you explain better why the LV struct if outside of his boundaries?
    You don't get the boundaries thing. It's not about LVITEM variable scope, it's about process address spaces. The LVITEM structure is allocated in your process. You obtain its address and pass it to SendMessage. SendMessage freezes your application and switches the context to a thread the list view belongs to. And definitely this thread in Explorer app context. There the message comes to list view control, it starts processing it been absolutely sure the LVITEM resides in its process address space (that actually is not). The control window procedure attempts to write the data to the pointed memory... boom-bang, hello guys. There's no memory allocated at this address in Explorer process, and this glorious try finishes with access violation exception.

    Another scenario - there is some memory allocated at this address (by some other Explorer code). The list view overwrites the data. Boom-bang! The corrupted data may result in other procedure crashing with the highest probability.

    The general point is: you never pass pointers between processes - because of process address space isolation. (Well, almost never - with very rare exceptions, like WriteProcessMemory, etc.)

    BTW, this latter point must help you solve the problem. You allocate the memory in remote process (Explorer) and pass that pointer to SendMessage, then you ReadProcessMemory, find out the remote pszText address, read process memory for that pointer... you get the idea I believe. Wow, do not forget to deallocate that memory after you're done with it.
    Last edited by Igor Vartanov; October 16th, 2007 at 05:27 AM.
    Best regards,
    Igor

  12. #12
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    1,384

    Re: Get filename from context menu

    juanpast, which app crashes? is it the explorer or your own application???

    Regards,
    Usman.

  13. #13
    Join Date
    Feb 2005
    Location
    Madrid (Spain)
    Posts
    511

    Re: Get filename from context menu

    Thanks Igor.

    usman999_1, crashes the Explorer no my application.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured