Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all,

I'm having a strange issue and I hope someone could help me on this one.

In the following C++ code sample:
C++
OFLOG_INFO(logger, "Auto open PDF report feature enabled. Launching pdf viewer for file: " + rData.getPDFFileName());

// It is apparently recommended... See MSDN references.
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); 

SHELLEXECUTEINFO ShExecInfo;
ShExecInfo.cbSize = sizeof(ShExecInfo);
ShExecInfo.fMask = NULL;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = (LPCSTR)"open";

// a small trick to cast from string to LPCSTR
char fileNameCast[MAX_PATH]; 
sprintf_s(fileNameCast, "\"%s\"", rData.getPDFFileName().c_str()); 

ShExecInfo.lpFile = (LPCSTR)fileNameCast;
ShExecInfo.lpParameters = NULL;
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOWDEFAULT;
ShExecInfo.hInstApp = NULL;

if(! ShellExecuteEx(&ShExecInfo))
{
	OFLOG_ERROR(logger, "Error during pdf auto-opening. Error Code: " +  int(ShExecInfo.hInstApp));
	OFLOG_ERROR(logger, GetLastError());
	somethingWrong = true;
}


I try to automatically open a PDF file with the default PDF reader.

This code works like a charm on my development computer but as soon as I deploy my application on a test machine, nothing happens. I can see from the logs that the code is executed without any error but just nothing...

I'm debugging this for a couple of days now and I'm pretty sure I'm missing something stupid. I hope someone can see it. See MSDN reference here.

Note the test machine has a pdf reader (Adobe Reader) installed and set as default. Running OS is Windows 7 on both development and deployment machine. I also tried with the simpler function ShellExecute with exactly the same result.

Thanks in advance for your help. I really appreciate.
Yass.
Posted
Comments
Sergey Alexandrovich Kryukov 8-May-13 11:41am    
You can never guarantee that a PDF reader is available in the target system... Yes, I got it: you have it in this case.
—SA
YassJD 8-May-13 14:35pm    
True... But this will be imposed as a prerequisite. Thanks for the remark :)
Sergey Alexandrovich Kryukov 8-May-13 15:17pm    
Sure.
—SA
Style-7 8-May-13 12:03pm    
Do you use full path?
Try it: ShellExecute( hWnd, "open", "", NULL, NULL, SW_SHOWNORMAL);
YassJD 8-May-13 14:39pm    
Hi,
Yes... I was actually using the ShellExecute in the beginning but I switched to ShellExecuteEx to get an accurate error code.

I did some tests and if the file name/path is not correct, the error code ERROR_FILE_NOT_FOUND or ERROR_PATH_NOT_FOUND is logged during the GetLastError call...

Thanks !

1 solution

Okay. I finally found what happened.

As you might imagine, the issue is not caused by the code itself. It was actually running under a Windows service (Local System), and thus in another environment. The PDF viewer was launched in the virtual user SYSTEM environment and not visible for the current user.

I am planning to use the CreateProcessAsUser function to make it run in the environment of the current user.

Thank you all for your help ! :)

Regards,
Yass
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900