Need help with redirected monitor and printer port creation

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am attempting to make a program that creates a printer, a port, and a port monitor in the registry. Now I want the port monitor to open an executable file (here I am just using notepad as a placeholder) instead of sending information to the printer.
Everything in my code seemingly works. Everything installs without any trouble. The only issue is that the port monitor does not look at the port I am telling it to. So essentially, the printer is in the registry along with the control panel,
the port is listed, and the port monitor is in the registry. If I print to the printer, it sits for a moment, then no notepad. Does anyone have any ideas? Thank you for your help.

<pre class="prettyprint using System;
using System.Windows.Forms;
using System.Management;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
using System.Diagnostics;
using System.Threading;


namespace Utility.ModifyRegistry
{
public class Program
{
static void Main(string[] args)
{
ModifyRegistry regmod = new ModifyRegistry();
PrintingUtil util = new PrintingUtil(null, null, null);
//util.AddPort("winning");
PrintingUtil.StopService("spooler", 600);
util.AddSoftPort("testing");
System.Threading.Thread.Sleep(1000);
PrintingUtil.StartService("spooler", 600);
regmod.ReadPortMon();
util.AddPrinter("Testing", "HP Universal Printing PCL 6", "testing", false);
//util.ModifyPort();
//PrinterPortCreation create = new PrinterPortCreation();
//PrinterPortCreation.AddMonitorPrinterPort("TestPort", "Redirected");
}
}
public class ModifyRegistry
{
bool found = false;
public string portMonName = "testPortMonitor";
public void ReadPortMon()
{
found = false;
RegistryKey testPortMon = Registry.LocalMachine.OpenSubKey("SYSTEM\ControlSet001\Control\Print\Monitors", true);
foreach (string pp in testPortMon.GetSubKeyNames())
{
if (pp == portMonName)
{
found = true; break;
}
}
if (found)
{
ModifyPortMon();
}
else
{
CreatePortMon();
}
}
public void CreatePortMon()
{
RegistryKey testPortMon = Registry.LocalMachine.OpenSubKey("System\ControlSet001\Control\Print\Monitors", true);
testPortMon.CreateSubKey(portMonName);
ReadPortMon();
}

public void RestartComputer()
{
System.Diagnostics.Process.Start("ShutDown", "-r -t 0");
}

//Port settings must be modified seperately from creation.
public void ModifyPortMon()
{
RegistryKey testPortMon = Registry.LocalMachine.OpenSubKey("System\ControlSet001\Control\Print\Monitors", true);
RegistryKey testMon = Registry.LocalMachine.OpenSubKey("System\ControlSet001\Control\Print\Monitors\testPortMonitor", true);
foreach (string pp in testPortMon.GetSubKeyNames())
{
if (pp == "testPortMonitor")
{
found = true; break;
}
testMon.SetValue(@"Command", "C:\WINDOWS\NOTEPAD.EXE");
testMon.SetValue(@"Printer", "Testing");
testMon.SetValue(@"Port", "Software\Microsoft\Windows NT\CurrentVersion\Ports\testing");
testMon.SetValue(@"Driver", "ddklocalmon.dll");
}
testMon.Close();
}
}

class PrintingUtil
{
private ManagementScope managementScope = null;

public PrintingUtil(string serverName, string adminUser, string adminUserPassword)
{
try
{
ConnectionOptions connectionOptions = new ConnectionOptions();
string wmiPath;
if (serverName == null || serverName.Length == 0 || adminUser == null || adminUser.Length == 0 || adminUserPassword == null || adminUserPassword.Length == 0)
{
//specify WMI path for local machine
wmiPath = @"rootcimv2";
}
else
{
//set connection parameters for remote machine
connectionOptions.Username = adminUser;
connectionOptions.Password = adminUserPassword;
//specify WMI path for remote machine
wmiPath = String.Format(@"\{0}rootcimv2", serverName);
}
managementScope = new ManagementScope(wmiPath, connectionOptions);
}
catch (Exception ex)
{
throw new Exception(
String.Format("WMI exception: {0}", ex.Message));
}
}

private ManagementClass InitClass(string className)
{
//specify Printer class management path
ManagementPath managementPath = new ManagementPath(className);
try
{
//create new WMI management class
return new ManagementClass(managementScope, managementPath, null);
}
catch (Exception ex)
{
throw new Exception(
String.Format(
"WMI exception: {0}", ex.Message));
}
}

public bool AddSoftPort(string portName)
{
RegistryKey softwarePort = Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows NT\CurrentVersion\Ports", true);
softwarePort.SetValue(portName, "");
softwarePort.Close();
return true;
}

//public bool AddPort(string portName)
//{
// bool result = false;
// try
// {
// ManagementClass printerClass = InitClass("Win32_TCPIPPrinterPort");
// ManagementObject printerObject = printerClass.CreateInstance();

// printerObject["HostAddress"] = "127.0.0.1";
// printerObject["Name"] = portName;
// printerObject["Protocol"] = 1; // Note 1 = Raw, 2 = LPR

// PutOptions options = new PutOptions();
// options.Type = PutType.UpdateOrCreate;
// printerObject.Put(options);
// result = true;
// }
// catch (Exception ex)
// {
// throw new Exception(String.Format("WMI exception: {0}", ex.Message));
// }
// return result;
//}

public bool AddPrinter(string printerName, string printerDriver, string portName, bool sharedPrinter)
{
bool result = false;
try
{
//init Win32_Printer class
ManagementClass printerClass = InitClass("Win32_Printer");
//create new Win32_Printer object
ManagementObject printerObject = printerClass.CreateInstance();
//set port parameters
if (portName == null || portName.Length == 0)
printerObject["PortName"] = "LPT1:";
else
{
//if (portName[portName.Length - 1] != :)
// printerObject["PortName"] = (portName + ":");
//else
printerObject["PortName"] = portName;
}
//set driver and device names
printerObject["DriverName"] = printerDriver;

printerObject["DeviceID"] = printerName;
//set sharing
if (sharedPrinter)
{
printerObject["Shared"] = sharedPrinter;
printerObject["ShareName"] = printerName;
}
// specify put options: update or create
PutOptions options = new PutOptions();
options.Type = PutType.UpdateOrCreate;
//put a newly created object to WMI objects set
printerObject.Put(options);

result = true;
}
catch (Exception ex)
{
throw new Exception(String.Format("WMI exception: {0}", ex.Message));
}
return result;
}

public bool AddDriver(string driverName, string infLoc)
{

bool result = false;

try
{

ManagementClass printerClass = InitClass("Win32_PrinterDriver");

ManagementObject printerObject = printerClass.CreateInstance();

printerObject["DriverName"] = driverName;

printerObject["InfName"] = infLoc;

PutOptions options = new PutOptions();

options.Type = PutType.UpdateOrCreate;

printerObject.Put(options);

result = true;

}

catch (Exception ex)
{

throw new Exception(String.Format("WMI exception: {0}", ex.Message));

}

return result;

}

//public void ModifyPort()
//{
// bool found = true;
// RegistryKey testPortMon = Registry.LocalMachine.OpenSubKey("System\ControlSet001\Control\Print\Monitors\Standard TCP/IP Port\Ports", true);
// RegistryKey testMon = Registry.LocalMachine.OpenSubKey("System\ControlSet001\Control\Print\Monitors\Standard TCP/IP Port\Ports\winning", true);
// foreach (string pp in testPortMon.GetSubKeyNames())
// {
// if (pp == "winning")
// {
// found = true; break;
// }
// testMon.SetValue(@"Command", "C:\WINDOWS\NOTEPAD.EXE");
// testMon.SetValue(@"Printer", "Testing");
// testMon.SetValue(@"Port", "winning");
// }
// testMon.Close();
//}

public static void StartService(string serviceName, int timeoutMilliseconds)
{
using (ServiceController service = new ServiceController(serviceName))
{
// Do not start the service if it is already running
if (service.Status == ServiceControllerStatus.Running)
return;

try
{
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);

Console.WriteLine("The Windows Services have been successfully started");
}
catch (Exception ex)
{
Console.WriteLine("Could not start " + serviceName + " Service.n Error: " + ex.Message.ToString());
}
}
}

public static void StopService(string serviceName, int timeoutMilliseconds)
{
using (ServiceController service = new ServiceController(serviceName))
{
// Do not start the service if it is already running
if (service.Status == ServiceControllerStatus.Stopped)
return;

try
{
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);

Console.WriteLine("The Windows Services have been successfully stopped");
}
catch (Exception ex)
{
Console.WriteLine("Could not stop " + serviceName + " Service.n Error: " + ex.Message.ToString());
}
}
}
}
}[/code]
<br/>


View the full article
 
Back
Top