-
October 15th, 2007, 07:22 AM
#1
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
-
October 15th, 2007, 07:30 AM
#2
Re: Get filename from context menu
Have a look at Shell Extension.
Regards,
Usman.
-
October 15th, 2007, 07:47 AM
#3
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 ?
-
October 15th, 2007, 08:52 AM
#4
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.
-
October 15th, 2007, 09:39 AM
#5
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??
-
October 15th, 2007, 09:50 AM
#6
Re: Get filename from context menu
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.
-
October 15th, 2007, 10:02 AM
#7
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.
-
October 15th, 2007, 10:50 AM
#8
Re: Get filename from context menu
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
-
October 15th, 2007, 10:57 AM
#9
Re: Get filename from context menu
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
-
October 15th, 2007, 11:55 AM
#10
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.
-
October 16th, 2007, 03:37 AM
#11
Re: Get filename from context menu
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
-
October 16th, 2007, 03:41 AM
#12
Re: Get filename from context menu
juanpast, which app crashes? is it the explorer or your own application???
Regards,
Usman.
-
October 16th, 2007, 03:47 AM
#13
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|