ShellExecute returns error on a link with parameters
I have a link with parameters that may be passed to the application, pretty much a line like this:
"C:\Documents and Settings\All Users\Start Menu\Program.lnk" "C:\Documents and Settings\PC12\Desktop\Sample.txt"
If I copy and paste the link above into Start -> Run, it works just fine and launches the Program with the Sample.txt attached to it, but when I try to open it with ShellExecute like this:
Code:
ShellExecute(NULL, _T("Open"), _T("\"C:\\Documents and Settings\\All Users\\Start Menu\\Program.lnk\" \"C:\\Documents and Settings\\PC12\\Desktop\\Sample.txt\""), NULL, SW_SHOW);
Re: ShellExecute returns error on a link with parameters
Yes, I apologize, I missed one NULL, then it would compile. As for separating path from parameters, I agree that it may work that way but my question then becomes, how do you separate path from parameters? You see, in real situation I don't have it hardcoded like that, it is provided by a user.
Re: ShellExecute returns error on a link with parameters
I don't know if there's a better way, but I just parse it with some reasonable heuristics. Here's a clip from a hotlink control I did a while back. Should give you an idea of how I parse the command string as supplied by the user:
Code:
void CXHotlink::OnLButtonDown(CView* /*pView*/, UINT /*nFlags*/, const CPoint& /*point*/)
{
CString cs;
wchar_t *p,*q,buf1[MAX_PATH],buf2[MAX_PATH];
long i;
HINSTANCE rc; //shellexecute() returns an HINSTANCE.
if(m_PropStruct.nAction>0){
p=wcschr(m_PropStruct.LinkText,L'\"');
if(p){
q=wcschr(p+1,L'\"');
if(q){
//somethying is enclosed in quotes
//copy everything between the quotes to the first buffer
wcsncpy_s(buf1,MAX_PATH,p+1,q-(p+1));
//copy the rest to the second buffer
wcsncpy_s(buf2,MAX_PATH,q+1,MAX_PATH-1);
}else{
//malformed
AfxMessageBox(L"Unable to parse command string.",MB_ICONERROR);
return;
}
}else{
//no quotes
//find space separator
p=wcschr(m_PropStruct.LinkText,L' ');
if(p){
wcsncpy_s(buf1,MAX_PATH,m_PropStruct.LinkText,p-m_PropStruct.LinkText);
//copy the rest to the second buffer
wcsncpy_s(buf2,MAX_PATH,p+1,MAX_PATH-1);
}else{
wcsncpy_s(buf1,MAX_PATH,m_PropStruct.LinkText,MAX_PATH-1);
buf2[0]=0;
}
}
rc=ShellExecute(AfxGetMainWnd()->m_hWnd,L"open",buf1,buf2,NULL,1);
if((int)rc<33){
AfxMessageBox(L"Unable to launch program",MB_ICONERROR);
}
}else{
i=wcstol(m_PropStruct.LinkText,NULL,10);
AfxGetMainWnd()->PostMessage(WM_GOTOVIEW,i,0);
}
}
Re: ShellExecute returns error on a link with parameters
In my project I allowed the user to browse for the program to execute, then specify the command line. I originally just looked for the first space to delimit command from arguments, but, if the program path had any spaces in it, I was screwed. I noticed that if the user browsed for the path, and the path had spaces in it, it came back from the CFileDIalog with quotes around it. That's why I added the check for quotes first.
Like I said, there is probably a better way, but this worked in every case we know of with our implementation.
Re: ShellExecute returns error on a link with parameters
I guess I should go with File Open dialog instead of letting a user type in the path in an edit box. I'm learning now that some shortcuts don't like the verb "Open", so that has to be NULL as well.
I'm not sure if Microsoft itself knows their own creation, i.e. ShellExecute?
Re: ShellExecute returns error on a link with parameters
Originally Posted by Arjay
Have you tried CreateProcess( NULL, _T("full path w parameters"), ... );
Arjay, CreateProcess unfortunately doesn't work for a link, and here I'm coming to a catch-22. Some of those links don't get resolved with a simple IShellLink call. I started a separate thread on this subject. For some reason Microsoft introduced a new type of links that return gibberish in IShellLink::GetPath, and since I have no clue what's going on there I thought that maybe ShellExecute can help.... but, as always with MS it's not that easy.
Last edited by ahmd; September 28th, 2009 at 04:07 PM.
Re: ShellExecute returns error on a link with parameters
Ahmd, post a valid link of something like Word and I'll try it using the Process class in C#. If that works, we can drill down and see how it's implemented under the covers.
Re: ShellExecute returns error on a link with parameters
Originally Posted by Arjay
Ahmd, post a valid link of something like Word and I'll try it using the Process class in C#. If that works, we can drill down and see how it's implemented under the covers.
Arjay, I'm attaching a link to MS Word 2007 that exhibits this behavior.
Hello, can you tell me how you detect if something is a link ?
Please start a new thread for a new topic. To answer your question, I believe that all you need to do is to check the file extension and see that it's .lnk for the file system link.
Re: ShellExecute returns error on a link with parameters
Arjay, isn't it what you showed a Winword itself? People tell me that it's an MSI (I believe) link, whatever that beast is. That's why I was ragging on Microsoft for reinventing the wheel.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.