1
votes

I m trying to open a file using shellexecute command my code is

ShellExecute(NULL,NULL,itemPath,NULL,NULL,SW_SHOW); 

or

ShellExecute(NULL,L"open",itemPath,NULL,NULL,SW_SHOW); 

I have used both ways but when I m giving path of a folder it opens a folder but when I m giving a full specified path of a file it doesn't work. one thing more if I m giving a hardcode path like for example

ShellExecute(NULL,L"open",L"E:\\abc.xlsx",NULL,NULL,SW_SHOW);

than it opens this file. can any one explain why it is happening.

1
You need to escape backslashes in C++, e.g. E:\\abc.xlsx.Jonathan Potter
I have used that. ShellExecute(NULL,L"open",L"E:\\abc.xlsx",NULL,NULL,SW_SHOW); this command runs perfectly . problem occurs when I m passing path using variable..Kashif Meo
What type is itemPath?IInspectable
itempath is of CString typeKashif Meo
If the call to ShellExecute appears to work when passing a string literal with the exact same content as a CString variable, then more likely than not your variable doesn't hold the value you think it does. At any rate, this isn't nearly enough information to help answer the question. You need to provide a minimal reproducible example. You should also consider calling ShellExecuteEx instead for better error reporting.IInspectable

1 Answers

2
votes

ShellExecute works correctly, and the defect can be found in your code. The only explanation that makes sense is that itemPath is not what you think it is. If it were indeed a pointer to null-terminated character array containing L"E:\\abc.xlsx" then ShellExecute would behave as you expect.

You can debug the problem by inspecting the content of itemPath to find out what it really contains. Were you to have provided an MCVE then we could have been more specific in the diagnosis of the problem.

Finally, ShellExecute is deprecated, largely because it provides no good means of reporting failure conditions. You should use ShellExecuteEx instead.