Added uninstall methods, and application manifest for PdfScribeUnitTests project

This commit is contained in:
S T Chan 2013-12-12 19:29:31 -05:00
parent e012a80ffa
commit 80c6ba8f58
5 changed files with 197 additions and 29 deletions

View File

@ -98,23 +98,23 @@ namespace PdfScribe
public uint AveragePPM; public uint AveragePPM;
} }
public static class NativeMethods internal static class NativeMethods
{ {
#region winspool
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)] [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool EnumMonitors(string pName, uint level, IntPtr pMonitors, uint cbBuf, ref uint pcbNeeded, ref uint pcReturned); internal static extern bool EnumMonitors(string pName, uint level, IntPtr pMonitors, uint cbBuf, ref uint pcbNeeded, ref uint pcReturned);
[DllImport("winspool.drv", SetLastError = true, CharSet = CharSet.Auto)] [DllImport("winspool.drv", SetLastError = true, CharSet = CharSet.Auto)]
public static extern Int32 AddMonitor(String pName, UInt32 Level, ref MONITOR_INFO_2 pMonitors); internal static extern Int32 AddMonitor(String pName, UInt32 Level, ref MONITOR_INFO_2 pMonitors);
[DllImport("winspool.drv", SetLastError = true, CharSet = CharSet.Auto)] [DllImport("winspool.drv", SetLastError = true, CharSet = CharSet.Auto)]
public static extern Int32 DeleteMonitor(String pName, String pEnvironment, String pMonitorName); internal static extern Int32 DeleteMonitor(String pName, String pEnvironment, String pMonitorName);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);
[DllImport("winspool.drv", EntryPoint = "XcvDataW", SetLastError = true)] [DllImport("winspool.drv", EntryPoint = "XcvDataW", SetLastError = true)]
public static extern bool XcvData(IntPtr hXcv, internal static extern bool XcvData(IntPtr hXcv,
[MarshalAs(UnmanagedType.LPWStr)] string pszDataName, [MarshalAs(UnmanagedType.LPWStr)] string pszDataName,
IntPtr pInputData, IntPtr pInputData,
uint cbInputData, uint cbInputData,
@ -126,38 +126,50 @@ namespace PdfScribe
[DllImport("winspool.drv", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)] [DllImport("winspool.drv", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
public static extern int AddPrinter(string pName, uint Level, [In] ref PRINTER_INFO_2 pPrinter); internal static extern int AddPrinter(string pName, uint Level, [In] ref PRINTER_INFO_2 pPrinter);
[DllImport("winspool.drv", EntryPoint = "OpenPrinterA", SetLastError = true)] [DllImport("winspool.drv", EntryPoint = "OpenPrinterA", SetLastError = true)]
public static extern int OpenPrinter( internal static extern int OpenPrinter(string pPrinterName,
string pPrinterName,
ref IntPtr phPrinter, ref IntPtr phPrinter,
PRINTER_DEFAULTS pDefault); PRINTER_DEFAULTS pDefault);
[DllImport("winspool.drv", SetLastError = true)] [DllImport("winspool.drv", SetLastError = true)]
public static extern int ClosePrinter(IntPtr hPrinter); internal static extern int ClosePrinter(IntPtr hPrinter);
[DllImport("winspool.drv", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)] [DllImport("winspool.drv", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
public static extern bool DeletePrinter(IntPtr hPrinter); internal static extern bool DeletePrinter(IntPtr hPrinter);
[DllImport("winspool.drv", EntryPoint="AddPrinterDriver", CharSet = CharSet.Auto, SetLastError = true)] [DllImport("winspool.drv", EntryPoint="AddPrinterDriver", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool AddPrinterDriver(String pName, internal static extern bool AddPrinterDriver(String pName,
int Level, int Level,
ref DRIVER_INFO_6 pDriverInfo); ref DRIVER_INFO_6 pDriverInfo);
[DllImport("winspool.drv")] [DllImport("winspool.drv")]
public static extern bool GetPrinterDriverDirectory(StringBuilder pName, internal static extern bool GetPrinterDriverDirectory(StringBuilder pName,
StringBuilder pEnv, StringBuilder pEnv,
int Level, int Level,
[Out] StringBuilder outPath, [Out] StringBuilder outPath,
int bufferSize, int bufferSize,
ref int Bytes); ref int Bytes);
[DllImport("winspool.drv", EntryPoint = "DeletePrinterDriverEx", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool DeletePrinterDriverEx(String pName,
String pEnvironment,
String pDriverName,
uint dwDeleteFlag,
uint dwVersionFlag);
#endregion
#region Kernel32
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr); internal static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);
#endregion
} }
} }

View File

@ -10,6 +10,24 @@ namespace PdfScribe
{ {
public class PdfScribeInstaller public class PdfScribeInstaller
{ {
#region Printer Driver Win32 API Constants
const uint DRIVER_KERNELMODE = 0x00000001;
const uint DRIVER_USERMODE = 0x00000002;
const uint APD_STRICT_UPGRADE = 0x00000001;
const uint APD_STRICT_DOWNGRADE = 0x00000002;
const uint APD_COPY_ALL_FILES = 0x00000004;
const uint APD_COPY_NEW_FILES = 0x00000008;
const uint APD_COPY_FROM_DIRECTORY = 0x00000010;
const uint DPD_DELETE_UNUSED_FILES = 0x00000001;
const uint DPD_DELETE_SPECIFIC_VERSION = 0x00000002;
const uint DPD_DELETE_ALL_FILES = 0x00000004;
#endregion
const string ENVIRONMENT_64 = "Windows x64"; const string ENVIRONMENT_64 = "Windows x64";
const string PRINTERNAME = "PDF Scribe"; const string PRINTERNAME = "PDF Scribe";
@ -23,17 +41,20 @@ namespace PdfScribe
const string DRIVERMANUFACTURER = "S T Chan"; const string DRIVERMANUFACTURER = "S T Chan";
const string DRIVERFILE = "PSCRIPT5.DLL"; const string DRIVERFILE = "PSCRIPT5.DLL";
const int DRIVERFILE_INDEX = 0;
const string DRIVERUIFILE = "PS5UI.DLL"; const string DRIVERUIFILE = "PS5UI.DLL";
const int DRIVERUIFILE_INDEX = 1;
const string DRIVERHELPFILE = "PSCRIPT.HLP"; const string DRIVERHELPFILE = "PSCRIPT.HLP";
const int DRIVERHELPFILE_INDEX = 2;
const string DRIVERDATAFILE = "SCPDFPRN.PPD"; const string DRIVERDATAFILE = "SCPDFPRN.PPD";
const int DRIVERDATAFILE_INDEX = 3;
#region Port operations #region Port operations
#if DEBUG
public int AddPdfScribePort_Test(string portName)
{
return AddPdfScribePort(portName);
}
#endif
private int AddPdfScribePort(string portName) private int AddPdfScribePort(string portName)
{ {
return DoXcvDataPortOperation(portName, "AddPort"); return DoXcvDataPortOperation(portName, "AddPort");
@ -110,7 +131,15 @@ namespace PdfScribe
// the system directory // the system directory
String fileSourcePath = Path.Combine(monitorFilePath, MONITORDLL); String fileSourcePath = Path.Combine(monitorFilePath, MONITORDLL);
String fileDestinationPath = Path.Combine(Environment.SystemDirectory, MONITORDLL); String fileDestinationPath = Path.Combine(Environment.SystemDirectory, MONITORDLL);
try
{
File.Copy(fileSourcePath, fileDestinationPath, true); File.Copy(fileSourcePath, fileDestinationPath, true);
}
catch (IOException)
{
// File in use, log -
// this is OK because it means the file is already there
}
MONITOR_INFO_2 newMonitor = new MONITOR_INFO_2(); MONITOR_INFO_2 newMonitor = new MONITOR_INFO_2();
newMonitor.pName = PORTMONITOR; newMonitor.pName = PORTMONITOR;
newMonitor.pEnvironment = ENVIRONMENT_64; newMonitor.pEnvironment = ENVIRONMENT_64;
@ -242,7 +271,7 @@ namespace PdfScribe
} }
else else
{ {
throw new ApplicationException("Call to winspool.drv succeeded with a zero size buffer - unexpected error."); throw new ApplicationException("Call to EnumMonitors in winspool.drv succeeded with a zero size buffer - unexpected error.");
} }
return portMonitors; return portMonitors;
@ -269,7 +298,7 @@ namespace PdfScribe
#if DEBUG #if DEBUG
public bool InstallSoftscanPrinter_Test() public bool InstallSoftscanPrinter_Test()
{ {
String driverSourceDirectory = @"C:\Code\OaisisRedmonInstaller\Lib\"; String driverSourceDirectory = @"C:\Code\PdfScribe\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(driverSourceDirectory, driverFilesToCopy, dependentFilesToCopy); return InstallPdfScribePrinter(driverSourceDirectory, driverFilesToCopy, dependentFilesToCopy);
@ -291,11 +320,11 @@ namespace PdfScribe
bool printerInstalled = false; bool printerInstalled = false;
String driverDirectory = RetrievePrinterDriverDirectory(); String driverDirectory = RetrievePrinterDriverDirectory();
if (AddPdfScribePort(PORTNAME) == 0) if (AddPdfScribePortMonitor(driverSourceDirectory))
{ {
if (CopyPrinterDriverFiles(driverSourceDirectory, driverFilesToCopy.Concat(dependentFilesToCopy).ToArray())) if (CopyPrinterDriverFiles(driverSourceDirectory, driverFilesToCopy.Concat(dependentFilesToCopy).ToArray()))
{ {
if (AddPdfScribePortMonitor(driverSourceDirectory)) if (AddPdfScribePort(PORTNAME) == 0)
{ {
if (InstallPrinterDriver(driverDirectory, dependentFilesToCopy)) if (InstallPrinterDriver(driverDirectory, dependentFilesToCopy))
{ {
@ -307,6 +336,22 @@ namespace PdfScribe
return printerInstalled; return printerInstalled;
} }
/// <summary>
///
/// </summary>
/// <returns></returns>
public bool UninstallPdfScribePrinter()
{
bool printerUninstalled = false;
DeletePdfScribePrinter();
RemovePDFScribePrinterDriver();
DeletePdfScribePort(PORTNAME);
RemovePdfScribePortMonitor();
return printerUninstalled;
}
private bool CopyPrinterDriverFiles(String driverSourceDirectory, private bool CopyPrinterDriverFiles(String driverSourceDirectory,
String[] filesToCopy) String[] filesToCopy)
{ {
@ -412,6 +457,17 @@ namespace PdfScribe
} }
public bool RemovePDFScribePrinterDriver()
{
bool printerRemoved = NativeMethods.DeletePrinterDriverEx(null, ENVIRONMENT_64, DRIVERNAME, DPD_DELETE_UNUSED_FILES, 3);
if (!printerRemoved)
{
throw new Win32Exception(Marshal.GetLastWin32Error(), "Could not remove PDF Scribe printer driver");
}
return printerRemoved;
}
private bool AddPdfScribePrinter() private bool AddPdfScribePrinter()
{ {
bool printerAdded = false; bool printerAdded = false;
@ -439,6 +495,34 @@ namespace PdfScribe
return printerAdded; return printerAdded;
} }
private bool DeletePdfScribePrinter()
{
bool printerDeleted = false;
PRINTER_DEFAULTS scribeDefaults = new PRINTER_DEFAULTS();
scribeDefaults.DesiredAccess = 0x000F000C; // All access
scribeDefaults.pDatatype = null;
scribeDefaults.pDevMode = IntPtr.Zero;
IntPtr scribeHandle = IntPtr.Zero;
try
{
if (NativeMethods.OpenPrinter(PRINTERNAME, ref scribeHandle, scribeDefaults) != 0)
{
if (NativeMethods.DeletePrinter(scribeHandle))
printerDeleted = true;
}
else
{
// log error
}
}
finally
{
if (scribeHandle != IntPtr.Zero) NativeMethods.ClosePrinter(scribeHandle);
}
return printerDeleted;
}
#endregion #endregion

View File

@ -13,6 +13,7 @@
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion> <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile>Client</TargetFrameworkProfile> <TargetFrameworkProfile>Client</TargetFrameworkProfile>
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
<IsWebBootstrapper>false</IsWebBootstrapper>
<PublishUrl>publish\</PublishUrl> <PublishUrl>publish\</PublishUrl>
<Install>true</Install> <Install>true</Install>
<InstallFrom>Disk</InstallFrom> <InstallFrom>Disk</InstallFrom>
@ -25,7 +26,6 @@
<MapFileExtensions>true</MapFileExtensions> <MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision> <ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion> <ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust> <UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled> <BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup> </PropertyGroup>
@ -71,6 +71,9 @@
<CodeAnalysisIgnoreBuiltInRuleSets>true</CodeAnalysisIgnoreBuiltInRuleSets> <CodeAnalysisIgnoreBuiltInRuleSets>true</CodeAnalysisIgnoreBuiltInRuleSets>
<CodeAnalysisIgnoreBuiltInRules>true</CodeAnalysisIgnoreBuiltInRules> <CodeAnalysisIgnoreBuiltInRules>true</CodeAnalysisIgnoreBuiltInRules>
</PropertyGroup> </PropertyGroup>
<PropertyGroup>
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="nunit-console-runner"> <Reference Include="nunit-console-runner">
<HintPath>..\Lib\nunit-console-runner.dll</HintPath> <HintPath>..\Lib\nunit-console-runner.dll</HintPath>
@ -108,6 +111,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="App.config" /> <None Include="App.config" />
<None Include="app.manifest" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.0,Profile=Client"> <BootstrapperPackage Include=".NETFramework,Version=v4.0,Profile=Client">

View File

@ -17,12 +17,32 @@ namespace PdfScribeUnitTests
{ } { }
#endregion #endregion
[Test] //[Test]
public void Test_DeletePdfScribePort() public void Test_DeletePdfScribePort()
{ {
var scribeInstaller = new PdfScribeInstaller(); var scribeInstaller = new PdfScribeInstaller();
scribeInstaller.DeletePdfScribePort("SSCAN"); scribeInstaller.DeletePdfScribePort("SSCAN");
} }
//[Test]
public void Test_RemovePdfScribeDriver()
{
var scribeInstaller = new PdfScribeInstaller();
scribeInstaller.RemovePDFScribePrinterDriver();
}
//[Test]
public void Test_InstallPdfScribePrinter()
{
var scribeInstaller = new PdfScribeInstaller();
scribeInstaller.InstallSoftscanPrinter_Test();
}
[Test]
public void Test_UninstallPdfScribePrinter()
{
var scribeInstaller = new PdfScribeInstaller();
scribeInstaller.UninstallPdfScribePrinter();
}
} }
} }

View File

@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<!-- UAC Manifest Options
If you want to change the Windows User Account Control level replace the
requestedExecutionLevel node with one of the following.
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
Specifying requestedExecutionLevel node will disable file and registry virtualization.
If you want to utilize File and Registry Virtualization for backward
compatibility then delete the requestedExecutionLevel node.
-->
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- A list of all Windows versions that this application is designed to work with. Windows will automatically select the most compatible environment.-->
<!-- If your application is designed to work with Windows 7, uncomment the following supportedOS node-->
<!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>-->
</application>
</compatibility>
<!-- Enable themes for Windows common controls and dialogs (Windows XP and later) -->
<!-- <dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>-->
</asmv1:assembly>