some refactoring

This commit is contained in:
S T Chan 2013-12-12 11:48:50 -05:00
parent 42b7abe4f0
commit e012a80ffa
4 changed files with 70 additions and 49 deletions

View File

@ -13,8 +13,8 @@ Global
Release|x86 = Release|x86 Release|x86 = Release|x86
EndGlobalSection EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1EAD8E9A-A123-4C37-B31E-AEE1354DF003}.Debug|x64.ActiveCfg = Debug|x64 {1EAD8E9A-A123-4C37-B31E-AEE1354DF003}.Debug|x64.ActiveCfg = Debug|Any CPU
{1EAD8E9A-A123-4C37-B31E-AEE1354DF003}.Debug|x64.Build.0 = Debug|x64 {1EAD8E9A-A123-4C37-B31E-AEE1354DF003}.Debug|x64.Build.0 = Debug|Any CPU
{1EAD8E9A-A123-4C37-B31E-AEE1354DF003}.Debug|x86.ActiveCfg = Debug|x86 {1EAD8E9A-A123-4C37-B31E-AEE1354DF003}.Debug|x86.ActiveCfg = Debug|x86
{1EAD8E9A-A123-4C37-B31E-AEE1354DF003}.Debug|x86.Build.0 = Debug|x86 {1EAD8E9A-A123-4C37-B31E-AEE1354DF003}.Debug|x86.Build.0 = Debug|x86
{1EAD8E9A-A123-4C37-B31E-AEE1354DF003}.Release|x64.ActiveCfg = Release|x64 {1EAD8E9A-A123-4C37-B31E-AEE1354DF003}.Release|x64.ActiveCfg = Release|x64

View File

@ -154,28 +154,10 @@ namespace PdfScribe
int bufferSize, int bufferSize,
ref int Bytes); ref int Bytes);
public static IntPtr DisableWow64Redirection()
{
IntPtr oldValue = IntPtr.Zero;
if (Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess)
if (!NativeMethods.Wow64DisableWow64FsRedirection(ref oldValue))
throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not disable Wow64 file system redirection.");
return oldValue;
}
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr); public static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);
public static void RevertWow64Redirection(IntPtr oldValue)
{
if (Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess)
{
if (!NativeMethods.Wow64RevertWow64FsRedirection(oldValue))
{
throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not reenable Wow64 file system redirection.");
}
}
}
} }
} }

View File

@ -91,7 +91,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="PdfScribeInstaller.cs" /> <Compile Include="PdfScribeInstaller.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="NativeMethods.cs" /> <Compile Include="NativeMethods.cs" />
</ItemGroup> </ItemGroup>

View File

@ -33,6 +33,7 @@ namespace PdfScribe
#region Port operations #region Port operations
private int AddPdfScribePort(string portName) private int AddPdfScribePort(string portName)
{ {
return DoXcvDataPortOperation(portName, "AddPort"); return DoXcvDataPortOperation(portName, "AddPort");
@ -89,9 +90,12 @@ namespace PdfScribe
#region Port Monitor #region Port Monitor
public bool AddPdfScribePortMonitor(String monitorName, /// <summary>
String monitorFile, /// Adds the PDF Scribe port monitor
String monitorPath) /// </summary>
/// <param name="monitorFilePath">Directory where the uninstalled monitor dll is located</param>
/// <returns>true if the monitor is installed, false if install failed</returns>
public bool AddPdfScribePortMonitor(String monitorFilePath)
{ {
bool monitorAdded = false; bool monitorAdded = false;
@ -99,20 +103,20 @@ namespace PdfScribe
try try
{ {
oldRedirectValue = NativeMethods.DisableWow64Redirection(); oldRedirectValue = DisableWow64Redirection();
if (!DoesMonitorExist(monitorName)) if (!DoesMonitorExist(PORTMONITOR))
{ {
// Copy the monitor DLL to // Copy the monitor DLL to
// the system directory // the system directory
String fileSourcePath = Path.Combine(monitorPath, monitorFile); String fileSourcePath = Path.Combine(monitorFilePath, MONITORDLL);
String fileDestinationPath = Path.Combine(Environment.SystemDirectory, monitorFile); String fileDestinationPath = Path.Combine(Environment.SystemDirectory, MONITORDLL);
File.Copy(fileSourcePath, fileDestinationPath, true); File.Copy(fileSourcePath, fileDestinationPath, true);
MONITOR_INFO_2 newMonitor = new MONITOR_INFO_2(); MONITOR_INFO_2 newMonitor = new MONITOR_INFO_2();
newMonitor.pName = monitorName; newMonitor.pName = PORTMONITOR;
newMonitor.pEnvironment = ENVIRONMENT_64; newMonitor.pEnvironment = ENVIRONMENT_64;
newMonitor.pDLLName = monitorFile; newMonitor.pDLLName = MONITORDLL;
if (!AddPortMonitor(newMonitor)) if (!AddPortMonitor(newMonitor))
throw new Win32Exception(Marshal.GetLastWin32Error(), String.Format("Could not add port monitor {0}", monitorName)); throw new Win32Exception(Marshal.GetLastWin32Error(), String.Format("Could not add port monitor {0}", PORTMONITOR));
else else
monitorAdded = true; monitorAdded = true;
} }
@ -120,25 +124,54 @@ namespace PdfScribe
} }
finally finally
{ {
/* if (oldRedirectValue != IntPtr.Zero) RevertWow64Redirection(oldRedirectValue);
// Remove the monitor dll if copied
try
{
File.Delete(Path.Combine(Environment.SystemDirectory, monitorFile));
}
catch { }
*/
if (oldRedirectValue != IntPtr.Zero) NativeMethods.RevertWow64Redirection(oldRedirectValue);
} }
return monitorAdded; return monitorAdded;
} }
public bool RemoveSoftscanPortMonitor(String monitorName)
/// <summary>
/// Disables WOW64 system directory file redirection
/// if the current process is both
/// 32-bit, and running on a 64-bit OS
/// </summary>
/// <returns>A Handle, which should be retained to reenable redirection</returns>
private IntPtr DisableWow64Redirection()
{
IntPtr oldValue = IntPtr.Zero;
if (Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess)
if (!NativeMethods.Wow64DisableWow64FsRedirection(ref oldValue))
throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not disable Wow64 file system redirection.");
return oldValue;
}
/// <summary>
/// Reenables WOW64 system directory file redirection
/// if the current process is both
/// 32-bit, and running on a 64-bit OS
/// </summary>
/// <param name="oldValue">A Handle value - should be retained from call to <see cref="DisableWow64Redirection"/></param>
private void RevertWow64Redirection(IntPtr oldValue)
{
if (Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess)
{
if (!NativeMethods.Wow64RevertWow64FsRedirection(oldValue))
{
throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not reenable Wow64 file system redirection.");
}
}
}
/// <summary>
/// Removes the PDF Scribe port monitor
/// </summary>
/// <returns>true if monitor successfully removed, false if removal failed</returns>
public bool RemovePdfScribePortMonitor()
{ {
bool monitorRemoved = false; bool monitorRemoved = false;
if ((NativeMethods.DeleteMonitor(null, ENVIRONMENT_64, monitorName)) != 0) if ((NativeMethods.DeleteMonitor(null, ENVIRONMENT_64, PORTMONITOR)) != 0)
monitorRemoved = true; monitorRemoved = true;
return monitorRemoved; return monitorRemoved;
} }
@ -236,15 +269,22 @@ namespace PdfScribe
#if DEBUG #if DEBUG
public bool InstallSoftscanPrinter_Test() public bool InstallSoftscanPrinter_Test()
{ {
String printerName = PRINTERNAME;
String driverSourceDirectory = @"C:\Code\OaisisRedmonInstaller\Lib\"; String driverSourceDirectory = @"C:\Code\OaisisRedmonInstaller\Lib\";
String[] driverFilesToCopy = new String[] { DRIVERFILE, DRIVERDATAFILE, DRIVERHELPFILE, DRIVERUIFILE }; String[] driverFilesToCopy = new String[] { DRIVERFILE, DRIVERDATAFILE, DRIVERHELPFILE, DRIVERUIFILE };
String[] dependentFilesToCopy = new String[] { "PSCRIPT.NTF" }; String[] dependentFilesToCopy = new String[] { "PSCRIPT.NTF" };
return InstallPdfScribePrinter(printerName, driverSourceDirectory, driverFilesToCopy, dependentFilesToCopy); return InstallPdfScribePrinter(driverSourceDirectory, driverFilesToCopy, dependentFilesToCopy);
} }
#endif #endif
public bool InstallPdfScribePrinter(String printerName,
String driverSourceDirectory, /// <summary>
/// Installs the port monitor, port,
/// printer drivers, and PDF Scribe virtual printer
/// </summary>
/// <param name="driverSourceDirectory">Directory where the uninstalled printer driver files are located</param>
/// <param name="driverFilesToCopy">An array containing the printer driver's filenames</param>
/// <param name="dependentFilesToCopy">An array containing dependent filenames</param>
/// <returns>true if printer installed, false if failed</returns>
public bool InstallPdfScribePrinter(String driverSourceDirectory,
String[] driverFilesToCopy, String[] driverFilesToCopy,
String[] dependentFilesToCopy) String[] dependentFilesToCopy)
{ {
@ -255,7 +295,7 @@ namespace PdfScribe
{ {
if (CopyPrinterDriverFiles(driverSourceDirectory, driverFilesToCopy.Concat(dependentFilesToCopy).ToArray())) if (CopyPrinterDriverFiles(driverSourceDirectory, driverFilesToCopy.Concat(dependentFilesToCopy).ToArray()))
{ {
if (AddPdfScribePortMonitor(PORTMONITOR, MONITORDLL, driverSourceDirectory)) if (AddPdfScribePortMonitor(driverSourceDirectory))
{ {
if (InstallPrinterDriver(driverDirectory, dependentFilesToCopy)) if (InstallPrinterDriver(driverDirectory, dependentFilesToCopy))
{ {