-
April 13th, 2010, 11:40 AM
#1
ShellExecute problem
In the following code the first ShellExecute statement does not work even though even though strAppDirectory contains the same path as the next ShellExecute statement that does work.
Do I have a dumb mistake that I am missing?
void CDBcalcDlg::OnHelp()
{
char szAppPath[MAX_PATH] = "";
CString strAppDirectory;
::GetModuleFileName(0, szAppPath, sizeof(szAppPath) - 1);
// Extract directory
strAppDirectory = szAppPath;
strAppDirectory = strAppDirectory.Left(strAppDirectory.ReverseFind('\\'));
ShellExecute(hWnd, _T("open"), _T("C:\\strAppDirectory\\dBcalc.chm"),NULL, NULL, SW_SHOWNORMAL);
ShellExecute(hWnd, _T("open"), _T("C:\\Users\\Clay\\Documents\\Visual Studio 2005\\Projects\\Chm_dBcalc\\dBcalc\\Debug\\dBcalc.chm"),NULL, NULL, SW_SHOWNORMAL);
}
Last edited by Clay L; April 13th, 2010 at 02:56 PM.
-
April 13th, 2010, 11:58 AM
#2
Re: ShellExecute problem
Is there supposed to be a space in the first path?
-
April 13th, 2010, 02:58 PM
#3
Re: ShellExecute problem
No there isn't, but I removed it and rebuilt the program but it didn't help.
Good catch though.
-
April 13th, 2010, 03:29 PM
#4
Re: ShellExecute problem
Originally Posted by Clay L
In the following code the first ShellExecute statement does not work even though even though strAppDirectory contains the same path as the next ShellExecute statement that does work.
Do I have a dumb mistake that I am missing?
Code:
void CDBcalcDlg::OnHelp()
{
char szAppPath[MAX_PATH] = "";
CString strAppDirectory;
::GetModuleFileName(0, szAppPath, sizeof(szAppPath) - 1);
// Extract directory
strAppDirectory = szAppPath;
strAppDirectory = strAppDirectory.Left(strAppDirectory.ReverseFind('\\'));
ShellExecute(hWnd, _T("open"), _T("C:\\strAppDirectory\\dBcalc.chm"),NULL, NULL, SW_SHOWNORMAL);
ShellExecute(hWnd, _T("open"), _T("C:\\Users\\Clay\\Documents\\Visual Studio 2005\\Projects\\Chm_dBcalc\\dBcalc\\Debug\\dBcalc.chm"),NULL, NULL, SW_SHOWNORMAL);
}
What strAppDirectory contains is irrelevant because you aren't using it in your ShellExecute calls.
Btw, you probably want to get out of the practice of using 'char' like in the following code:
Code:
char szAppPath[MAX_PATH] = "";
Instead use TCHAR
Code:
TCHAR szAppPath[MAX_PATH] = _T("");
Actually, in your case you don't need to create a temp buffer to store string data because the CString class allows you to write to it directly.
Code:
::GetModuleFileName(0, strAppDirectory.GetBuffer( MAX_PATH ), MAX_PATH - 1);
strAppDirectory.ReleaseBuffer( );
-
April 13th, 2010, 04:30 PM
#5
Re: ShellExecute problem
Originally Posted by MSDN
If the function succeeds, it returns a value greater than 32. If the function fails, it returns an error value that indicates the cause of the failure.
So....the million-dollar-question is....?
-
April 14th, 2010, 02:02 PM
#6
Re: ShellExecute problem
The problem was that I had strAppDirectory inside quote marks.
Either of the two statements below work properly to get the path into strToOpen.
strToOpen.Format(_T("%s\\dBcalc.chm"), strAppDirectory); //This works
strToOpen = strAppDirectory + _T("\\dBcalc.chm"); //So does this
ShellExecute(hWnd, _T("open"), _T(strToOpen),NULL, NULL, SW_SHOWNORMAL); // executes normally now
The answers came from two different guys on a MSD forum.
Arjay also pointed out my error but I didn't understand it at the time.
Last edited by Clay L; April 14th, 2010 at 02:05 PM.
-
April 14th, 2010, 03:49 PM
#7
Re: ShellExecute problem
You don't need the _T here because it is a CString, not a char*.
-
April 14th, 2010, 05:17 PM
#8
Re: ShellExecute problem
Originally Posted by Skizmo
You don't need the _T here because it is a CString, not a char*.
You are right of course. I just copied it from the answer and didn't notice that.
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
|