Some more refactoring, removed some thrown Win32 exceptions, replaced them with tracing code

This commit is contained in:
S T Chan 2013-12-23 01:47:53 -05:00
parent a0048698e7
commit 2367d0e6c0
13 changed files with 157 additions and 52 deletions

View File

@ -11,6 +11,11 @@ using System.Runtime.InteropServices;
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
#if DEBUG
[assembly: AssemblyConfiguration("DEBUG")]
#else
[assembly: AssemblyConfiguration("RELEASE")]
#endif
// Version information for an assembly consists of the following four values:
//

View File

@ -35,8 +35,8 @@ Global
{1EAD8E9A-A123-4C37-B31E-AEE1354DF003}.Debug|x64.Build.0 = Debug|x64
{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}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1EAD8E9A-A123-4C37-B31E-AEE1354DF003}.Release|Any CPU.Build.0 = Release|Any CPU
{1EAD8E9A-A123-4C37-B31E-AEE1354DF003}.Release|Any CPU.ActiveCfg = Release|x64
{1EAD8E9A-A123-4C37-B31E-AEE1354DF003}.Release|Any CPU.Build.0 = Release|x64
{1EAD8E9A-A123-4C37-B31E-AEE1354DF003}.Release|x64.ActiveCfg = Release|x64
{1EAD8E9A-A123-4C37-B31E-AEE1354DF003}.Release|x64.Build.0 = Release|x64
{1EAD8E9A-A123-4C37-B31E-AEE1354DF003}.Release|x86.ActiveCfg = Release|x86
@ -47,16 +47,20 @@ Global
{09BB3AA3-96D3-4BA1-BCB3-4E17067F42B2}.Debug|x64.Build.0 = Debug|x64
{09BB3AA3-96D3-4BA1-BCB3-4E17067F42B2}.Debug|x86.ActiveCfg = Debug|x86
{09BB3AA3-96D3-4BA1-BCB3-4E17067F42B2}.Debug|x86.Build.0 = Debug|x86
{09BB3AA3-96D3-4BA1-BCB3-4E17067F42B2}.Release|Any CPU.ActiveCfg = Release|x86
{09BB3AA3-96D3-4BA1-BCB3-4E17067F42B2}.Release|x64.ActiveCfg = Release|x86
{09BB3AA3-96D3-4BA1-BCB3-4E17067F42B2}.Release|Any CPU.ActiveCfg = Release|x64
{09BB3AA3-96D3-4BA1-BCB3-4E17067F42B2}.Release|Any CPU.Build.0 = Release|x64
{09BB3AA3-96D3-4BA1-BCB3-4E17067F42B2}.Release|x64.ActiveCfg = Release|x64
{09BB3AA3-96D3-4BA1-BCB3-4E17067F42B2}.Release|x64.Build.0 = Release|x64
{09BB3AA3-96D3-4BA1-BCB3-4E17067F42B2}.Release|x86.ActiveCfg = Release|x86
{09BB3AA3-96D3-4BA1-BCB3-4E17067F42B2}.Release|x86.Build.0 = Release|x86
{E8679E1B-8C89-4201-97D5-7E43C5A486C9}.Debug|Any CPU.ActiveCfg = Debug|x64
{E8679E1B-8C89-4201-97D5-7E43C5A486C9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E8679E1B-8C89-4201-97D5-7E43C5A486C9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E8679E1B-8C89-4201-97D5-7E43C5A486C9}.Debug|x64.ActiveCfg = Debug|x64
{E8679E1B-8C89-4201-97D5-7E43C5A486C9}.Debug|x64.Build.0 = Debug|x64
{E8679E1B-8C89-4201-97D5-7E43C5A486C9}.Debug|x86.ActiveCfg = Debug|x86
{E8679E1B-8C89-4201-97D5-7E43C5A486C9}.Debug|x86.Build.0 = Debug|x86
{E8679E1B-8C89-4201-97D5-7E43C5A486C9}.Release|Any CPU.ActiveCfg = Release|x64
{E8679E1B-8C89-4201-97D5-7E43C5A486C9}.Release|Any CPU.Build.0 = Release|x64
{E8679E1B-8C89-4201-97D5-7E43C5A486C9}.Release|x64.ActiveCfg = Release|x64
{E8679E1B-8C89-4201-97D5-7E43C5A486C9}.Release|x64.Build.0 = Release|x64
{E8679E1B-8C89-4201-97D5-7E43C5A486C9}.Release|x86.ActiveCfg = Release|x86

View File

@ -1,4 +1,5 @@
<Window x:Class="PdfScribe.ActivityNotification"
x:ClassModifier="internal"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ActivityNotification" Height="300" Width="300"

View File

@ -16,9 +16,9 @@ namespace PdfScribe
/// <summary>
/// Interaction logic for ActivityNotification.xaml
/// </summary>
public partial class ActivityNotification : Window
internal partial class ActivityNotification : Window
{
public ActivityNotification()
internal ActivityNotification()
{
InitializeComponent();
}

View File

@ -38,8 +38,8 @@ namespace PdfScribe
/// with a standard error icon, and
/// just a Close button
/// </summary>
/// <param name="captionText">Text that goes in the window caption</param>
/// <param name="instructionText">Instructional text (Appears next to the icon)</param>
/// <param name="captionText">Text that goes in the window's caption</param>
/// <param name="instructionText">Instructional text (Appears next to the error icon)</param>
/// <param name="messageText">Smaller message detail text at bottom</param>
public void ShowSimple(String captionText,
String instructionText,

View File

@ -8,9 +8,8 @@ using System.Windows;
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("PdfScribe")]
[assembly: AssemblyDescription("Postscript output redirector")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyDescription("Postscript printer output redirector")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.

View File

@ -77,6 +77,7 @@ namespace PdfScribeCore
const string NATIVE_COULDNOTENABLE64REDIRECTION = "Could not enable 64-bit file system redirection.";
const string NATIVE_COULDNOTREVERT64REDIRECTION = "Could not revert 64-bit file system redirection.";
const string INSTALL_ROLLBACK_FAILURE_AT_FUNCTION = "Partial uninstallation failure. Function {0} returned false.";
#endregion
@ -108,20 +109,38 @@ namespace PdfScribeCore
#region Port operations
#if DEBUG
public int AddPdfScribePort_Test()
public bool AddPdfScribePort_Test()
{
return AddPdfScribePort();
}
#endif
private int AddPdfScribePort()
private bool AddPdfScribePort()
{
return DoXcvDataPortOperation(PORTNAME, PORTMONITOR, "AddPort");
bool portAdded = false;
int portAddResult = DoXcvDataPortOperation(PORTNAME, PORTMONITOR, "AddPort");
switch (portAddResult)
{
case 0:
portAdded = true;
break;
}
return portAdded;
}
public void DeletePdfScribePort()
public bool DeletePdfScribePort()
{
DoXcvDataPortOperation(PORTNAME, PORTMONITOR, "DeletePort");
bool portDeleted = false;
int portDeleteResult = DoXcvDataPortOperation(PORTNAME, PORTMONITOR, "DeletePort");
switch (portDeleteResult)
{
case 0:
portDeleted = true;
break;
}
return portDeleted;
}
/// <summary>
@ -130,7 +149,7 @@ namespace PdfScribeCore
/// <param name="portName"></param>
/// <param name="xcvDataOperation"></param>
/// <returns></returns>
/// <remarks>I can't remember the name of the developer who wrote this code originally,
/// <remarks>I can't remember the name/link of the developer who wrote this code originally,
/// so I can't provide a link or credit.</remarks>
private int DoXcvDataPortOperation(string portName, string portMonitor, string xcvDataOperation)
{
@ -284,7 +303,16 @@ namespace PdfScribeCore
{
bool monitorRemoved = false;
if ((NativeMethods.DeleteMonitor(null, ENVIRONMENT_64, PORTMONITOR)) != 0)
{
monitorRemoved = true;
// Try to remove the monitor DLL now
if (!DeletePdfScribePortMonitorDll())
{
logEventSource.TraceEvent(TraceEventType.Warning,
(int)TraceEventType.Warning,
"Could not remove port monitor dll.");
}
}
return monitorRemoved;
}
@ -435,17 +463,12 @@ namespace PdfScribeCore
driverDirectory,
1024,
ref dirSizeInBytes))
throw new ApplicationException("Could not retrieve printer driver directory.");
throw new DirectoryNotFoundException("Could not retrieve printer driver directory.");
return driverDirectory.ToString();
}
#if DEBUG
public bool InstallSoftscanPrinter_Test()
{
String driverSourceDirectory = @"C:\Code\PdfScribe\Lib\";
return InstallPdfScribePrinter(driverSourceDirectory);
}
#endif
delegate bool undoInstall();
/// <summary>
/// Installs the port monitor, port,
@ -460,24 +483,27 @@ namespace PdfScribeCore
bool printerInstalled = false;
Stack<undoInstall> undoInstallActions = new Stack<undoInstall>();
String driverDirectory = RetrievePrinterDriverDirectory();
undoInstallActions.Push(this.RemovePdfScribePortMonitor);
if (AddPdfScribePortMonitor(driverSourceDirectory))
{
//if (CopyPrinterDriverFiles(driverSourceDirectory, driverFilesToCopy.Concat(dependentFilesToCopy).ToArray()))
if (CopyPrinterDriverFiles(driverSourceDirectory, printerDriverFiles.Concat(printerDriverDependentFiles).ToArray()))
{
if (AddPdfScribePort() == 0)
undoInstallActions.Push(this.RemovePdfScribePortMonitor);
if (AddPdfScribePort())
{
undoInstallActions.Push(this.RemovePDFScribePrinterDriver);
if (InstallPdfScribePrinterDriver())
{
undoInstallActions.Push(this.DeletePdfScribePrinter);
if (AddPdfScribePrinter())
{
undoInstallActions.Push(this.RemovePdfScribePortConfig);
if (ConfigurePdfScribePort())
{
printerInstalled = true;
}
else
{
// Failed to configure port
@ -507,6 +533,31 @@ namespace PdfScribeCore
{
//Failed to add port monitor
}
if (printerInstalled == false)
{
// Printer installation failed -
// undo all the install steps
while (undoInstallActions.Count > 0)
{
undoInstall undoAction = undoInstallActions.Pop();
try
{
if (!undoAction())
{
this.logEventSource.TraceEvent(TraceEventType.Error,
(int)TraceEventType.Error,
String.Format(INSTALL_ROLLBACK_FAILURE_AT_FUNCTION, undoAction.Method.Name));
}
}
catch (Win32Exception win32Ex)
{
this.logEventSource.TraceEvent(TraceEventType.Error,
(int)TraceEventType.Error,
String.Format(INSTALL_ROLLBACK_FAILURE_AT_FUNCTION, undoAction.Method.Name) +
String.Format(WIN32ERROR, win32Ex.ErrorCode.ToString()));
}
}
}
return printerInstalled;
}
@ -517,13 +568,18 @@ namespace PdfScribeCore
/// <returns></returns>
public bool UninstallPdfScribePrinter()
{
bool printerUninstalled = false;
bool printerUninstalled = true;
DeletePdfScribePrinter();
RemovePDFScribePrinterDriver();
DeletePdfScribePort();
RemovePdfScribePortMonitor();
RemovePdfScribePortConfig();
if (!DeletePdfScribePrinter())
printerUninstalled = false;
if (!RemovePDFScribePrinterDriver())
printerUninstalled = false;
if (!DeletePdfScribePort())
printerUninstalled = false;
if (!RemovePdfScribePortMonitor())
printerUninstalled = false;
if (!RemovePdfScribePortConfig())
printerUninstalled = false;
return printerUninstalled;
}
@ -660,18 +716,31 @@ namespace PdfScribeCore
printerDriverInstalled = NativeMethods.AddPrinterDriver(null, 6, ref printerDriverInfo);
if (printerDriverInstalled == false)
{
int lastWinError = Marshal.GetLastWin32Error();
throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not add printer PDF Scribe printer driver.");
//int lastWinError = Marshal.GetLastWin32Error();
//throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not add printer PDF Scribe printer driver.");
logEventSource.TraceEvent(TraceEventType.Error,
(int)TraceEventType.Error,
"Could add PDF Scribe printer driver." +
String.Format(WIN32ERROR, Marshal.GetLastWin32Error().ToString()));
}
return printerDriverInstalled;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public bool RemovePDFScribePrinterDriver()
{
bool driverRemoved = NativeMethods.DeletePrinterDriverEx(null, ENVIRONMENT_64, DRIVERNAME, DPD_DELETE_UNUSED_FILES, 3);
if (!driverRemoved)
{
throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not remove PDF Scribe printer driver");
//throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not remove PDF Scribe printer driver");
logEventSource.TraceEvent(TraceEventType.Error,
(int)TraceEventType.Error,
"Could not remove PDF Scribe printer driver." +
String.Format(WIN32ERROR, Marshal.GetLastWin32Error().ToString()));
}
return driverRemoved;
}
@ -690,16 +759,20 @@ namespace PdfScribeCore
pdfScribePrinter.pDatatype = "RAW";
pdfScribePrinter.Attributes = 0x00000002;
int softScanPrinterHandle = NativeMethods.AddPrinter(null, 2, ref pdfScribePrinter);
if (softScanPrinterHandle != 0)
int pdfScribePrinterHandle = NativeMethods.AddPrinter(null, 2, ref pdfScribePrinter);
if (pdfScribePrinterHandle != 0)
{
// Added ok
int closeCode = NativeMethods.ClosePrinter((IntPtr)softScanPrinterHandle);
int closeCode = NativeMethods.ClosePrinter((IntPtr)pdfScribePrinterHandle);
printerAdded = true;
}
else
{
throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not add PDF Scribe virtual printer.");
//throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not add PDF Scribe virtual printer.");
logEventSource.TraceEvent(TraceEventType.Error,
(int)TraceEventType.Error,
"Could not add PDF Scribe virtual printer." +
String.Format(WIN32ERROR, Marshal.GetLastWin32Error().ToString()));
}
return printerAdded;
}
@ -723,7 +796,10 @@ namespace PdfScribeCore
}
else
{
// log error
logEventSource.TraceEvent(TraceEventType.Error,
(int)TraceEventType.Error,
"Could not delete PDF Scribe virtual printer." +
String.Format(WIN32ERROR, Marshal.GetLastWin32Error().ToString()));
}
}
finally
@ -758,9 +834,9 @@ namespace PdfScribeCore
portConfiguration.SetValue("Description", "PDF Scribe", RegistryValueKind.String);
portConfiguration.SetValue("Command", "", RegistryValueKind.String);
portConfiguration.SetValue("Arguments", "", RegistryValueKind.String);
portConfiguration.SetValue("Printer", "", RegistryValueKind.String);
portConfiguration.SetValue("Printer", PRINTERNAME, RegistryValueKind.String);
portConfiguration.SetValue("Output", 0, RegistryValueKind.DWord);
portConfiguration.SetValue("ShowWindow", 0, RegistryValueKind.DWord);
portConfiguration.SetValue("ShowWindow", 2, RegistryValueKind.DWord);
portConfiguration.SetValue("RunUser", 1, RegistryValueKind.DWord);
portConfiguration.SetValue("Delay", 300, RegistryValueKind.DWord);
portConfiguration.SetValue("LogFileUse", 0, RegistryValueKind.DWord);

View File

@ -7,7 +7,6 @@ using System.Runtime.InteropServices;
// associated with an assembly.
[assembly: AssemblyTitle("PdfScribeCore")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from

View File

@ -20,7 +20,7 @@
-->
<supportedRuntime version="v4.0" />
<supportedRuntime version="v2.0.50727"/>
<!-- <supportedRuntime version="v2.0.50727"/> -->
</startup>

View File

@ -51,6 +51,26 @@
<CodeAnalysisIgnoreBuiltInRuleSets>true</CodeAnalysisIgnoreBuiltInRuleSets>
<CodeAnalysisIgnoreBuiltInRules>true</CodeAnalysisIgnoreBuiltInRules>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>AnyCPU</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisIgnoreBuiltInRuleSets>true</CodeAnalysisIgnoreBuiltInRuleSets>
<CodeAnalysisIgnoreBuiltInRules>true</CodeAnalysisIgnoreBuiltInRules>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>AnyCPU</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisIgnoreBuiltInRuleSets>true</CodeAnalysisIgnoreBuiltInRuleSets>
<CodeAnalysisIgnoreBuiltInRules>true</CodeAnalysisIgnoreBuiltInRules>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />

View File

@ -25,6 +25,7 @@
<ErrorReportUrlHistory />
<FallbackCulture>en-US</FallbackCulture>
<VerifyUploadedFiles>false</VerifyUploadedFiles>
<ProjectView>ShowAllFiles</ProjectView>
</PropertyGroup>
<PropertyGroup>
<ReferencePath>

View File

@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.0.*")]
//[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -37,7 +37,7 @@ namespace PdfScribeUnitTests
public void Test_InstallPdfScribePrinter()
{
var scribeInstaller = new PdfScribeInstaller();
scribeInstaller.InstallSoftscanPrinter_Test();
scribeInstaller.InstallPdfScribePrinter(@"C:\Code\PdfScribe\Lib\");
}
//[Test]