Reputation: 1
I'm an extremely novice programmer in a bit of a pickle.
Background:
Within my company we use a VMware environment for our users. They launch their virtual desktop and ThinPrint will provide a fresh printer install for that user's session. That printer install is determined by the VLAN the user is logging in from. The printer is installed with whatever default settings are part of the driver.
Problem:
We have a specific Zebra thermal label printer that is giving us a problem. The printer uses labels of a certain dimension which need to be defined within the driver. Unlike conventional printers that utilize built-in Windows Printer Forms, this Zebra Printer uses these proprietary forms that are called "stocks". These stocks are built into the Zebra driver. I can easily create a stock with the appropriate dimensions and save it, but the problem lies with this being in a VM environment. I need to have my custom stock be part of the printer install for the session that the user logs into. ThinPrint will only install the printer as a fresh install every time and won't include the custom stock.
Why I Need Your Help:
I've discovered the registry key that corresponds with the custom stock I create. I can inject this registry key and the stock shows up beautifully even without a reboot. So I could easily make a batch file that injects the key during login. The registry key, however, is a subkey of the specific printer name. That means that the REG file I create would have to be tailored to the printer name during that user's session, which would of course vary for all the different zebra printers we have in the company.
Here is an example of the REG file:
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers\CC_ZEBRA\PrinterDriverData\Papers] "StockNum"=dword:00000001
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers\CC_ZEBRA\PrinterDriverData\Papers\0] "Data"=hex:4f,6e,2d,44,65,6d,61,6e,64,00,00,00,00,00,00,00,00,00,00,00,00,00,\ 00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,\ 00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,01,01,00,00,1e,00,03,00,\ 00,00,00,00,fe,00,fa,02,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,\ 00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,\ 00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,\ 00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,\ 00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,\ 00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,\ 00,00,00,00,00,00,00,00,00,00,ec,00,00,00
Everything in this REG file would stay the same with the exception of the CC_ZEBRA key. I need a way to parse through the list of installed printers and replace that key with the printer name that's installed. Alternately I've considered giving all of our zebra printers incremental names (Zebra1, Zebra2, Zebra3, etc...) and looping through until it finds a match and then runs the tailored REG file I have for that printer.
Any help or suggestions would be appreciated everyone! Thank you in advanced!
Upvotes: 0
Views: 386
Reputation: 2737
Here's a simple C# example app (per your specs) that gets the installed printers and injects a new registry key if the printer name matches a filter. This was written with .Net 4.5.
using System;
using System.Drawing.Printing;
using System.Linq;
using Microsoft.Win32;
namespace GetPrintersAndUpdateRegistryExample
{
class Program
{
// Needs to run as admin in order to create subkey in registry
static void Main(string[] args)
{
LaunchServiceUpdate();
}
public static void LaunchServiceUpdate()
{
for (var i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
{
var printerPath = PrinterSettings.InstalledPrinters[i];
// Replace \sea_ with a search filter that makes sense for you. All our printers start with 'sea'.
if (!printerPath.Contains(@"\sea_"))
{
continue;
}
var printerName = printerPath.Split('\\').Last();
CreateRegistryEntry(printerName);
}
}
private static void CreateRegistryEntry(string printerName)
{
try
{
var regPath = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers\" + printerName + @"\PrinterDriverData\Papers";
var key = Registry.LocalMachine.CreateSubKey(regPath);
key.SetValue("StockNum", 1, RegistryValueKind.DWord);
key.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.Read();
}
}
}
}
Upvotes: 0