00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "stdafx.h"
00028 #include "CCPrintInstallFunctions.h"
00029
00030 #include "CCPrintRegistry.h"
00031 #include "Helpers.h"
00032 #include "CCCommon.h"
00033
00034 #include <winspool.h>
00035 #include <tchar.h>
00036 #include <atlchecked.h>
00037
00039 static LPCTSTR pPrinterFilelist[] =
00040 {
00041 _T("PSCRIPT5.DLL"),
00042 _T("PS5UI.DLL"),
00043 _T("PSCRIPT.HLP"),
00044 #ifdef CC_PDF_CONVERTER
00045 _T("CCPDFPrn.PPD"),
00046 _T("CCPDFConverter.ini"),
00047 _T("CCPrintUI.DLL"),
00048 _T("CCPSRendering.DLL"),
00049 #elif EXCEL_TO_PDF
00050 _T("XLPDFPrn.PPD"),
00051 _T("XL2PDFConverter.ini"),
00052 _T("XL2PDFPrintUI.DLL"),
00053 _T("XL2PDFPSRendering.DLL"),
00054 #else
00055 #error "Please select one of the printer types"
00056 #endif
00057 _T("PSCRIPT.NTF"),
00058 _T("PSCRPTFE.NTF"),
00059 NULL
00060 };
00061
00062 #ifdef CC_PDF_CONVERTER
00064 #define PRINTER_NAME _T("CC PDF Converter")
00066 #define PORT_NAME _T("CCPDFPort")
00068 #define DRIVER_NAME _T("CC PDF Virtual Printer")
00070 #define HARDWARE_ID _T("CCPDFConverter_Driver0101")
00072 #define MONITOR_NAME _T("CCDPF_Redmon")
00074 #define MONITOR_DLL _T("CCPDF_redmonnt.dll")
00075 #elif EXCEL_TO_PDF
00077 #define PRINTER_NAME _T("Excel to PDF Converter")
00079 #define PORT_NAME _T("XL2PDFPort")
00081 #define DRIVER_NAME _T("Excel to PDF Virtual Printer")
00083 #define HARDWARE_ID _T("XL2PDFConverter_Driver0101")
00085 #define MONITOR_NAME _T("XL2PDF_Redmon")
00087 #define MONITOR_DLL _T("XL2PDF_redmonnt.dll")
00088 #else
00089 #error "Please select one of the printer types"
00090 #endif
00091
00093 bool IsX64()
00094 {
00095 HMODULE hKernel32 = ::LoadLibrary(_T("KERNEL32.DLL"));
00096 if (hKernel32 == NULL)
00097 return false;
00098
00099 VOID (WINAPI* pfnGetNativeSystemInfo)(LPSYSTEM_INFO);
00100
00101 *(FARPROC*)&pfnGetNativeSystemInfo = ::GetProcAddress(hKernel32, "GetNativeSystemInfo");
00102 if (pfnGetNativeSystemInfo == NULL)
00103 {
00104 ::FreeLibrary(hKernel32);
00105 return false;
00106 }
00107
00108 SYSTEM_INFO si;
00109 (*pfnGetNativeSystemInfo)(&si);
00110
00111 ::FreeLibrary(hKernel32);
00112
00113 return (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64);
00114 }
00115
00116
00120 CCPDFPrinterInstaller::CCPDFPrinterInstaller(HWND hWnd) : PrinterInstall(PRINTER_NAME, PORT_NAME, DRIVER_NAME, HARDWARE_ID, pPrinterFilelist), m_hWnd(hWnd)
00121 {
00122 m_bX64 = IsX64();
00123 }
00124
00128 void CCPDFPrinterInstaller::ShowError(LPCTSTR lpError)
00129 {
00130 ::MessageBox(m_hWnd, lpError, _T("Installing printer"), MB_ICONERROR|MB_OK);
00131 }
00132
00137 bool CCPDFPrinterInstaller::IsPrinterInstalled()
00138 {
00139
00140 bool bInstalled;
00141
00142 if (!PrinterInstall::IsPrinterInstalled(bInstalled))
00143 {
00144
00145 ShowError(_T("Error while testing for existing printer"));
00146 return false;
00147 }
00148
00149
00150 return bInstalled;
00151 }
00152
00157 bool CCPDFPrinterInstaller::DoInstall(HMODULE hModule , LPTSTR installationPath )
00158 {
00159
00160 TCHAR cPath[MAX_PATH + 2], cError[2048];
00161 std::tstring sInstallPath;
00162
00163
00164 if (_tcscmp(installationPath,""))
00165 {
00166 _tcscpy_s (cPath, MAX_PATH, installationPath);
00167 _tcscat_s (cPath, MAX_PATH, "\\");
00168 }
00169 else
00170 {
00171 if (!::GetModuleFileName(hModule, cPath, MAX_PATH + 1))
00172 {
00173 ShowError("Cannot retrieve path");
00174 return false;
00175 }
00176
00177
00178 TCHAR* pSlash = _tcsrchr(cPath, '\\');
00179 if (pSlash == NULL)
00180 {
00181 ShowError(_T("Error while retrieving path"));
00182 return false;
00183 }
00184 *(pSlash + 1) = '\0';
00185 }
00186
00187
00188 sInstallPath = cPath;
00189
00190
00191 _tcscat_s(cPath, _S(cPath), m_bX64? _T("DriverFiles64bit") : _T("DriverFiles"));
00192
00193
00194
00195 if (!AddPortMonitor(MONITOR_NAME, MONITOR_DLL, cPath))
00196 {
00197 _stprintf_s(cError, _S(cError), _T("Printer installation error %d (%d)"), GetError(), GetLastError());
00198 ShowError(cError);
00199 return false;
00200 }
00201
00202
00203 if (Install(cPath, m_hWnd) != 0)
00204 {
00205
00206 _stprintf_s(cError, _S(cError), _T("Printer installation error %d (%d)"), GetError(), GetLastError());
00207 ShowError(cError);
00208 RemovePortMonitor(MONITOR_NAME);
00209 return false;
00210 }
00211
00212
00213
00214 HKEY hKey;
00215 if (::RegCreateKeyEx(HKEY_LOCAL_MACHINE, _T("SYSTEM\\CurrentControlSet\\Control\\Print\\Monitors\\") MONITOR_NAME _T("\\Ports\\" PORT_NAME), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL) != ERROR_SUCCESS)
00216 {
00217
00218 ShowError(_T("Could not access the port registry settings"));
00219 Uninstall(m_hWnd);
00220 RemovePortMonitor(MONITOR_NAME);
00221 return false;
00222 }
00223
00224 #ifdef CC_PDF_CONVERTER
00225 std::tstring sAppPath = ::ConcatPaths(sInstallPath, _T("CCPDFConverter.exe"));
00226 #elif EXCEL_TO_PDF
00227 std::tstring sAppPath = ::ConcatPaths(sInstallPath, _T("XL2PDFConverter.exe"));
00228 #else
00229 #error "Please select one of the printer types"
00230 #endif
00231 if (::RegSetValueEx(hKey, _T("Command"), 0, REG_SZ, (const BYTE*)sAppPath.c_str(), (sAppPath.size() + 1) * sizeof(TCHAR)) != ERROR_SUCCESS)
00232 {
00233
00234 ShowError(_T("Could not write the port registry settings (1)"));
00235 ::RegCloseKey(hKey);
00236 Uninstall(m_hWnd);
00237 RemovePortMonitor(MONITOR_NAME);
00238 return false;
00239 }
00240
00241 DWORD dwValue = 300;
00242 if (::RegSetValueEx(hKey, _T("Delay"), 0, REG_DWORD, (const BYTE*)&dwValue, sizeof(dwValue)) != ERROR_SUCCESS)
00243 {
00244 ShowError(_T("Could not write the port registry settings (2)"));
00245 ::RegCloseKey(hKey);
00246 Uninstall(m_hWnd);
00247 RemovePortMonitor(MONITOR_NAME);
00248 return false;
00249 }
00250
00251 dwValue = 1;
00252 if (::RegSetValueEx(hKey, _T("RunUser"), 0, REG_DWORD, (const BYTE*)&dwValue, sizeof(dwValue)) != ERROR_SUCCESS)
00253 {
00254 ShowError(_T("Could not write the port registry settings (3)"));
00255 ::RegCloseKey(hKey);
00256 Uninstall(m_hWnd);
00257 RemovePortMonitor(MONITOR_NAME);
00258 return false;
00259 }
00260
00261 dwValue = 0;
00262 if (::RegSetValueEx(hKey, _T("ShowWindow"), 0, REG_DWORD, (const BYTE*)&dwValue, sizeof(dwValue)) != ERROR_SUCCESS)
00263 {
00264 ShowError(_T("Could not write the port registry settings (4)"));
00265 ::RegCloseKey(hKey);
00266 Uninstall(m_hWnd);
00267 RemovePortMonitor(MONITOR_NAME);
00268 return false;
00269 }
00270 ::RegCloseKey(hKey);
00271
00272
00273
00274 BOOL bRet = FALSE;
00275 PRINTER_DEFAULTS pd;
00276 HANDLE hPrinter;
00277 ZeroMemory(&pd, sizeof(PRINTER_DEFAULTS));
00278 pd.DesiredAccess = PRINTER_ALL_ACCESS;
00279 if (!::OpenPrinter(PRINTER_NAME, &hPrinter, &pd))
00280 {
00281 ShowError(_T("Could not open the printer for setting paths (1)"));
00282 return false;
00283 }
00284
00285
00286 CCPrintRegistry::SetRegistryString(hPrinter, _T("DB Path"), ::ConcatPaths(sInstallPath, _T("cc2.db3")).c_str());
00287 CCPrintRegistry::SetRegistryString(hPrinter, _T("Image Path"), ::ConcatPaths(sInstallPath, _T("Images\\")).c_str());
00288 CCPrintRegistry::SetRegistryBool(hPrinter, (LPTSTR)SETTINGS_AUTOOPEN, CanOpenPDFFiles());
00289 ::ClosePrinter(hPrinter);
00290
00291 return true;
00292 }
00293
00297 bool CCPDFPrinterInstaller::DoRemove()
00298 {
00299 TCHAR cError[2048];
00300
00301
00302 if (Uninstall(m_hWnd) != 0)
00303 {
00304 _stprintf_s(cError, _S(cError), _T("Printer removal error %d (%d)"), GetError(), GetLastError());
00305 ShowError(cError);
00306 return false;
00307 }
00308
00309
00310 if (!RemovePortMonitor(MONITOR_NAME))
00311 {
00312 _stprintf_s(cError, _S(cError), _T("Printer removal error %d (%d)"), GetError(), GetLastError());
00313 ShowError(cError);
00314 return false;
00315 }
00316
00317 return true;
00318 }